我有兩個圖像:手動分割(紅線)和自動分割(藍線)。如何裁剪和縮放與另一個圖像相關的圖像?
我想要做的是
- 重疊它們,以便在第一對應於第二點的點。
- 裁剪圖像(自動分割)以使點具有相同的尺寸和相同的比例尺,以便我可以通過比較兩個輪廓來驗證分割。
雖然我試圖從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')
示例圖像將幫助(如果你沒有足夠的代表,將它們上傳到第三方網站,有人會在編輯爲你)。還解釋你的意思是「不一致」,你選擇哪個/多少個控制點會有所幫助。 – nkjt 2015-01-15 18:45:16
好吧,我的圖像沒有失真。它們的規模不同,其中一個比另一個更加剪裁。有圖片的鏈接:https://www.dropbox.com/s/c5lfr0n8z881jk6/Bif11_automated.png?dl=0 https://www.dropbox.com/s/e7emf9alo7un98v/Bif11_manual.png?dl=0你認爲我應該選擇多少點? – 2015-01-16 11:54:50
我已經把鏈接放到了圖片上。這些圖像沒有失真,它們處於不同的尺度。我只是一個將它們轉換爲相同的比例,以比較同一座標系中的兩個計數。我嘗試了所有的Matlab中的圖像註冊的例子,但沒有成功,Doe sanyone有一個消解? – 2015-01-20 11:42:10