How to compare an image with another image?
Show older comments
I want to determine the degree of mixing of 2 different colored beads. Below image represents the ideal mixing case.

I want to use the above ideal image as a standard and use it to compare other images (like the one below) to determine how well the beads are mixed.

Please let me know how it can be done? Is there any factor/variable I can assign for the ideal case and compare it with the non-deal cases?
Thank You.
Accepted Answer
More Answers (1)
Image Analyst
on 2 Dec 2018
ALright, looks like you must be having trouble. So I made this code to get you started.
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 = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'beads.jpg';
folder = pwd;
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 image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original Color Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% 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;
% Get the blue mask.
[blueMask, maskedRGBImage] = createMask(rgbImage);
% Display the image.
subplot(2, 2, 2);
imshow(blueMask);
caption = sprintf('Initial Blue Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis('on', 'image');
% Need to get rid of "holes" caused by not using crossed polarizing filters.
binaryImage = imclearborder(~blueMask);
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage);
caption = sprintf('Blue Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis('on', 'image');
% % Compute the sizes of the holes.
% props = regionprops(binaryImage, 'Area');
% allAreas = sort([props.Area], 'descend')
% subplot(2, 2, 4);
% histogram(allAreas);
% grid on;
% title('Histogram of "holes"');
% Get small blobs smaller than 1000 pixels.
binaryImage = bwareafilt(binaryImage, [0, 1000]);
% Use that to fill in "holes" in the blue mask.
blueMask = blueMask | binaryImage;
% Compute the area fraction of blue.
blueAreaFraction = sum(blueMask(:)) / numel(blueMask)
% Display the image.
subplot(2, 2, 3:4);
imshow(blueMask);
caption = sprintf('Final Blue Mask Image, Blue Area Fraction = %.3f', blueAreaFraction);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis('on', 'image');
% Tell user via a popup message.
uiwait(helpdlg(caption));
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 02-Dec-2018
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.529;
channel1Max = 0.652;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.559;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.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;
end

Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!