【发布时间】:2017-04-09 16:45:08
【问题描述】:
我正在开发一个应用程序的一部分,我现在需要将两张图像一起比较并检查它们之间的相似程度。目前这是通过将两个图像转换为二进制来完成的,计算黑白像素的数量,然后最终将白色像素的数量除以图像中存在的像素总数。
我现在面临的问题是我正在使用的图像的背景,因为它计入“相似度分数”并使其成为不准确的结果。我对如何解决这个问题有一个想法,但我不知道如何付诸实践。
例如,假设这是图像文件,它是像素值。
0000000000
0000110000
0001001000
0011001100
0001001000
0000110000
0000000000
我的想法是从左到右,从右到左,从上到下,从下到上搜索每一行和每一列,以检测图像背景中包含的黑色像素的数量。然后希望一旦它检测到一个白色像素,它就会停止搜索该行/列并移动到下一个。最后,当搜索完成时,我会知道背景占用了多少黑色像素,然后我可以从总计中减去该值,这将使我获得更准确的读数。
我只是不知道如何以这种方式进行搜索,并且我不想消除屏幕上的每个黑色像素,因为它们很重要,因为它们也包含在这些对象中(下面的示例)。有谁知道我会怎么做,或者知道更简单的方法吗?
当前代码:
for i = 1:length(jpgFiles)
baseFileName = jpgFiles(i).name;
fullFileName = fullfile(referenceFolder, baseFileName);
%Reading in the images & converting the images to Black & White
a = im2bw(imread(firstImage));
b = im2bw(imread(fullFileName));
compiledImage = a==b;
fprintf(1, 'Reading %s\n', fullFileName);
axes(handles.axes4);
disp(compiledImage); %Displays the values if the pixels match
[X, Y] = find(a ~= b); %Find coordinates for which the two images are equal
imshow(a); %Show first image
hold on %Retains the current plot and certain axes properties
plot(Y, X, 'y.'); %Overlay those coordinates
hold off %Resets axes properties to their default before drawing new plots
%Getting the values of white and black pixels
blackCount = sum(sum(a==b ==0));
whiteCount = sum(sum(a==b));
disp('Black Count:');
disp(blackCount);
disp('White Count:');
disp(whiteCount);
%Calculating the percentage
maxTotal = blackCount + whiteCount;
decimalValue = whiteCount / maxTotal;
percentageValue = sprintf('%.0f%%',100*decimalValue);
disp(decimalValue);
disp(percentageValue);
%Title settings and delay, if needed
title('Strongest Features (Yellow Hides Any Discrepancies)', 'fontweight', 'bold'); %Sets the title
drawnow;
%pause(5);
%Adding into the image array
imageArray{j} = baseFileName;
j = j + 1;
%Adding into the popular features array
popFeaturesArray{i} = 100*decimalValue;
i = i + 1;
end
图片示例:
【问题讨论】:
标签: matlab