How to crop an image A which is a subset of an image B

2 次查看(过去 30 天)
I have two images where image A is a subset of image B.I could with feature matching technique - identify, match and show the images as a pair. Now that i have identified and matched image A with image B. i want to clip out(crop) that exact region from image B and create a new image. How can i do this, any help is appreciated ?

采纳的回答

Dima Lisin
Dima Lisin 2015-10-13
编辑:Dima Lisin 2015-10-25
If you have matched the points, then you can simply find the bounding box of the matched points:
points = round(matchedTarget.Location);
left = min(points(:, 1));
right = max(points(:, 1));
top = min(points(:, 2));
bottom = max(points(:, 2));
croppedImage = target(top:bottom, left:right, :);
  4 个评论
Dima Lisin
Dima Lisin 2015-10-25
编辑:Dima Lisin 2015-10-25
Sorry, I've fixed the answer. Use round(), and switch top:bottom and left:right.
dheeraj surabhi
dheeraj surabhi 2015-10-30
Thanks Dima. It works.
However i have a problem here. the fact that we are doing round() makes the cropped image loose a slight portion of edges as in the new image that i cropped out would have borders cut down slightly. This is happening since we are rounding the indices i guess, any idea on how can i overcome this and be able to crop the exact image?

请先登录,再进行评论。

更多回答(2 个)

Thorsten
Thorsten 2015-10-12
If I understood correctly, you have found the part in image B that is exactly like image A, and now you want to crop that part of image B. Because both are identical, the cropped image would be exactly like A, so you can just write
C = A;
  2 个评论
dheeraj surabhi
dheeraj surabhi 2015-10-12
I guess what you meant is just to copy as a new image because they are identical anyway but assume that image B is true-color and image A is Gray. So i want to crop out the actual region from image B so that i can have a new color image that is identical to image A but is cropped out from image B.
Thorsten
Thorsten 2015-10-12
C = B(ind1,ind2,:);
where ind1, ind2 are the vectors ob indices that define the region.

请先登录,再进行评论。


Image Analyst
Image Analyst 2015-10-12
You have said you "have identified and matched image A with image B". If you did that, then you know the coordinates, for example like I did in my attached normxcorr2() demo. So simply use imcrop() after that.
  2 个评论
dheeraj surabhi
dheeraj surabhi 2015-10-12
I have identified using SURF feature matching technique but i don't know how to get the coordinates of the image A with respect to the image B. Here is my code where i could finally show the match as an overlay. If i could get that rectangle coordinates then imcrop would do the job.
% Detect features in both images.
ptsTarget = detectSURFFeatures(Target);
ptsSample = detectSURFFeatures(Sample);
% Extract feature descriptors.
[featuresTarget, validPtsTarget] = extractFeatures(Target, ptsTarget);
[featuresSample, validPtsSample] = extractFeatures(Sample, ptsSample);
% Match features by using their descriptors.
indexPairs = matchFeatures(featuresTarget, featuresSample);
% Retrieve locations of corresponding points for each image.
matchedTarget = validPtsTarget(indexPairs(:,1));
matchedSample = validPtsSample(indexPairs(:,2));
% Show point matches. Notice the presence of outliers.
figure;
showMatchedFeatures(Target,Sample,matchedTarget,matchedSample);
title('Matched points (including outliers)');
%Estimate Transformation
[tform, inlierSample, inlierTarget] = estimateGeometricTransform(...
matchedSample, matchedTarget, 'similarity');
% Display matching point pairs used in the computation of the
% transformation matrix.
figure;
showMatchedFeatures(Target,Sample, inlierTarget, inlierSample);
title('Matching points (inliers only)');
legend('ptsTarget','ptsSample');
% Compute the inverse transformation matrix.
Tinv = tform.invert.T;
% Compute the recovered scale and angle.
ss = Tinv(2,1);
sc = Tinv(1,1);
scale_recovered = sqrt(ss*ss + sc*sc) ;
theta_recovered = atan2(ss,sc)*180/pi ;
% Recover the indentified positions by transforming the sample image.
outputView = imref2d(size(Target));
recovered = imwarp(Sample,tform,'OutputView',outputView);
% Show the sample image with recovered scale and angle as a pair blended
% into the Target image
figure, imshowpair(Target(:,:,1),recovered,'blend')
Image Analyst
Image Analyst 2015-10-12
Sorry - I don't have the Computer Vision System Toolbox. Perhaps Dima will answer you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by