MATLAB: How to crop the red region in an image

crop a red region in an imageImage Processing Toolboximage segmentationmammogram

I have a mammogram image and in it a red contour, the rgb channels across this contour are changed whereas they are constant all over the other regions. I want to do a for loop to search about this region in the image and crop it. Can anyone help?

Best Answer

I'm guessing that it's a mammogram that's had computer graphics (red pixels) burned into the image. To find the pure red pixels, you need to find where the red signal is 255 or 65535, depending on whether it's uint8 or uint16, and the green and blue signals are 0. See this snippet:
% Find pure red pixels, pure green pixels, and pure blue pixels.
maxValue = intmax(class(rgbImage))
pureRedPixels = (redChannel == maxValue) & (greenChannel == 0) & (blueChannel == 0);
pureGreenPixels = (redChannel == 0) & (greenChannel == maxValue) & (blueChannel == 0);
pureBluePixels = (redChannel == 0) & (greenChannel == 0) & (blueChannel == maxValue);
Use the one for red pixels and then call imshow() to see them. If you don't see anything then use
pureRedPixels = (redChannel <= 0.8*maxValue) & (greenChannel == 0) & (blueChannel == 0);
Keep lowering the 0.8 until you start to see something. I'm not sure exactly what color your red is. The initial code assumes red is (255,0,0) or (65535,0,0).