Clear Filters
Clear Filters

Cropping Abnormal Image for Multiple Laboratory Trials

2 views (last 30 days)
Working on a porject that involved cropping a pad image. I can't get the crop just right -- I want the entire pad cropped out of the backround and for it to work for multiple trials and different images -- not just this image. I uploaded 3 samples photos. Thank you!

Answers (2)

Shubham on 23 Feb 2024
Hi Lauren,
It seems that you are trying to extract the pad and remove the background of the image.
Although it is not possible to crop the pad out of the image, but you can define a region of interest (ROI)” for the same. Upon checking the three images, I am assuming that the position of camera and the relevant objects are not moved in between. In such a scenario, you can create a “ROI” for one image and use it for others. For creating a customizable “ROI”, please refer to the drawpolyon” function:
Another approach you could perform would be to remove the background through image segmentation with the help of masking. You can leverage the “Color Thresholder” to create the mask for the image. Have a look at the following documentation:
I have generated the mask using the above method and tested it out on the provided images. You can refer to the following code snippet and output:
image = imread('0ml.png');
[bw,mi] = createMask(image);
function [BW,maskedRGBImage] = createMask(RGB)
% Convert RGB image to chosen color space
I = rgb2ycbcr(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 27.000;
channel1Max = 233.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 98.000;
channel2Max = 129.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 128.000;
channel3Max = 210.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;
You can use the generated code for masking all the images. You can also crop out the image on the edges using a script. You can use the “imcrop” function for the same:
You can also refer to this post available on MathWorks Blogs which goes through the whole process of background removal:
You may also find the below MATLAB Answers relevant to your query:
I hope this helps!

Image Analyst
Image Analyst on 27 Feb 2024
You can certainly crop out the pad's bounding box to a new image if you need to. Not sure you need to though.
I've worked on exactly this kind of image for over 30 years. I never needed to crop the image though I did classify the image into stain, pad, and background. Then you can measure all kinds of things about the classes.
If you still need help, let me know. If you let me know what city you're working in, I might be able to help substantially more.

Community Treasure Hunt

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

Start Hunting!