필터 지우기
필터 지우기

how to crop image from detected rectangle.?

조회 수: 3 (최근 30일)
Nimisha
Nimisha 2016년 2월 16일
댓글: bhagyashri kapre 2019년 9월 12일
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