# how to crop a rectangle/square out of a image when four end points are given ?

조회 수: 9(최근 30일)
Animesh 2014년 1월 21일
댓글: Animesh 2014년 1월 22일
How to crop a rectangle aligned at some angle to the x axes when its four coordinates are known ?? Say for example I have a square and I want to crop out a square connecting all the mid points of the sides of the bigger square.Basically I want to make all the pixels inside the smaller square white and the remaining as black.

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

### 채택된 답변

Bruno Pop-Stefanov 2014년 1월 21일
편집: Bruno Pop-Stefanov 2014년 1월 21일
I wrote a custom function that "draws" lines onto a matrix (superimposeLines.m) and a script that use that function to draw the four sides of your tilted rectangle (Animesh.m). Then, I use the function imfill to fill all the pixels inside the small rectangle with the same color as the sides.
Here is the final result:
You can use that function to draw more complex shapes, like a polygon. All you have to do is properly write the starting and end points of each line in your polygon and pass it to superimposeLines. Look at the code in the attached files for how to properly format your matrix of point coordinates.
##### 댓글 수: 1표시 없음숨기기 없음
Animesh 2014년 1월 22일
Thanks a lot sir for helping with the code..

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

### 추가 답변(2개)

Image Analyst 2014년 1월 21일
Use poly2mask() to make a binary image from the 4 corner points:
Then call regionprops to get the bounding box:
measurements = regionprops(binaryImage, 'BoundingBox');
Then crop out out the tilted box from the original image:
croppedImage = imcrop(originalImage, measurements.BoundingBox);
##### 댓글 수: 2표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
Animesh 2014년 1월 22일
Sorry for the clarity sir,I wanted the exact thing given in the above figure

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

Doug Hull 2014년 1월 21일
In MATLAB, an image is really just a matrix. You could do something like this:
m = ones(20,20,3)
m(3:8,2:17,:) = 0
imshow(m)
You get the idea.

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

### 범주

Find more on Image Arithmetic in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by