我想在Matlab中用RGB像素匹配这两个图像..我猜我的代码还可以..但它不起作用 [英] I want to match this two image by RGB pixel in Matlab..I guess my code is ok..but it's not working
问题描述
在这里,我使用的是主图像"cricketteam.jpg",并裁剪了一部分名为"musfiq.jpg"的图像.但是从我的代码来看,即使它们相同,我什至无法匹配单个像素.有什么问题吗?
Here I am using main image ''cricketteam.jpg' and cropped a portion of it named 'musfiq.jpg'. But from my code, I can't even match a single pixel though they are same. What is the problem?
这是我的代码:
function imageMatch
i1=imread('cricketteam.jpg');
[y,x,d]=size(i1);
m=imread('musfiq.jpg');
[y1,x1,d]=size(m);
x2=1;
y2=1;
row_match=0;
flag_y=0;
cx=1;
xc=1;
xc1=1;
s_loop=x-x1;
s_loop= s_loop+1;
for i=1:y
cx=xc;
for j=cx:x
if j==s_loop && row_match==0
break;
end
f=impixel(m,x2,y2);
ma=impixel(i1,j,i);
end
if isequal(f,ma)==1
if row_match == 0
xc=cx;
end
row_match=row_match+1;
x2=x2+1;
else
flag_y=0;
row_match=0;
y2=1;
x2=1;
end
if row_match==x1
flag_y=flag_y+1;
row_match=0;
y2=y2+1;
x2=1;
break;
end
end
if flag_y==y1
disp('Image Matched')
break;
end
end
end
这里是 cricketteam.jpg :
这是 musfiq.jpg 的部分:
推荐答案
解决方案
Jpeg图像包含jpeg压缩,因此您要查找的补丁不会在图像中原样显示. 相反,您可以查找最大化归一化互相关( NCC). NCC是一种评估两个补丁之间相似度的方法. 为此,请使用MATLAB的 normxcorr2 函数.
Jpeg images include jpeg compression, and therefore the patch which you are looking for is not going to appear as-is in the image. Instead, you can look for the location of the patch which maximizes the normalized cross correlation (NCC) between the images. The NCC is a measure which evaluates the level of similarity between two patches. In order to do so, use MATLAB's normxcorr2 function.
代码
%reads images
I = rgb2gray(imread('team.jpg'));
template = rgb2gray(imread('p.jpg'));
[h,w]=size(template);
%calculate the ncc at each pixel
c = normxcorr2(template,I);
%finds the maximal ncc pixel
[y, x] = find(c==max(c(:)));
%extracts the relevant patch
yoffSet = ypeak-size(template,1);
xoffSet = xpeak-size(template,2);
patch = I(y-h+1:y,x-w+1:x);
结果
数字结果:
C(y,x) = 0.9992 %ncc result (complete match is 1)
[y, x]= 84 342 %most suitable patch center
图像输出:
%display the original image with the found patch
hFig = figure;
hAx = axes;
imshow(I,'Parent', hAx);
imrect(hAx, [xoffSet+1, yoffSet+1, size(template,2), size(template,1)]);
这篇关于我想在Matlab中用RGB像素匹配这两个图像..我猜我的代码还可以..但它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!