我有两张图片:手动分割(红线)和自动分割(蓝线)。
我想做的是
虽然我使用下面的代码尝试了MATHWORKS registering-an-image-using-normalized-cross-correlation的类似解决方案,但是我收到了错误。
我做错了什么?为什么xbegin
和offset
为负?
% Algorithm for image validation
% Open the two images which will be compared
name2=input('Image name ( automated segmentation) ','s');
img_automated=imread(name2,'png');
figure (1), imshow(img_automated), title('Image automated')
name=input('Image name ( manual segmentation) ','s');
img_manual=imread(name,'png');
img_manual_gray=rgb2gray(img_manual);
figure (2), imshow (img_manual),title('Image manual')
img_automated_gray=rgb2gray(img_automated);
%img_double=im2double(img_automated_gray);
figure (3), imshow (img_automated_gray), title (' Image converted to double ');
imcontrast
%uiwait(img_automated_gray)
img_automated_eq=adapthisteq(img_automated_gray);
figure (5), imshow (img_automated_eq), title (' Image after histogram equalization ');
img_automated_gray=rgb2gray(img_automated);
figure (6), imshowpair(img_manual,img_automated_eq)
title('Images overlap')
%Step 2: Choose Subregions of Each Image
%It is important to choose regions that are similar.The image sub_automated
%will be the template, and must be smaller than the image sub_manual.
% interactively
[sub_manual,rect_manual] = imcrop(img_manual); % choose the pepper below the onion
[sub_automated,rect_automated] = imcrop(img_automated_gray); % choose the whole onion
% display sub images
figure(8), imshow(sub_automated)
figure(9), imshow(sub_automated)
%Step 3: Do Normalized Cross-Correlation and Find Coordinates of Peak
%Calculate the normalized cross-correlation and display it as a surface plot.
% The peak of the cross-correlation matrix occurs where the sub_images are
% best correlated. normxcorr2 only works on grayscale images, so we pass it
% the red plane of each sub image.
c = normxcorr2(sub_automated(:,:,1),sub_manual(:,:,1));
figure (10), surf(c), shading flat
%Step 4: Find the Total Offset Between the Images
%The total offset or translation between images depends on the location
%of the peak in the cross-correlation matrix, and on the size and position
%of the sub images.
% offset found by correlation
[max_c, imax] = max(abs(c(:)));
[ypeak, xpeak] = ind2sub(size(c),imax(1));
corr_offset = [(xpeak-size(sub_automated,2))
(ypeak-size(sub_automated,1))];
% relative offset of position of subimages
rect_offset = [(rect_manual(1)-rect_automated(1))
(rect_manual(2)-rect_automated(2))];
% total offset
offset = corr_offset + rect_offset;
xoffset = offset(1);
yoffset = offset(2);
%Step 5: See if the Onion Image was Extracted from the Peppers Image
%Figure out where onion falls inside of peppers.
xbegin = round(xoffset+1);
xend = round(xoffset+ size(img_automated_gray,2));
ybegin = round(yoffset+1);
yend = round(yoffset+size(img_automated_gray,1));
% extract region from peppers and compare to onion
extracted_automated =img_manual(ybegin:yend,xbegin:xend,:);
if isequal(img_automated_gray,extracted_automated)
disp('extracted_automated.png was extracted from img_automated.png')
end
%Step 6: Pad the Onion Image to the Size of the Peppers Image
%Pad the automated image to overlay on manual, using the offset determined above.
recovered_automated = uint8(zeros(size(img_manual)));
recovered_onion(ybegin:yend,xbegin:xend,:) = img_automated_gray;
figure(11), imshow(recovered_automated)
figure (12), imshowpair(img_manual(:,:,1),recovered_automated,'blend')
答案 0 :(得分:1)
这个答案可能不完整,但根据您已经包含的信息量,我可以分享一下: