compare one image with an aray of images
1 回表示 (過去 30 日間)
古いコメントを表示
Jishnoop JAYAPRAKASH
2020 年 4 月 25 日
コメント済み: Image Analyst
2020 年 4 月 25 日
i have a set of 11 images in a folder, i have to choose an image in random and check whether the choosen image is equal to any one of the 11 images and get the output as "which image is equal to the choosen image".
myFolder = 'C:\Users\hp\Desktop\final image';
filePattern = fullfile(myFolder, '*.tif');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
Z = imread(fullFileName);
end
[a,b]=imgetfile;
I=imread(a);
for i=1:length(Z)
if (I==imread(Z{i}))
disp('YES');
else
disp('NO')
break
end
end
disp(x)
this code shows an error like this

kindly help me in resolving the issue
0 件のコメント
採用された回答
Image Analyst
2020 年 4 月 25 日
No need for a cell array int he first place. No need to read all those images into memory, which might cause you to run out of memory if you had a lot of images. Just get the random number, read in that image, then loop over all other images using isequal() to look for a match. Fixed code is below:
fprintf('Beginning to run %s.m ...\n', mfilename);
fontSize = 14;
myFolder = pwd; %'C:\Users\hp\Desktop\final image';
filePattern = fullfile(myFolder, '*.tif');
imageFiles = dir(filePattern);
numberOfImages = length(imageFiles)
randomNumber = randi(numberOfImages)
% Get that random image.
baseRefFileName = imageFiles(randomNumber).name;
refFullFileName = fullfile(myFolder, baseRefFileName);
referenceImage = imread(refFullFileName);
% Display reference image.
subplot(1, 2, 1);
imshow(referenceImage);
caption = sprintf('%s', baseRefFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
% Loop over all images.
for k = 1:length(imageFiles)
baseFileName = imageFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s to see if it matches image %d\n', fullFileName, randomNumber);
thisImage = imread(fullFileName);
% Display test image.
subplot(1, 2, 2);
imshow(thisImage);
drawnow;
if isequal(referenceImage, thisImage)
fprintf('\t"%s" matches "%s".\n', baseRefFileName, baseFileName);
caption = sprintf('Match! %d of %d : %s', k, length(imageFiles), baseRefFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
if k < length(imageFiles)
pause(2); % Delay a little bit so we can see it, unless it's the very last image.
end
else
fprintf('\t"%s" does NOT match "%s".\n', baseRefFileName, baseFileName);
caption = sprintf('No Match! %d of %d : %s', k, length(imageFiles), baseRefFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
pause(0.4); % Delay a little bit so we can see it.
end
end
fprintf('Done running %s.m .\n', mfilename);
2 件のコメント
Image Analyst
2020 年 4 月 25 日
The input image was chosen. You said to choose one at random -- " i have to choose an image in random" -- and that's what exactly what I did when I called randi(). And using isequal() does the comparison pixel by pixel. So this perfectly meets your needs. If you want to simplify it (and make it less user friendly) you could get rid of the lines of code where I display the images and titles, get rid of the calls to pause() (not needed if you're not displaying anything), and get rid of fprintf() which displays stuff in the command window. So the code will run but you basically won't see anything and won't know if it worked, unless you leave in at least one of the fprintf()'s.
その他の回答 (1 件)
David Hill
2020 年 4 月 25 日
myFolder = 'C:\Users\hp\Desktop\final image';
filePattern = fullfile(myFolder, '*.tif');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
Z{k} = imread(fullFileName);%need to store in cell array
end
[a,b]=imgetfile;
I=imread(a);
for i=1:length(Z)
if isequal(I,Z{i})%should use isequal
disp('YES');
else
disp('NO')
break
end
end
disp(x)
1 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!