how to crop image from detected rectangle.?

4 次查看(过去 30 天)
clear all;clc
boxImage = imread('IMG.jpg');
boxImage = boxImage(:,:,3);
figure; imshow(boxImage);
title('Image of a Box');
sceneImage = imread('IMG1.jpg');
sceneImage = sceneImage(:,:,3);
figure; imshow(sceneImage);
title('Image of a Cluttered Scene');
boxPoints = detectSURFFeatures(boxImage);
scenePoints = detectSURFFeatures(sceneImage);
figure; imshow(boxImage);
title('100 Strongest Feature Points from Box Image');
hold on;
plot(boxPoints.selectStrongest(100));
figure; imshow(sceneImage);
title('300 Strongest Feature Points from Scene Image');
hold on;
plot(scenePoints.selectStrongest(300));
[boxFeatures, boxPoints] = extractFeatures(boxImage, boxPoints);
[sceneFeatures, scenePoints] = extractFeatures(sceneImage, scenePoints);
boxPairs = matchFeatures(boxFeatures, sceneFeatures);
matchedBoxPoints = boxPoints(boxPairs(:, 1), :);
matchedScenePoints = scenePoints(boxPairs(:, 2), :);
figure;
showMatchedFeatures(boxImage, sceneImage, matchedBoxPoints, ...
matchedScenePoints, 'montage');
title('Putatively Matched Points (Including Outliers)');
[tform, inlierBoxPoints, inlierScenePoints] = ...
estimateGeometricTransform(matchedBoxPoints, matchedScenePoints, 'affine');
figure;
showMatchedFeatures(boxImage, sceneImage, inlierBoxPoints, ...
inlierScenePoints, 'montage');
title('Matched Points (Inliers Only)');
boxPolygon = [1, 1;... % top-left
size(boxImage, 2), 1;... % top-right
size(boxImage, 2), size(boxImage, 1);... % bottom-right
1, size(boxImage, 1);... % bottom-left
1, 1]; % top-left again to close the polygon
newBoxPolygon = transformPointsForward(tform, boxPolygon);
figure; imshow(sceneImage);
hold on;
line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), 'Color', 'y');
title('Detected Box');
After ruuning this code, i am able to detect features of one image into another and it draws a rectange at detected portion. Now i want to crop that detected portion and want to save.. How to crop it and save it.?

采纳的回答

Image Analyst
Image Analyst 2016-2-16
Have you tried imcrop() and imwrite()?
  6 个评论
Sharmin Akhter
Sharmin Akhter 2019-2-20
thanx.. i am also facing the same problem..
bhagyashri kapre
bhagyashri kapre 2019-9-12
xLeft = min(newBoxPolygon(:, 1))
xRight = max(newBoxPolygon(:, 1))
yTop = min(newBoxPolygon(:, 2))
yBottom = max(newBoxPolygon(:, 2))
height = abs(yBottom - yTop)
width = abs(xRight - xLeft);
croppedImage = imcrop(sceneImage, [xLeft, yTop, width, height])
imshow(croppedImage);
Thank you above answer is helful for me in my project

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by