Cropping an irregular image in matlab

18 views (last 30 days)
Lauren Smith on 10 Nov 2023
Answered: William Rose on 10 Nov 2023
Hi! I am working on a project which involves finding color saturation on a mentrual pad. I have the pad image file and I want to eliminate the background area so that the rest of my code is only reading the colors from the inside of the pad, not the outside. How would I do this? I was looking at the imcrop feature but it seems to only crop rectangles, and I have seen other tutorials for polygon shapes -- just not an irregular shape. Any help would be appreciated! Thanks!

Cris LaPierre on 10 Nov 2023
Edited: Cris LaPierre on 10 Nov 2023
I think what you are describing is called a mask. If your analysis uses color-based criteria, then I suggest using the Color Threshholder App.
Doing that and playing around with some of the color spaces, I was able to obatin the following result (code autogenerated from the app).
% Convert RGB image to chosen color space
I = rgb2ycbcr(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 255.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 140.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 255.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
% Set background pixels where BW is false to zero.
Lauren Smith on 10 Nov 2023
thank you! I will look into this!

William Rose on 10 Nov 2023
@Lauren Smith, it appears to me that a rectangular crop would work for this image. A rectangle can be selected that includes nothing but pad. (Some of the pad will be unstained.) If that is not OK, you could make a matlab script in which the user selects a polygon. That would hard for me, so if it were my project I would use an external iimage editor program to select the non-rectangular region of interst and save it. YOu will end up with a background that will be white or black or whatever color you select. Two examples are attached. I did one with the polygon selector tool in a program called SNagIt, which I use for screen captures and simple image editing. I did the other with the Paint program that comes with windows. In that program, you use your mouse to draw an irregular curve around the region of interest. There is also a "Magic Select" feature in Windows Paint3D which you might like: You select a rectangle of interest, then the program selects a tight subset within that rectangle which looks "interesting". Then when you save the image, there is an option to select "with transparency", which I did select. This causes the "magic select" region, which has an irregular shape, to be saved with a transparent background. I tried it on your image and got the result attached. See attached files.
Once you have one of these file,s you can import to matlab and view the histogram of the pixel saturations. I will demonstrate with the paint3d file, and you can try it with the others and decide what works for you.
B=rgb2hsv(A);
histogram(B(:,:,2))
The histogram plot shows the saturations within the image. The big bar at the left is due to the transparent background. Results are a bit different for the other two files because they include more of the unstained pad in the image.

Categories

Find more on Region and Image Properties in Help Center and File Exchange

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!