how to match plot and image coordinates ??

80 Ansichten (letzte 30 Tage)
Matlabber 1.0
Matlabber 1.0 am 26 Jun. 2015
Beantwortet: Matlabber 1.0 am 19 Jun. 2018
Dea all, I am doing some image analysis. My basic problem is that images and plots in matlab have different conventions on their coordinate system (CS) and this screws me. this makes some stuff really tricky and i could not yet solve that:
clear all;close all
% load image and view it
I = imread('coins.png');
figure(1); imagesc(I); colormap(gray); axis equal; axis tight; hold on;
% (i take imagesc rather than imshow for the axis ticks)
%find the circles & centers & plot
[centers, radii, metric] = imfindcircles(I,[15 30]);
viscircles(centers, radii,'EdgeColor','b');
% plotting them works well:
plot(centers(:,1),centers(:,2));
% now lets modify the image around the first center
x=round(centers(1,1))-5:round(centers(1,1))+5; %xrange around 1st center
y=round(centers(1,2))-5:round(centers(1,2))+5; %yrange around 1st center
I(x,y)=0; % put its surrounding to zero
figure(2)
imagesc(I); colormap(gray); axis equal; axis tight; hold on;
% => fail!
% the plotting has different coordinates:
plot(x,y,'.')
additionally, I want the image CS to match the plot CS, such that the pixel (0,0) is in the lower left, (1,0) it right neigbor and (0,1) just above (0,0). I manageed to do this via
figure(); imagesc(I'); set(gca,'YDir','normal'); %mind the transpose!
but this screws up coordinate matching completely. can you help me?
In the end I need plot(point1,point1) and I(point1,point2) to be displayed in the same spot, within the standard plot CS.
Thank you very much for any suggestions!
  2 Kommentare
Matlabber 1.0
Matlabber 1.0 am 26 Jun. 2015
another illustrating example:
I = imread('coins.png');
figure()
[a,b]=size(I);
plot([0,a],[0,b]); hold on
imagesc(I);colormap(gray); axis equal; axis tight;
bx=[1,50]
by=[10,100]
I(bx(1):bx(2),by(1):by(2))=0;
imagesc(I);colormap(gray); axis equal; axis tight;
plot([bx(1),bx(2),bx(2),bx(1)],[by(1),by(1),by(2),by(2)])
% => 2 diferent boxes!
Image Analyst
Image Analyst am 26 Jun. 2015
chris, did you see my answer? Evidently not because you're making the same mistake. Again: row, column is NOT x,y!!! It's just NOT.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 26 Jun. 2015
You have made a very common error that trips up all beginners eventually, and even sometimes experts:
(x,y) are not the coordinates of matrices in MATLAB!
And images are matrices. The coordinates of matrices are (row, column) which is NOT (x,y) - it's (y,x). So when you did this:
I(x,y)=0; % put its surrounding to zero
You should have done this:
I(y, x) = 0; % put its surrounding to zero
  3 Kommentare
Image Analyst
Image Analyst am 26 Jun. 2015
No, the x,y / row,column thing has been around as long as there has been digital images, and it's not just MATLAB that will have the issue. It's just a convention, like the convention that plots with cartesian coordinates have the origin at the lower left and y increases as you rise vertically, while for images and arrays, the origin is at the upper left and y (row, line number) increases as you go down vertically. They're just conventions you have to get used to.
You just have to get in the right mindset. Sometimes people like to think in terms of x,y and sometimes they like to think in terms of row, column. You just have to look out. For example if you're using getinput() on an image, it returns x,y. They might have chosen it to return row, column which would work fine for images, but it would be reversed to the way people are used to for regular x,y types of plots. There are a lot of functions in MATLAB that return a 2D coordinate - some return row, column (like find() function) and some return x,y - so you just have to check and be careful.
Matlabber 1.0
Matlabber 1.0 am 26 Jun. 2015
sounds not very promising. i guess ill have to transpose on the correct occasions, alsways

Melden Sie sich an, um zu kommentieren.


Matlabber 1.0
Matlabber 1.0 am 19 Jun. 2018
use the transpose " ' " and set the y-axis to "normal", in order to align the image-coordinate system to that of the internal matrix notation:
im=peaks(100); % make an image
im(1:20;20:40)=max(im(:)); % modify a specific region
imshow(im',[]);set(gca,'YDir','normal') % plot it such that the image coordinate system matches that of the internal matrix notation
so you can do all your image/ data processing as usual. once you plot images (also in combination with normal plots), transpose the image before plotting and make sure the y axis of your plot is set to normal.

Community Treasure Hunt

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

Start Hunting!

Translated by