'Plot over image appears offset
I'm attempting to draw a circle and a straight line a=b over an imported image. The plot appears but I've run into two problems:
- The image is not centered - here, the circle is supposed to be in the center of the image
- The line appears with an inverted slope
clear;close all;
I0 = imread('temp.png');
I1 = im2double(I0);
I = imresize(I1,[320 320]);
a=0:1:320;
b = a;
r=320/2;
xc=0; yc=0;
th = linspace( 0, 2*pi, 100);
x = r * cos(th) + xc;
y = r * sin(th) + yc;
figure,imshow(I)
hold on
plot(b,a,'linewidth',3)
hold on
plot(x,y,'white','linewidth',3)
I can fix the slope of the line by plotting (b,-a,'linewidth',3) instead but the line does not draw over the image.
Any help would be much appreciated. Yes this is for an assignment but it would not the final answer at all :)
Solution 1:[1]
The point (0,0) is not the center of the image. Since you are displaying the image
Iwithimshow, the axes will have the size ofI, in your case from0to320. So you need to choose the center of the circle based on that. See modified line with comment%1in the code below.imshowsets the axisijmode, used for images, which inverts the vertical axis. To plot the line normally you can set thexymode. See added line with comment%2in the code.
clear;close all;
I0 = imread('temp.png');
I1 = im2double(I0);
I = imresize(I1,[320 420]);
a=0:1:320;
b = a;
r=320/2;
xc = size(I, 2)/2; yc = size(I, 1)/2; %1
th = linspace( 0, 2*pi, 100);
x = r * cos(th) + xc;
y = r * sin(th) + yc;
figure,imshow(I)
hold on
axis xy %2
plot(b,a,'r','linewidth',3)
hold on
plot(x,y,'white','linewidth',3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Luis Mendo |

