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

6 views (last 30 days)
I have two images (manual segmentation red line) and automated segmentation (blue line). What I want to is scaling the 2nd image and crop in order to the points in the 2nd are in the same coordinate system and correspond to the points in the first (manual segmentation). And then crop the image (automated segmentation) for this have the same dimensions as the first, in the same scale . I already tried to make the registration technique but the requested image does not correspond to what I want:
code below:
While I was trying the solution that was more similar of mine this is in these link http://www.mathworks.com/help/images/examples/registering-an-image-using-normalized-cross-correlation.html. So I try to do it, the code is below, but it appears these error, what am I doing wrong for the xbegin and offset was 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')

Accepted Answer

Image Analyst
Image Analyst on 19 Jan 2015
Look for posts here on registration, especially those by Alex Taylor of the Mathworks, who I think wrote the functions.
  3 Comments
Helena Henrques
Helena Henrques on 20 Jan 2015
Thanks. I did it with a simple example. But imregister just works with grayscale images and I want to have the countour (blue line) in the coordinates of the registered image,do you have some suggestions?
Image Analyst
Image Analyst on 20 Jan 2015
Just segment out the colored lines into gray scale images and then register.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!