Crop an image using multiple coordinates in Matlab - matlab

I used the "imfreehand" to crop an irregular shape and save its positions into a variable. This position variable is a 85*2 double matrix (85 points, X and Y coordinates). Now, I want to use the same position to crop another image (different layer of the image, but the location of the objects is the same). The functions I can find all requires rectangle positions (X1,X2,Y1,Y2). In my situation, I have 82 different (X,Y) coordinates, how can I use the position information to crop a new image?

From what I understand, you want to take the coordinates created by imfreehand(...) to create a cropable object on another image. You can use the function impoly(hparent,position) for this purpose.
The MathWorks page provides an example to guide you on its usage.

Related

Hidden surface removing in Matlab

I have a representation of 3D object as a a) cloud point and b) triangle mesh
My goal is to rotate this object and than obtain a surface which is visible from one specific view.
Then I would remove the points which are not visible from a given view. Does anyone know how to do this in MATLAB. What method is the fastest?
The file with the point cloud contains the coordinates of each point, and information about the color stored in three RGB channels.
First line:
`-35.4717 88.8637 -99.3782 97 78 46`
I will be grateful for any help.
One possible way would be to re-implement the pipeline of a graphic processor.
Transform your object and project all triangles into an image plane. In this image plane, the distances of each part of the triangle can be stored.
With that information you can check if a vertex is further away than the one you have painted into the image plane.

Matlab: apply projection correction to an image subset

Following the question I posted here, I need to apply a projective transformation to an image given 4 points.
Say I successfully segmented the QR code from an image:
and I have stored in an array of points the coordinates of the QR vertices. In this case I would only need a rotation in order oto obtain the rectified image but in here:
I need to apply a projective correction to the image.
Is there a way of making these transformations knowing the coordinates of the said vertices?
EDIT
I solved it using #Xiang's suggestion and using HSV components of the image.
If I understand the question correctly you have the 4 corner points and you want to know to which coordinates to map them in the transformed image. Well, this is up to you. You know this is a square so just choose an arbitrary height or calculate based on some measurement from the original image and generate the coordinates:
(0,0)
(0, size)
(size, 0)
(size, size)
Now you can compute the transform and apply it to the original image using maketform.
From Matlab docs http://www.mathworks.com/help/images/ref/maketform.html:
T = maketform('projective',U,X)
To apply the transform use imtransform and set the fields UData, VData, XData, YData to specify your source coordinate system and the new sampling coordinates you wish to transform to.

Calculate the perimeter of a shape specified with a cloud of points

I have a shape which you can imagine as a lake in a field observed from the top (2D). I determined the border pixels of the shape after an image processing, so that I have the coordinates of each border point.
Now I want to calculate the perimeter of this shape. My problem is that I have the points not in following order that would give a closed loop, but unordered.
How can a problem like this be solved in Matlab? (including Curve-Fitting-Toolbox etc.)
Thank you for any suggestions!
You can use the function regionprops for this.
Turn your image into a binary image with 1 inside your 'lake' and 0 outside (which you should be easily able to do, as you mention you extracted the boundaries).
Then use:
props=regionprops(YourBinaryImage, 'Perimeter');
You can then access the perimeter as follows: props.Perimeter
If you have set of 3D points with (x,y,z) coordinates, you may set z to zero and use the 2D (x,y) points to find the convex hull using convhull regardless of their order .

Rounded corner rectangle coordinate representation

Simple rounded corner rectangle code in Matlab can be written as follows.
rectangle('Position',[0,-1.37/2,3.75,1.37],...
'Curvature',[1],...
'LineWidth',1,'LineStyle','-')
daspect([1,1,1])
How to get the x and y coordinates arrays of this figure?
To get the axes units boundaries, do:
axisUnits = axis(axesHandle) % axesHandle could be gca
axisUnits will be an four elements array, with the following syntax: [xlowlim xhighlim ylowlim yhighlim], it will also contain the zlow and zhigh for 3-D plots.
But I think that is not what you need to know. Checking the matlab documentation for the rectangle properties, we find:
Position four-element vector [x,y,width,height]
Location and size of rectangle. Specifies the location and size of the
rectangle in the data units of the axes. The point defined by x, y
specifies one corner of the rectangle, and width and height define the
size in units along the x- and y-axes respectively.
It is also documented on the rectangle documentation:
rectangle('Position',[x,y,w,h]) draws the rectangle from the point x,y
and having a width of w and a height of h. Specify values in axes data
units.
See if this illustrate what you want. You have an x axis that goes from −100 to 100 and y axis that goes from 5 to 15. Suppose you want to put a rectangle from −30 to −20 in x and 8 to 10 in y.
rectangle('Position',[-30,8,10,2]);
As explained by the comments there appears to be no direct way to query the figure created by rectangle and extract x/y coordinates. On the other hand, I can think of two simple strategies to arrive at coordinates that will closely reproduce the curve generated with rectangle:
(1) Save the figure as an image (say .png) and process the image to extract points corresponding to the curve. Some degree of massaging is necessary but this is relatively straightforward if blunt and I expect the code to be somewhat slow at execution compared to getting data from an axes object.
(2) Write your own code to draw a rectangle with curved edges. While recreating precisely what matlab draws may not be so simple, you may be satisfied with your own version.
Whether you choose one of these approaches boils down to (a) what speed of execution you consider acceptable (b) how closely you need to replicate what rectangle draws on screen (c) whether you have image processing routines, say for reading an image file.
Edit
If you have the image processing toolbox you can arrive at a set of points representing the rectangle as follows:
h=rectangle('Position',[0,-1.37/2,3.75,1.37],...
'Curvature',[1],...
'LineWidth',1,'LineStyle','-')
daspect([1,1,1])
axis off
saveas(gca,'test.png');
im = imread('test.png');
im = rgb2gray(im);
figure, imshow(im)
Note that you will still need to apply a threshold to pick the relevant points from the image and then transform the coordinate system and rearrange the points in order to display properly as a connected set. You'll probably also want to tinker with resolution of the initial image file or apply image processing functions to get a smooth curve.

matlab: how do create a projection

I want to create a perspective projection of a 3D image onto the x,y plane with a focal length of 10 and a principal point (-1, -5).
I found:
view
viewmtx
But I do not get how to tell matlab that I want to use the x,y plane nor how to set the focal length? Can someone explain me how to do that?
I'm afraid you are not looking for the right functions.
The view function does only change the point of view on the current axis, while viewmtxreturn a transformation matrix.
You may want to do something similar to what it is discussed on this post How do I draw a texture-mapped triangle in MATLAB?, where maketform and imtransform are the key functions to get a plane image reprojected into another certain 3d plane.