如何使用规范化关联查找多个匹配模式

时间:2013-05-19 15:29:07

标签: image matlab image-processing correlation normalize

我正在使用normxcorr2找到与我的模式完全匹配的区域,我还想找到看起来像模式的其他区域(在红色矩形中)。我认为如果我能找到下一个最大值,那么它将起作用,并且该值不能在第一个最大区域或第一个最大区域中,但我不能这样做。或者,如果您有任何想法使用normxcorr2找到其他区域,请告诉我,我根本不知道。
这是我的代码。我修改了这个http://www.mathworks.com/products/demos/image/cross_correlation/imreg.html

onion = imread('pattern103.jpg'); %pattern image
peppers = imread('rsz_1jib-159.jpg'); %Original image

onion = rgb2gray(onion);
peppers = rgb2gray(peppers);

%imshow(onion)
%figure, imshow(peppers)

c = normxcorr2(onion,peppers);
figure, surf(c), shading flat

% offset found by correlation
[max_c, imax] = max(abs(c(:)));
[ypeak, xpeak] = ind2sub(size(c),imax(1));
corr_offset = [(xpeak-size(onion,2))
               (size(onion,1)-ypeak)]; %size of window show of max value 

offset = corr_offset;
xoffset = offset(1);
yoffset = offset(2);

xbegin = round(xoffset+1); fprintf(['xbegin = ',num2str(xbegin)]);fprintf('\n');
xend   = round(xoffset+ size(onion,2));fprintf(['xend = ',num2str(xbegin)]);fprintf('\n');
ybegin = round(yoffset+1);fprintf(['ybegin = ',num2str(ybegin)]);fprintf('\n');
yend   = round(yoffset+size(onion,1));fprintf(['yend = ',num2str(yend)]);fprintf('\n');

% extract region from peppers and compare to onion
extracted_onion = peppers(ybegin:yend,xbegin:xend,:);
if isequal(onion,extracted_onion)
   disp('pattern103.jpg was extracted from rsz_org103.jpg')
end

recovered_onion = uint8(zeros(size(peppers)));
recovered_onion(ybegin:yend,xbegin:xend,:) = onion;
figure, imshow(recovered_onion)

[m,n,p] = size(peppers);
mask = ones(m,n);
i = find(recovered_onion(:,:,1)==0);
mask(i) = .2; % try experimenting with different levels of
              % transparency

% overlay images with transparency
figure, imshow(peppers(:,:,1)) % show only red plane of peppers
hold on
h = imshow(recovered_onion); % overlay recovered_onion
set(h,'AlphaData',mask)

Extracted Area

Output

Pattern

Originalimage

0 个答案:

没有答案