plotting is displayed upside down at MATLAB GUI - matlab

I am working on video surveillance on crowd density estimation. I am implementing a corner detection method for this project. However, I have a problem when my code is combined in GUI MATLAB, the display of plotting corner is upside down and it is not overlay on the original image. Here my code on plotting the image in GUI Matlab.
The problem is solved already and here is the correct code:
% Find row,col coords.
[r,c] = find(cim2);
%After getting row and column coordinate, I plot them together in the original image which is
% overlay image and plotting
imshow(inFrame, 'Parent', handles.axes8);
hold on;
p1 = plot([c],[r],'r.');
set(p1, 'Parent', handles.axes8);
Thank you for #Lokesh for his suggestion.

The explanation of your problem is very well given by floris.But here due to lack of full your code we are not able to perform it on our system. you can aslo try for
axis ij
which is some what similar to above answer. you can also try the below code.
imshow(inFrame,'Parent',handles.axes8);
hold on;
p1 = plot([c],[r],'r.');
set(p1,'Parent',handles.axes8);
hope this works for you..let me know about the result..

imshow inverts the Y axis. Conventionally, in image display the top left corner of the screen is (0,0) with i (the first coordinate) running down the screen, and j (the second coordinate) from left to right.
If you want the image axis to look like a conventional plot axis you can do
axis xy
This will "invert" your image, putting 0,0 in the bottom left.

Related

Projecting an image onto a cylindrical surface in a way where the image appears flat

I'm trying to print out QR codes onto the surface of water bottles using matlab. However, the QR bottles cannot be recognized because of the curved surface of the bottle, and the therefore deformed shape of the QR codes.
I was wondering what mathematical equation I might have to apply to deform the picture (for both the general geometry of the picture, and the spacing deformation inside the picture), so that when printed onto a cylindrical surface, it seems like its on a flat surface to a camera.
Thanks!
I had some success with this and though it doesn't use Matlab, it may give you, or someone else an idea on a possible way to get started.
I generated a QR code with qrencode in Terminal like this:
qrencode -o qr.png -d 300 'http://www.thesetchells.com'
I resized that to 500x500 and then generated a "displacement map" which is just a linear gradient with a contrast stretch created to match the size of the QR code. I used ImageMagick in Terminal, but obviously you can use Matlab or other tools:
I then applied the displacement map to my QR code like this:
convert qr.png map.png -fx 'p{v*w,j}' result.png
And got a distorted QR code, which I printed and wrapped round a large bottle and my iPhone was able to read it.
Anthony Thyssen has some very useful information on "displacement maps" here.
So I've tried an approach to my problem using the basic "car.jpg" image included in Matlab.
So far, I've taken a square part of the car picture, and wrapped it around the surface of part of a cylinder.
Here is my code:
close all, clf, clear all
%% Creating cylinder
r = 6; %centimeter
h = 25; %centimeter
[X,Y,Z] = cylinder(r,100);
x = X(:,1:ceil(length(X)/3));
y = Y(:,1:ceil(length(Y)/3));
z = Z(:,1:ceil(length(Z)/3));
%% Plotting cylinder surface
figure(1), clf
h = surf(x,y,z); hold on
axis([-r r -r r]*2.2)
plot3([-r -r], get(gca,'ylim'), [0 0]);
plot3([r r], get(gca,'ylim'), [0 0]);
plot3(get(gca,'xlim'), [-r -r], [0 0]);
plot3(get(gca,'xlim'), [r r], [0 0]);
xlabel('x');
ylabel('y');
zlabel('z');
rotate3d on
axis vis3d
%% Car image
img = imread('car.jpg');
img = imrotate(img,180);
figure(2), clf
%imshow(img(1:340, 1:340, :));
imagesc(img(1:340, 1:340, :));
figure(), clf
warped_plot = warp(x,y,z,img(1:340, 1:340, :))
The next step could be to now project that warped image onto a flat surface (but I'm not sure how to do that mathematically, nor have I found a in-built Matlab function to do this).
Another possibility I was hoping to get feedback on, is to find a mathematical relation between corresponding horizontal points on the flat and cylindrical image, and then to apply the inverse of that equation to the flat image, so that once it is printed out and stuck onto a cylindrical surface it appears flat.

MATLAB Shift the origin (0,0) of the pixels in my image

As an input of my code, I need to have some positions on my picture: the positions are in pixels and the origin (0,0) is in the CORNER top left of my picture
The problem is that when I run my code which maps the positions on my picture, the origin shifts to the bottom left :
So my question is : how could I also shift my input (positions of my picture1) so that it is relevant with the code process?
Thank you for your help,
Aude
Adding an answer with a bit detail:
In computers, image processing etc it is the norm to have the (0,0) pixel in the top left corner. However, it is understandable that when you have x,y data, you'd want to plot it together with the image. Here some solutions:
imshow(image);
axis xy; % This line sets up the axis in a standard coordinate system
you can revert it with axis ij
Often, this is not enough. As imshow assumes that each pixel is in integer index position, and you may not have that. Maybe your data is in milimeters, or in any other arbitrary units. A solution to that is using imagesc
imagesc(img);
is equivalent to imashow(img);axis xy. Additionally, you can choose arbitrary matrices for pixel locations as imagesc(x,y,img);
Finally, you can flipud your data for plotting, but I suggest you do that inline with the plot, so you dont modify the data itself.
imshow(flipud(img))
With images you reverse the Y axis:
set(ax,'YDir','reverse');
That would depend on your code. Maybe you can do it on-the-fly so you'll get the desired output right away.
If not just flip the output. You could use flipud for that.
https://de.mathworks.com/help/matlab/ref/flipud.html

Matlab - reverse value of axis in plot [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I edit the axes of an image in MATLAB to reverse the direction?
The colour image is plotted using the image function based on some information obtained using imread function and for the white and blue image basically I am selecting the coordinates of the heat point(red and blue and their variations basically) from the map and then displaying them using plot function. The problem is that the plotted values are reversed on the Y axis and I can't figure out how to reverse the Y axis of the plot in order to obtain the same correlation between the images.
Could you please explain me how to solve this problem?
By default, matlab draws graphs with (0,0) at the bottom left corner. However, it draw images with (0,0) in the top-left corner.
You can change the image axes to standard bottom-left origin by using the command
axis xy;
Remember to make sure that your image is the currently selected figure.
Use rot90() to rotate the matrix, or a combination or fliplr() (flips matrix, left and right) and flipud() (flips matrix up and down) that produced the heat map image.
If you are plotting an image and you don't want to see the axis tic marks you can turn them off with
axis off
if you are interested in changing the direction of either the x-axis and/or y-axis in an axes handle-object, you can use the set function as follows
set(axesHandle,'XDir','rev','YDir','rev')
where you use XDir or YDir (or both) based on the axis you want reversed.

How do I edit the axes of an image in MATLAB to reverse the direction?

I would like to edit the axes in my series of images being displayed.
This is what my image looks like:
As you can see, it ranges from 0 to about 500 from top to bottom. Can I invert that?
Plus, I want to mirror the image being shown, so that it starts from left to right... or, if it's possible, to let the axes show from right to left.
To reverse an axis, you can set the 'XDir' or 'YDir' property of the current axes to 'reverse':
set(gca,'XDir','reverse'); %# This flips the x axis
Keep in mind that flipping an axis in this way flips everything in the plot as well. This probably isn't what you want to do for the y axis. You probably just want to flip the y axis labels, which you can do by modifying the 'YTickLabel' property in the following way:
yLimits = get(gca,'YLim'); %# Get the y axis limits
yTicks = yLimits(2)-get(gca,'YTick'); %# Get the y axis tick values and
%# subtract them from the upper limit
set(gca,'YTickLabel',num2str(yTicks.')); %'# Convert the tick values to strings
%# and update the y axis labels
Im = imread('onion.png');
Im = flipdim(Im ,1); % vertical flip the image.
axis xy; %set the xy to be at (0,0), this flips the image back again.
And whoop dee doo the image now have an y axis with the range from bottom to top!
How can I reverse the y-axis when I use the IMAGE or IMAGESC function to display an image in MATLAB? Another solution from mathworks
I found gnovice's answer helpful but it needed some tweaks for me. I think the following is a more general way to reverse the labels on the y axis. Simply sort the y tick numbers in descending order and relabel.
yTicks = get(gca,'YTick');
yTicks_reverse = sort(yTicks,2,'descend');
set(gca,'YTickLabel',num2str(yTicks_reverse.'));
I was redirected here from a duplicate question:
Flipping axis ticks
What 'ale' wanted to do there was just to flip the y-axis direction to be top down. If that is the only thing needed and nothing else, I would use:
axis ij

Plot Overlay MATLAB

How do you take one plot and place it in the corner (or anywhere for that matter) of another plot in MATLAB?
I have logarithmic data that has a large white space in the upper right-hand side of the plot. In the white space I would like to overlay a smaller plot containing a zoomed in version of the log plot in that white space (sort of like a magnified view).
Before you tell me it can't be done, I would like to mention that I have seen it in action. If my description is lacking, just let me know and I'll attempt to better describe it to you.
An example:
x = 1:20;
y = randn(size(x));
plot(x, y,'LineWidth',2)
xlabel('x'), ylabel('y'), title('Plot Title')
h = axes('Position', [.15 .65 .2 .2], 'Layer','top');
bar(x,y), title('Bar Title')
axis(h, 'off', 'tight')
You can use axes properties 'position' and 'units' and make them overly. Pay attention to create small axes after big one or use uistack() function so that big does not hide small one.
What you can not do is to make an axes child of another one (like Mathworks do with legend). But you do not need it anyway.
For the second plot you have to use axes and line instead of plot and hold on.
Units as 'normalized' (which is default) allows uniform resizable look when parent figure is being resized (e.g. manually maximized).