Differentiate smooth and rough objects

10 views (last 30 days)
Nataliya
Nataliya on 28 Oct 2017
Answered: Image Analyst on 16 Dec 2017
I am trying to differentiate smooth object (as shown in left) and rough object (as shown in right). I tried solidity property from regionprops() but it didn't work. Any suggestions?

Answers (3)

Selva Karna
Selva Karna on 28 Oct 2017
you can use image subtraction

Image Analyst
Image Analyst on 29 Oct 2017
Why did solidity not work? Please attach individual images and your code so I can fix it. Circularity might be a better measure of outline smoothness than solidity - have to try it and see.
  3 Comments
Nataliya
Nataliya on 16 Dec 2017
I am not able to binarize it accurately. I could not find the threshold value that will binarize the image. Is there any other way or function that will work on color image to distinguish smooth and rough objects?

Sign in to comment.


Image Analyst
Image Analyst on 16 Dec 2017
Try this code:
function testRGBImage()
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%===============================================================================
% Get the name of the first image the user wants to use.
baseFileName = 'frame_3.jpg';
folder = fileparts(which(baseFileName)); % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 3, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get mask
[mask,maskedRGBImage, labImage] = createMask(rgbImage);
% Get rid of blobs in right half of the image.
mask(:, round(columns/2):end) = false;
% Take the largest blob and fill holes in it.
mask = imfill(bwareafilt(mask, 1), 'holes');
% Display the mask image.
subplot(2, 3, 2);
imshow(mask, []);
axis on;
caption = sprintf('Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Display the masked RGB image.
subplot(2, 3, 3);
imshow(maskedRgbImage);
axis on;
caption = sprintf('Masked Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Get the average LAB value of the image in the mask area.
propsL = regionprops(mask, labImage(:,:,1), 'MeanIntensity');
propsA = regionprops(mask, labImage(:,:,2), 'MeanIntensity');
propsB = regionprops(mask, labImage(:,:,3), 'MeanIntensity');
meanL = propsL.MeanIntensity;
meanA = propsA.MeanIntensity;
meanB = propsB.MeanIntensity;
% Create a color difference image with delta E values.
deltaEImage = sqrt((labImage(:,:,1) - meanL) .^2 + (labImage(:,:,2) - meanA) .^2 + (labImage(:,:,3) - meanB) .^2)
% Display the delta E image.
subplot(2, 3, 4);
imshow(deltaEImage, []);
axis on;
caption = sprintf('Color difference Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Get areas, standard deviation, and perimeter of pixel values of the color difference image.
props = regionprops(mask, deltaEImage, 'Area', 'Perimeter', 'PixelValues', 'MeanIntensity');
area = props.Area
stdDev = std(props.PixelValues)
perimeter = props.Perimeter
meanColorDifference = props.MeanIntensity
% Compute circularity
circularity = (4*pi*area) / perimeter^2
% Display the histogram of areas image.
subplot(2, 3, 5);
histogram(props.PixelValues);
grid on;
caption = sprintf('Histogram of Color Differences');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
end
function [BW, maskedRGBImage, labImage] = 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 16-Dec-2017
%------------------------------------------------------
% Convert RGB image to chosen color space
labImage = rgb2lab(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 14.963;
channel1Max = 100.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -6.620;
channel2Max = 72.590;
% Define thresholds for channel 3 based on histogram settings
channel3Min = -15.883;
channel3Max = 58.775;
% Create mask based on chosen histogram thresholds
sliderBW = (labImage(:,:,1) >= channel1Min ) & (labImage(:,:,1) <= channel1Max) & ...
(labImage(:,:,2) >= channel2Min ) & (labImage(:,:,2) <= channel2Max) & ...
(labImage(:,:,3) >= channel3Min ) & (labImage(:,:,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
But you really need to improve your starting image. You need to get rid of the glare and specular reflections. Try using crossed polarizers and changing the location of your lighting.

Community Treasure Hunt

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

Start Hunting!