How can I segment images that contain objects with very similar colours?

14 ビュー (過去 30 日間)
Ahmed
Ahmed 2023 年 2 月 26 日
回答済み: Abhijeet 2023 年 3 月 7 日
Hello Matlab community,
I want to segment the following image by detecting the pellets (nearly spherical particles) and sinter (irregularly shaped particles). The challenge is their similarity in colour making the colour segmentation very difficult. Is there any solution to accomplish this?

回答 (2 件)

Harsh
Harsh 2023 年 3 月 7 日
Hi Ahmed,
I understand that you are trying to segment two objects with similar colors.
I think you can achieve the same by performing simple colour thresholding in L*a*b* color space.
The L*a*b* color space is actually intended to quatify the perceived colors. Therefore, it serves useful in detecting small differences in color, which is exactly the application you are looking for.
In the following lines, please find the code and steps to generate the appropriate image segmentation as per your query.
  • Save the following code as a MATLAB script named "createMask.m". This is auto-generated code from MATLAB's color thresholder app.
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 07-Mar-2023
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2lab(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 60.069;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -8.530;
channel2Max = -0.257;
% Define thresholds for channel 3 based on histogram settings
channel3Min = -2.229;
channel3Max = 5.428;
% 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;
end
  • Then execute the following commands in MATLAB by substituting the <ImageName> with the image name that you want to segment:
rgb = imread('<ImageName>');
createMask(rgb)
[a,b]=createMask(rgb);
% a is binarized output of the segmentation.
% b is the segmented RGB Image.
imshow(a)
  • The following are the outputs that I am able to achieve using the above code on the sample image that you have shared. First image shows the binarized output and second image is the segmented RGB Image.

Abhijeet
Abhijeet 2023 年 3 月 7 日
Hi,
A standard way to segment images with very similar color objects include adjusting the contrast in grayscaled image. Steps are explained below :
  1. Convert the image to grayscale using the rgb2gray() function.
  2. Enhance the contrast of the image using the imadjust() function.
  3. Apply a median filter to remove noise using the medfilt2() function.
  4. Apply a threshold to separate the pellets and sinter using the imbinarize() function.
  5. Remove small objects from the binary image using the bwareaopen() function.
  6. Perform a morphological opening to separate the particles using the strel() and imopen() functions.
  7. Perform a connected component analysis to identify the pellets and sinter using the bwlabel() function.
  8. Calculate the properties of the particles using the regionprops() function.
Sample code for the ablove steps is attached below :
gray_img = rgb2gray(img);
contrast_img = imadjust(gray_img);
median_img = medfilt2(contrast_img);
thresh = graythresh(median_img);
bw_img = imbinarize(median_img, thresh);
bw_img = bwareaopen(bw_img, 50);
se = strel('disk', 3);
opened_img = imopen(bw_img, se);
[labels, num_labels] = bwlabel(opened_img);
particle_props = regionprops(labels, 'Area', 'Eccentricity', 'Perimeter', 'Solidity');
Classify the particles based on their properties. For example, you can classify particles with low eccentricity and high solidity as pellets, and particles with high eccentricity and low solidity as sinter.

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by