Completing a line in a pixel image array

Hello All
I want to create a pixel array image and then want to make a line with a specified angle from the centre. I want to make all the pixel falling on this line (or the next immediate pixel) as ones and the others zeros.
I found out there is a method to draw lines, using plot function, but that is not my goal. I am trying to make a filter for an image.
Here is what I have achieved so fat, but the line only comes half way and then there is another half which I cant understand.
close all, clear all;
clc;
rows=500;
cols=500;
img=zeros(rows,cols);
theta=50;
deltatheta=0.5;
if theta>90
theta=-(90-(theta-90));
end
for i=1:rows
for j=1:cols
x = j-cols/2 ;
y = rows/2-i;
if atan(y/x)*180/pi<theta && atan((rows/2-(i+1))/((j+1)-cols/2))*180/pi>theta
img(i,j) = 1 ;
end
end
end
figure, imagesc(img), colormap gray
Any help would be much appreciated.

1 comentario

newbie
newbie el 10 de En. de 2014
I am quite new to MatLab and is finding the experience all interesting. However, I am frustrated at this point. It might be very simple for most of you but need some serious help here.

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 10 de En. de 2014
Editada: Matt J el 10 de En. de 2014
I would approach it like below. Basically, it looks at all the rays from the image center to each pixel coordinate and tests which rays make an acute angle to your line (dot product>=0) and also which pixels have a projected distance to your line of one or less (using cross product computation).
theta=50;
rows=500; cx=rows/2+.5;
cols=500; cy=cols/2+.5;
e=ones(1,rows*cols);
[X,Y,Z]=ndgrid((1:rows)-cx, (1:cols)-cy, 0);
Coords=[X(:),Y(:),Z(:)].';
linedir=[cosd(theta), sind(theta),0].';
AcuteAngle=(linedir.'*Coords >= 0);
DistLessThan1=sum(cross(Coords,linedir*e).^2)<=1 ;
img=reshape(AcuteAngle & DistLessThan1,rows,cols);

3 comentarios

newbie
newbie el 10 de En. de 2014
Thank you very much Matt for the response. However, your code also creates only half portion of the line. I quite did not understand the ndgrid function that you have written, so I dont know if I am mistaken,
Thanks again for the help
Matt J
Matt J el 10 de En. de 2014
Editada: Matt J el 10 de En. de 2014
Yes, it only draws half a line because in your post you said something about wanting it to run "from the centre". However, if you want the whole line, you would just omit the AcuteAngle test from the version I gave you,
img=reshape(DistLessThan1,rows,cols);
newbie
newbie el 10 de En. de 2014
Okay, now I get it. Thank you very much for help. It was a relief :)

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 10 de En. de 2014
Do you want just a binary image (line or no line)? If so, use Matt's code or the first two lines of my code below. Do you have an existing image where you want to burn the line into? If so, use hline, and burn in the mask:
hLine = imline(gca,[10 100],[10 100]); % Define line.
binaryImage = hLine.createMask(); % Create binary mask.
burnedImage(binaryImage) = 255; % Burn into image.
You can find the endpoints (args 2 and 3) for a certain angle using normal trigonometry. If you want a full blown demo with both line and ellipse being burned into the existing gray level image, see the attached demo.

3 comentarios

Matt J
Matt J el 10 de En. de 2014
Editada: Matt J el 10 de En. de 2014
You can find the endpoints (args 2 and 3) for a certain angle using normal trigonometry.
It's actually rather tedious. For different angles, the ray will intersect different sides of the image bounding box. If going this route, I would recommend doing the endpoint computation with VERT2LCON and LCON2VERT, as below
theta=50;
rows=200; cx=rows/2+.5;
cols=200; cy=cols/2+.5;
linedir=[-sind(theta), cosd(theta)];
endpoints=[[cx,cy]; [cx,cy]+norm([rows,cols])*linedir];
% not bound to the image grid
[A,b,Aeq,beq]=vert2lcon(endpoints);
A=[A;eye(2);-eye(2)];
b=[b;rows;cols;-1;-1];
endpoints=lcon2vert(A,b,Aeq,beq); %bound to image grid
endpoints=fliplr(endpoints).';
h=imline(gca,endpoints(1,:),endpoints(2,:));
Image Analyst
Image Analyst el 10 de En. de 2014
Editada: Image Analyst el 10 de En. de 2014
Good point. I didn't think of that case. I just thought of the lines lying totally inside the image. If they struck the image edge it would get somewhat complicated.
newbie
newbie el 10 de En. de 2014
Thank you very much Analyst for the response. This also does the work the trick for me, but I think Matt's code is more generic and addresses the issue. As i said, I want to classify each pixel falls into a line or a band of lines. I dont want to burn it into another image, but I want to do an inverse fourier of an image with a customizable filter like this.

Iniciar sesión para comentar.

Categorías

Más información sobre Images en Centro de ayuda y File Exchange.

Preguntada:

el 10 de En. de 2014

Comentada:

el 10 de En. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by