How to crop and scale an image in relation to another?

I have two images: a manual segmentation (red line) and automated segmentation (blue line).

What I want to do is

  1. Overlap them so that the points in the first correspond to the points in the second.
  2. Crop the image (automated segmentation) for the points to have the same dimensions and same scale, so that I can validate the segmentation by comparing the two contours.

While I tried a similar solution from MATHWORKS, registering-an-image-using-normalized-cross-correlation, using the code below, but I got an error.

What am I doing wrong? Why is xbegin and offset negative?

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

Upvotes: 0

Views: 749

Answers (1)

akamath
akamath

Reputation: 570

This answer may not be complete, but with the amount of information you've already included, I have this to share:

  1. It would be interesting to know what kind of distortion the two images you are trying to register exhibit. I see that you've used 'similarity' as the transformation type. There are other transformation types that may be more suitable to your images, and getting this wrong will not give you a good result in spite of you selecting enough, and accurately located control points.
  2. The MATLAB documentation lists out other ways to register two images, and depending on the type of features in your images, one of the other options: namely Intensity based (using imregister) or feature based (using a set of functions from the Computer Vision System Toolbox) may be more accurate.
  3. To crop the images to be of the same size, you could display them using imshowpair as you already are, and then use imcrop to the size you intend. imshowpair displays all the pixels of the registered pair, so that your final result is always a sub-component of what is displayed.

Upvotes: 1

Related Questions