What is the mistale in the loop?

4 views (last 30 days)
I want to match the similarity of an image with all images of a folder. I compare the histogram error between to images using loop. But I've done some mistakes to code the loop. So, my program doesn't answer anything. The code is:
a = imread('c.jpg'); %read an image
srcFiles = dir('similar\*.jpg'); %directory of the source folder
Im1=rgb2gray(a); % convert images to type double (range from from 0 to 1 instead of from 0 to 255)
Im1 = im2double(Im1); % Calculate the Normalized Histogram
hn1 = imhist(Im1)./numel(Im1);
for i = 1 : length(srcFiles) % determins how many images are there in the folder,
filename = strcat('similar\',srcFiles(i).name);
Im2=rgb2gray(filename);
Im2 = im2double(Im2);
hn2 = imhist(Im2)./numel(Im2);
% Calculate the histogram error
f(i) = sum((hn1 - hn2).^2);% Calculate the histogram error
f %display the result to console
end
Can anyone please help me to correct the mistakes???

Accepted Answer

Image Analyst
Image Analyst on 16 Mar 2014
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
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
referenceGrayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(referenceGrayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
referenceGrayImage = referenceGrayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(referenceGrayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(referenceGrayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
srcFiles = dir(fullfile(folder, '*.jpg')); %directory of the source folder
Im1 = im2double(referenceGrayImage); % Calculate the Normalized Histogram
hn1 = imhist(referenceGrayImage)./numel(referenceGrayImage);
for i = 1 : length(srcFiles) % determins how many images are there in the folder,
filename = fullfile(folder,srcFiles(i).name)
thisImage = imread(filename);
% Display the original gray scale image.
subplot(2, 2, 3);
imshow(thisImage);
title(filename, 'FontSize', fontSize);
drawnow;
Im2=rgb2gray(thisImage);
Im2 = im2double(Im2);
hn2 = imhist(Im2)./numel(Im2);
% Calculate the histogram error
f(i) = sum((hn1 - hn2).^2);% Calculate the histogram error
f(i) %display the result to console
promptMessage = sprintf('f=%f\nDo you want to Continue processing,\nor Cancel to abort processing?',f(i));
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end
  1 Comment
Shaila parvin
Shaila parvin on 16 Mar 2014
Thanks a lot. It works 100%.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 16 Mar 2014
What is length(srcFiles)? It sounds like it's not even entering the loop since you said it doesn't do anything at all. Did you step through the code? http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/

Community Treasure Hunt

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

Start Hunting!

Translated by