I have grayscale images which contain a clear foreground and background, which Binarize is easily able to split out. Then within the foreground, I want to binarize again, to find the area around the high point.

Here is an example image (modified from one of the examples in the help for RemoveBackground):

enter image description here

pepper = Import["https://i.stack.imgur.com/yX1jr.png"]

Now this image has a chili pepper, which is the brightest part, a few leaves and then an almost black background. I want to select just the chili, not the leaves too.

RemoveBackground will easily remove the black background and make it transparent if I wish,

pepper2 = RemoveBackground[pepper, Black]

enter image description here

Or I can set it to be non-transparent entirely black via:

pepper3 = ImageMultiply[pepper, Binarize[pepper]]

Having done that, I can't use Binarize (or FindThreshold) again, as it treats the background as still being there.

GraphicsRow[{pepper, pepper2, pepper3}]

enter image description here

How do I find the 'best' region around the point of highest intensity? If I can clip the image sufficiently that the actual background is entirely removed, Binarize will work fine and give me a nice looking region.

My actual images look something more like this:

enter image description here

which you can see from a 3D plot has a well-defined bump, that I wish to characterise.

actual = Import["https://i.stack.imgur.com/CMbiE.png"]
ListPlot3D[ImageData@actual, ImageSize -> 800]

enter image description here

  • $\begingroup$ I don't suppose you want to just use RemoveBackground before your binarize? $\endgroup$ – Carl Lange Apr 8 at 7:59
  • $\begingroup$ @CarlLange, RemoveBackground just sets the background to have an alpha channel, which ImagePad actually does too (when the initial image has an alpha channel, as this one has gained when I uploaded it). $\endgroup$ – KraZug Apr 8 at 8:14
  • 1
    $\begingroup$ Ah, hmm. I suppose ImageCrop with no arguments would also work. $\endgroup$ – Carl Lange Apr 8 at 8:15
  • $\begingroup$ @CarlLange, my background isn't nice and uniform, so I can't just crop it out of the picture - the foreground can go towards the edge of the image in several different directions $\endgroup$ – KraZug Apr 8 at 8:17
  • $\begingroup$ @KraZug you really need to formulate your problem properly --- especially posting the right original image you are working and the result you seeking. $\endgroup$ – Vitaliy Kaurov Apr 8 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.