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

5 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Bruno Pop-Stefanov
Bruno Pop-Stefanov le 21 Jan 2014
Modifié(e) : Bruno Pop-Stefanov le 21 Jan 2014
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.

Plus de réponses (2)

Image Analyst
Image Analyst le 21 Jan 2014
Use poly2mask() to make a binary image from the 4 corner points:
binaryImage = poly2mask(x, y);
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 commentaires
Image Analyst
Image Analyst le 21 Jan 2014
When you say "Basically I want to make all the pixels inside the smaller square white and the remaining as black." that is not necessarily cropping. You could just set some regions black or white without cropping. You'd need to specify what you want to happen to the pixels in the region outside the larger square, the 4 triangular corner regions, and the inner small square. I'm not really sure what region you want black, white, or unchanged.
Animesh
Animesh le 22 Jan 2014
Sorry for the clarity sir,I wanted the exact thing given in the above figure

Connectez-vous pour commenter.


Doug Hull
Doug Hull le 21 Jan 2014
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.

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by