Matlab: How to convert image coordinate to array indices - image

I have a GUI which display 2D image. However, I am not able to recover data accurately by using mouse coordinates. This is because I am not able to convert image coordinates to array indices properly.
I use ginput to get mouse coordinates.
Any help would be appreciated.
Regards
Dushyant

When displaying an image in matlab, your origin(for image) sits at top left corner of the image, x-axis increase towards downwards and y-axis increase towards right, while with mouse you get co-ordinates with x-axis increasing towards right and y-axis towards up(so a rotation of +90 degrees with respect to image co-ordinates plus a translation),,, so you have to transform one of your co-ordinates to the other to get the right values, for example if your origin of figure window is at top left corner and image spans the whole frame then,,,
x_image = -y_mouse and
y_image = x_mouse.

Try that:
fig=figure, imshow(myfigure);
[x, y] = getpts(fig);

Related

Align Pointclouds to a coordinate system

I'm currently working on 3D-Reconstruction from images to measure the object size.
I have created a pointcloud and wanted to measure it size now (in the scaling of the pointcloud, as I know the size, because it is the reference object).
But that's where I don't know how to do it. I was previously thinking about searching the outer points (it's a box) and just calculate the distance. This of course works, but it's not really the width, length, and height of the object, as the pointcloud is rotated and translated.
Plotted pointclouds with coordinate axis
As you can see in the image, the pointclouds are rotated and translated and my question is:
How can I calculate the rotation and translation to apply it back to the coordinate axis?
If something was unclear, hit me up, and I will try to elaborate better.
Edit:
So I calculated the plane of the side of my object now, which is:
0.96x + -0.03y + 0.28z + -4.11 = 0
I don't really care if it's aligned with the XY-plane or the YZ-plane, but it has to be aligned with one of them.
Now it's about the calculation from the object plane and the world plane where I'm struggling.

Image with absolute position to position in pdf itext

So I have an image and I set its position absolute (jsf). Then the user can change the position with the drag/drop functions from primefaces. The new coordinates (left/top) are then stored in the mysql database. When the page is reloaded, the image is again absolutely positioned with the coordinates from the db.
For example the image coordinates are (the page is scrollable): left:68px; top: 826.5px
--> In java I use itext and I want to place the image with the absolute values ​​from the database. I know that the 0/0 coordinates of the PDF document are bottom left.
I want to use image.setAbsolutePosition() but how does the coordinates match???
They right coordinates for the pdf would be: x about 135 and y about 700 but how this fit together with the coordinates on the screen 68px/826.5px? I have already calculate around a lot but do not understand it...
I also scale the images: the original with/height on screen: 35x35
and I use: image.scaleAbsolute(25, 25);
So how does this work? thx :)
If you have an image whose top left corner is at {x,y} in a top-left coordinate system and you want to place it in a coordinate system that uses bottom-left for the origin and bottom-left for placement then you need to know both the height of the image and the height of the document. The {x} won't change but the new {y} should be Document.Height - {original y} - Image.Height}.
The image below shows an image that's at {50,50} in a top-left system. Once we add it to a document we need to know the document's height (800px) so that we can map. We also need to know the image's height (75px).

How to draw keypoints onto input image using matlab?

This question is regarding SIFT features and its use in MATLAB. I have some data containing the following information:
The x,y co-ordinates of each feature point
The scale at each feature point
The orientation at each feature point
The descriptor vector for each feature point. Each vector has 128 components.
I'm having a problem with drawing the keypoints onto the corresponding input image. Does anyone have an idea of how to do this? Are there any built-in functions I can use?
Thanks a lot.
If you just want to display the points themselves, read in the x and y co-ordinates into separate arrays, then display the image, use the hold on command, then plot the points. This will overlay your points on top of the image.
Assuming you have these co-ordinates already available and stored as x and y, and assuming you have your image available and stored as im, you can do this:
imshow(im);
hold on;
plot(x, y, 'b.');
Keep in mind that the x co-ordinates are horizontal and increase from the left to right. The y co-ordinates are vertical and increase from top to bottom. The origin of the co-ordinates is at the top left corner, and is at (1,1).
However, if you want to display the detection window, the size of it and the orientation of the descriptor, then suggest you take a look at the VLFeat library as it has a nice function to do that for you.
Suggestion by Rafael Monteiro (See comment below)
You can also modify each point so that it reflects the scale that it was detected at. If you want to do it this way, assume you have saved the scales as another array called scales. After, try doing the following:
imshow(im);
hold on;
for i = 1 : length(x)
plot(x(i), y(i), 'b.', 'MarkerSize', 10*scales(i));
end
The constant factor of 10 is pretty arbitrary. I'm not sure how many scales you have used to detect the keypoints and so if the scale is at 1.0, then this would display the keypoint at the default size when plotting. Play around with the number until you get results that you're comfortable with. However, if scale doesn't matter for you, the first method is fine.

Labeling plots with images in Matlab

Is there any way of labeling plots with images. For example, when I use the following:
plot(Y(:,1),Y(:,2),'o','LineWidth',2);
gname(names)
I can label each dot in a plot with a name. Is there any way to insert images instead of names?
It is possible, but not as convenient as gname by far. You can use the low-level version of image to insert images in your plot at arbitrary positions. Here's a simple example which puts the "Mandrill" image that comes with Matlab with its upper left corner pixel at the position (pi/2, 0):
% example plot
x = linspace(0, 2*pi, 100);
plot(x, cos(x))
% insert image
load mandrill
colormap(map)
image('CData', X, 'XData', [pi/2, pi/2 + 0.5], 'YData', [0, -0.3])
The result looks like this:
Problems with this approach:
There is no interactive point-and-click facility, you have to explicitly insert and position the image labels programmatically, or program such a point-and-click facility yourself. ginput might help doing so.
A figure window can only have one associated color map. That means if you have different images, they either all have to use the same colormap or have to be truecolor images.
Not just the position, but also the display size of the image has to be specified in the call to image, and both are by default specified with respect to the plot's coordinate system. This makes it hard to achieve the correct aspect ratio. You can switch (temporarily) to absolute units using the axes property 'Units' , but then you have to figure out the correct position in e.g. absolute millimeters or inches. Moreover, images are usually indexed with vertical coordinates increasing from top to bottom, while plots usually have vertical coordinates increasing from bottom to top. This is the reason for the negative value -0.3 in the 'YData' property above.
Alternatively, you can insert images each in their own little axes sitting on top of the plot's axes, which makes it easy to get the right orientation and aspect ratio using axis image. You'll still have the problem though to figure out the correct position for the axes.

Crop an image in Matlab

I want to crop an image from a specific row onwards. Please help me how can I do this. I am a beginner in Matlab.
This page has a lot of great info on dealing with images in matlab.
When you load an image in matlab, it is loaded as a MxNx3 matrix. The third dimension stores the RGB values of each pixel. So to crop an image you simply select just the range of rows and columns you want to keep:
cropped_image = image(RowStart:RowEnd,ColStart:ColEnd,:);
See this: http://www.mathworks.com/help/techdoc/creating_plots/f9-47085.html
There is a graph editor icon in the screen where you see your graph, it should look like this:
Press it, you will get a big graph editor, now try pressing on the graph or one of the functions, in the lower right part you can set ranges, this will crop the image.
You can use imcrop function in Matlab
CropIm = imcrop(I, rectangle);
rectangle is a four-element position vector [xmin ymin width height] which indicates the size and position of the crop rectangle.
Im = imread('test.tif');
Im2 = imcrop(Im,[75 68 130 112]);
imshow(Im), figure, imshow(Im2)

Resources