如何裁剪和缩放图像相对于另一个?

时间:2015-01-15 18:20:09

标签: matlab image-processing resize scaling matlab-cvst

我有两张图片:手动分割(红线)和自动分割(蓝线)。

我想做的是

  1. 重叠它们,使第一个中的点对应于第二个中的点。
  2. 裁剪点的图像(自动分割)具有相同的尺寸和相同的比例,以便我可以通过比较两个轮廓来验证分割。
  3. 虽然我使用下面的代码尝试了MATHWORKS registering-an-image-using-normalized-cross-correlation的类似解决方案,但是我收到了错误。

    我做错了什么?为什么xbeginoffset为负?

     % 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')
    

1 个答案:

答案 0 :(得分:1)

这个答案可能不完整,但根据您已经包含的信息量,我可以分享一下:

  1. 了解您尝试注册的两个图像会出现什么样的失真会很有趣。我看到你已经使用了相似性'作为transformation type。还有其他一些转换类型可能更适合您的图像,尽管您选择了足够的,准确定位的控制点,但错误的结果并不能给您带来好的结果。
  2. MATLAB documentation列出了注册两个图像的其他方式,并根据图像中的要素类型,选择其中一个选项:即基于强度(使用imregister)或基于特征(使用一组计算机视觉系统工具箱中的功能可能更准确。
  3. 要将图像裁剪为相同尺寸,您可以使用imshowpair显示它们,然后使用imcrop达到您想要的尺寸。 imshowpair显示已注册对的所有像素,因此最终结果始终是显示内容的子组件。