bugGNU Octave - Bugs: bug #66148, [octave forge] (image) Add...

 
 

bug #66148: [octave forge] (image) Add imbinarize() function for replacement of im2bw() function

Submitter:  Yu Hongbo <yuhongbo>
Submitted:  Fri 30 Aug 2024 02:02:14 PM UTC
   
 
Category:  Octave Package Severity:  1 - Wish
Priority:  3 - Low Item Group:  Feature Request
Status:  Fixed Assigned to:  avinoam
Originator Name:  Open/Closed:  * Closed
Release:  * other Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Post a Comment

Add a New Comment Rich Markup
   

Discussion

Jump to the original submission

Wed 13 Aug 2025 04:00:44 PM UTC, comment #24: 

imbinarize.m was pushed (after fixing the typo).
https://sourcefor ... 3a54690c847cc36d1

Thanks, Hartmut. Closing as fixed

Avinoam Kalma <avinoam>
Group Member
Wed 13 Aug 2025 06:55:29 AM UTC, comment #23: 

Thanks, Avinoam, for changing this back in your patch of comment #22 and enabling the use of 3d images in imbinarize again for the "global" option. I think this is very good and ready now.

I've just found a tiny typo in a comment to a unit test of imbinarize, where it refers to the "gloabl" option.

Hartmut <hardy>
Wed 13 Aug 2025 05:46:08 AM UTC, comment #22: 

Thanks, Hartmut.
Please review the attached patch.
I think it addresses comment #21 correctly.


(file #57524)

Avinoam Kalma <avinoam>
Group Member
Tue 12 Aug 2025 06:50:15 AM UTC, comment #21: 

There is one change in the comment #19 patch for imbinarize that made me first think that it makes this function slightly worse than it was in the proposed function code before:

  • The latest change has put an input check in place, to only accept 2d images in imbinarize. Before this patch, at least the (default) global algorithm  accepted 3d images, just the adaptive algorithm did not work. I still think that the treatment of rgb color images, at least for the default global case, is routinely used by people (even by me). But I now realised that out underlying graythresh function does accept rgb input images, but only returns one single threshhold value for the three color channels. Therefore, also the (previously working) global threshhold on rgb images had not given a useful result. Therefore, I can accept that this feature (rgb image treatment) is now removed even from the global algorithm of imbinarize. -> Nothing to do here (yet).


  • But the Matlab function imbinarize, claims in its help text to also be able to work on 3d grayscale images. This use case seems realistic to me. And we currently already do support this use case in the underlying graythresh function. And we used to support this use case in the original version of imbinarize with the default "global" option. Therefore I would like to see the treatment of 3d grayscale images in imbinarize re-actived. Just not named as treatment of rgb images (in the test comments, and in the help string), but named as treatment of 3d grayscale images, which only works with the "global" algorithmic path and throw an error message if requested together with "adaptive". -> Can you follow this reasonsing?


Additionally, I realised a minor point, where we might improve the image package now:

  • We should probably mention the existence of the two new function imbinarize and adaptthresh in the "see also" text of related existing functions like graytresh and im2bw.
Hartmut <hardy>
Mon 11 Aug 2025 06:21:51 PM UTC, comment #20: 

Thanks Hartmut (comment #18)

adaptthresh.m pushed

https://sourcefor ... 4b69f7fec35268fe/

Avinoam Kalma <avinoam>
Group Member
Mon 11 Aug 2025 05:37:33 PM UTC, comment #19: 

Attached patch for imbinarize.

In this patch:

  • Check that the input image is a 2D grayscale image
  • Special treatment for int8, int32 & uint32, which imacast does not treat
  • Add tests for each allowed data type
  • Add spaces and fix indentation


Please review

(file #57519)

Avinoam Kalma <avinoam>
Group Member
Mon 11 Aug 2025 05:19:18 PM UTC, comment #18: 

The new patch, in comment #17, looks fine to me. Thanks.

Hartmut <hardy>
Sun 10 Aug 2025 09:08:53 PM UTC, comment #17: 

Thanks again for the review.
Please review the updated patch
Thanks :-)

(file #57518)

Avinoam Kalma <avinoam>
Group Member
Sun 10 Aug 2025 06:47:05 PM UTC, comment #16: 

Thanks for this patch. I think it fixes all my mentioned issues. Here are some smaller points I found when reviewing the patch:

  • There is a typo in the help string: "int16m"


  • You check the input image now for "ndims == 3" and throw an error message if true. Wouldn't it be better to check for "ndims > 2" ? (And additionally check for any dim > 2 if it is not equal to 1, is there an easy way to check this? Maybe check of a "squeezed" version of the image is more than 2d?)


  • Matlab does accept 3d images, but we cannot do this at the moment without major effort, because neither our underlying medfilt2 nor our imboxfilt currently supports 3d images. -> Nothing to do here.


  • I think casting int8, int32 and uint32 images to double directly in the code of adaptthresh is fine, because these are not so common image types that most image package functions do not support. (Should we support those more exotic types in more image package functions in the future, this type conversion should probably better go into imcast, instead.) -> Nothing to do here.


Hartmut <hardy>
Sun 10 Aug 2025 02:29:37 PM UTC, comment #15: 


Attached patch for adapthtresh. Indeed, some of the data types were not treated correctly.

In this patch:

  • Check that the input image is a 2D grayscale image
  • Check that the input image is not a logical image
  • Speical treatment for int8, int32 & uint32, which imacast does not treat
  • add tests for each allowed data type
  • Add spaces


We must convert single images to double to achieve compatibility with MATLAB.

Please review


(file #57517)

Avinoam Kalma <avinoam>
Group Member
Sun 03 Aug 2025 10:32:22 AM UTC, comment #14: 

Thanks, @Hartmut, for the review.
I will fix the functions according to your comments.
 

Avinoam Kalma <avinoam>
Group Member
Sun 03 Aug 2025 09:25:58 AM UTC, comment #13: 

Some more feedback on the second new function "imbinarize". In general it work very well, point to possibly improve it are:

  • Please only cast input images with integer types to "double", images of input type "single" should stay as "single", to save   memory. (This does not help for adaptthresh wich needs to cast everything to double, anyways. But it should reduce the memory footprint for the default Otsu method by a factor of two.)


  • The behavior on rgb color input images is currently inconsistent: Color images are treated reasonably well (each color channel treated separatly) when using the "global" method, but color input images throw an error message (by imboxfilt or somewhere else, see above) when used with the "adaptive" method. I suggest to add an input check in imbinarize to throw a helpful error message if someone asks for "adaptive" treatment of a rgb color input image. (The alternative of splitting a rgb input image into 3 grayscale images and process them separately, does not make much sense with the adaptive algorithms, in my opinion.) This shold then also be mentioned in the help text.


  • It would also be nice here, to have a (very simple) test case in place, to check for any allowed input image type (double, single, uintxx, intyy).


Thanks again for these good new functions, Avinoam.

PS: I have not compared the function outputs to Matlab output. I just assume you did this yourself, at least for the used test cases in your code.


Hartmut <hardy>
Sun 03 Aug 2025 08:51:14 AM UTC, comment #12: 

Thanks for those two new functions, Avinoam!
I have just (eventually) checked the function "adaptthresh.m". It looks very nice and fine to me, and all the tests pass. The only things that could be improved in my opinion are:

  • Please let "adaptthresh" throw an error message itself, if the image input is a 3d image. Currently, only an error message of imboxfilt is thrown in this case, but I think adaptthresh should throw this error message itself. Reason: In Matlab, it is possible to use 3d image inputs, and also some users (like me) might unintenionally input rgb input images, and in both those cases a message like "adaptthresh currently only supports 2d image inputs" would be very useful.


  • (optional) I would suggest to have a (very simple) test case for each of all the possible data types of the input image (single, double, uint8/16/32, int8/16/32).
Hartmut <hardy>
Fri 30 May 2025 07:40:09 AM UTC, comment #11: 

Pushed:
https://sourcefor ... ff32d7c2a2a6fcdc8

This fix will be part of version 2.18.0 of the Image package.

Avinoam Kalma <avinoam>
Group Member
Mon 07 Apr 2025 08:45:22 PM UTC, comment #10: 

Attached an updated version of adaptthresh.m and a new function imbinarize.m.

Please review

(file #57113, file #57114)

Avinoam Kalma <avinoam>
Group Member
Wed 19 Mar 2025 05:52:49 AM UTC, comment #9: 

Please review: new function adaptthresh.m
(which will be used by imbinarize)

(file #57039)

Avinoam Kalma <avinoam>
Group Member
Mon 09 Sep 2024 08:24:13 PM UTC, comment #8: 

Yes, I'd also love to see an imbinarize function in the image package. But I haven't found the time to implement it myself in the last years, yet. And yes, please keep im2bw as well.

Hartmut <hardy>
Sat 31 Aug 2024 12:31:17 PM UTC, comment #7: 

Thanks. Of course

Avinoam Kalma <avinoam>
Group Member
Sat 31 Aug 2024 10:05:41 AM UTC, comment #6: 

I don't have anything to add. But when you add imbinarize please don't remove im2bw.

Carnë Draug <carandraug>
Group Member
Sat 31 Aug 2024 09:35:18 AM UTC, comment #5: 

Adding stakeholders

Avinoam Kalma <avinoam>
Group Member
Sat 31 Aug 2024 09:34:25 AM UTC, comment #4: 

imbinarize is a very useful function, and it is my to-do list for a long time.

I will write it (don't hold your breath, it will take some time)

Avinoam Kalma <avinoam>
Group Member
Sat 31 Aug 2024 07:13:16 AM UTC, comment #3: 


imbinarize() function has another restriction: the im has to be a grayscale image matrix, so the workaround is:

> function bw = imbinarize (im)
>   if (!isgray (im))
>       error('im should be grayscale image')
>   endif
>   threshold = graythresh (im);
>   bw = im2bw (im, threshold);
> endfunction

Yu Hongbo <yuhongbo>
Fri 30 Aug 2024 02:38:04 PM UTC, comment #2: 

e.g., does the following function produce the simplest expected output?


function bw = imbinarize (im)
  threshold = graythresh (im);
  bw = im2bw (im, threshold);
endfunction

Nicholas Jankowski <nrjank>
Group Member
Fri 30 Aug 2024 02:25:09 PM UTC, comment #1: 

the matlab help for im2bw suggests that


thresh = graythresh(I);
BW = im2bw(I,thresh);


is a workaround to emulate:

BW = imbinarize(I);


until a compatible imbinarize function is implemented.  Are there other imbinarize features you need immediately? it may be that other workarounds, or possibly even a placeholder wrapper function, could be implemented for those features.

Nicholas Jankowski <nrjank>
Group Member
Fri 30 Aug 2024 02:02:14 PM UTC, original submission:  

Matlab added imbinarize() function for replacement of im2bw() function, and im2bw() function is deprecated now.
In #66107, what I really need is imbinarize() function.

Yu Hongbo <yuhongbo>

 

Attached Files

Attached Files
file #57524:  imbinarize_v1.patch added by avinoam (9.0KiB - application/octet-stream)
file #57519:  imbinarize.patch added by avinoam (8.2KiB - application/octet-stream)
file #57518:  adaptthresh_v1.patch added by avinoam (11KiB - application/octet-stream)
file #57517:  adaptthresh.patch added by avinoam (11KiB - application/octet-stream)
file #57113:  adaptthresh_V1.m added by avinoam (11KiB - application/octet-stream)
file #57114:  imbinarize_V0.m added by avinoam (8.6KiB - application/octet-stream)
file #57039:  ‏‏adaptthresh_V0.m added by avinoam (11KiB - application/octet-stream)

(Note: upload size limit is set to 16MiB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

 

Dependencies

This item does not depend on any other items.

No items depend on this one.

 

Mail Notification Carbon-Copy List

Carbon-Copy List
  • -email is unavailable- added by hardy (Posted a comment)
  • -email is unavailable- added by avinoam
  • -email is unavailable- added by avinoam
  • -email is unavailable- added by avinoam (Posted a comment)
  • -email is unavailable- added by nrjank (Posted a comment)
  • -email is unavailable- added by yuhongbo (Submitted the item)
  •  

    Votes

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    History

    Follow 22 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2025-08-13 avinoam StatusPatch Submitted Fixed
        Open/ClosedOpen Closed
    2025-08-13 avinoam Attached File- Added imbinarize_v1.patch, #57524
    2025-08-11 avinoam Attached File- Added imbinarize.patch, #57519
    2025-08-10 avinoam Attached File- Added adaptthresh_v1.patch, #57518
    2025-08-10 avinoam StatusIn Progress Patch Submitted
    2025-08-10 avinoam Attached File- Added adaptthresh.patch, #57517
    2025-08-03 avinoam StatusReady For Test In Progress
    2025-05-30 nrjank StatusPatch Submitted Ready For Test
    2025-05-13 avinoam StatusIn Progress Patch Submitted
    2025-04-07 avinoam Attached File- Added adaptthresh_V1.m, #57113
        Attached File- Added imbinarize_V0.m, #57114
    2025-03-19 avinoam StatusConfirmed In Progress
    2025-03-19 avinoam Attached File- Added ‏‏adaptthresh_V0.m, #57039
    2024-08-31 avinoam Carbon-Copy- Added hardy
        Carbon-Copy- Added carandraug
    2024-08-31 avinoam StatusNone Confirmed
        Assigned toNone avinoam
    2024-08-30 nrjank CategoryOctave Function Octave Package
        Severity3 - Normal 1 - Wish
        Priority5 - Normal 3 - Low
        Releasedev other

    Back to the top

    Powered by Savane 3.16.
    Corresponding source code