Cropping an irregular image in matlab
12 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(2 个)
Cris LaPierre
2023-11-10
编辑:Cris LaPierre
2023-11-10
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).
RGB = imread('padimage.jpg');
% 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.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
imshow(maskedRGBImage)
You can walk through an example of how to use the app here: https://www.mathworks.com/help/images/image-segmentation-using-the-color-thesholder-app.html
William Rose
2023-11-10
@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.
A=imread('padimage_paint3d.png');
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.
Good luck with your work.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!