How to crop a square that has a rotational angle?

조회 수: 8 (최근 30일)
Guilherme Pelegrini
Guilherme Pelegrini 2017년 3월 9일
답변: Image Analyst 2022년 4월 19일
I need to crop one square of this image:
As you can see, I can't use imcrop() because it isn't horizontal, the squares has a rotational angle, and the region cropped need to be precisely on the edge of the line.
How can I do it?

답변 (3개)

Massimo Zanetti
Massimo Zanetti 2017년 3월 9일
편집: Massimo Zanetti 2017년 3월 9일
Inspect this: roipoly
Here is an example of what you can do:
%open an image
A = imread('coins.png');
%run roi tool, where you can define your polygon
BW = roipoly(A);
%extract image pixels in the roi
S = regionprops(BW,'ConvexImage','BoundingBox');
r = S.BoundingBox;
Q = A( round(r(2)) + (1:r(4)) , round(r(1)) + (1:r(3)) );
%display cropped image
im = imagesc(Q);
im.AlphaData = S.ConvexImage;
axis image; colormap gray;
In the roipoly window track your points manually to define a polygon, as in the picture:
Then, right-click and "Create Mask", you will get your cropped image:
  댓글 수: 1
Selina
Selina 2019년 4월 1일
Hi,
Is it possible to use rectangle crop using the ROI function? If so, can anyone please send a sample coding?

댓글을 달려면 로그인하십시오.


cui,xingxing
cui,xingxing 2022년 3월 5일
I wrote an enhanced 'imcrop' function that supports rotating rectangle interception, but yours is an irregular quadrilateral, but you can also refer to what I wrote with 'estimateGeometricTransform2D' to estimate the conversion matrix, and then bring in the 'imwarp' function can be completed arbitrary shape interception, I hope it helps you

Image Analyst
Image Analyst 2022년 4월 19일
If you need to find the white lines automatically, you can try hough(), houghlines(), or radon().
I'm attaching a demo for how to use the radon transform to find the angle or rotation, and then it uses imrotate() to rotate the image until it's straight. Adapt as needed.

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by