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 :)
CodePudding user response:
The point (0,0) is not the center of the image. Since you are displaying the image
I
withimshow
, the axes will have the size ofI
, in your case from0
to320
. So you need to choose the center of the circle based on that. See modified line with comment%1
in the code below.imshow
sets the axisij
mode, used for images, which inverts the vertical axis. To plot the line normally you can set thexy
mode. See added line with comment%2
in 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)