Completing a line in a pixel image array
Afficher commentaires plus anciens
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 commentaire
newbie
le 10 Jan 2014
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 10 Jan 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 commentaires
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
le 10 Jan 2014
Modifié(e) : Image Analyst
le 10 Jan 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
le 10 Jan 2014
Catégories
En savoir plus sur Images dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!