How can I draw a particular line pixel by pixel ?

조회 수: 11 (최근 30일)
Teo Protoulis
Teo Protoulis 2018년 3월 22일
댓글: Image Analyst 2018년 3월 23일
I have the below code in order to draw the line: y = m*x + b.
I want to draw the line pixel by pixel using the code below:
x1 = 6;
x0 = 3;
y1 = 7;
y0 = 2;
Dx = x1 - x0;
Dy = y1 - y0;
m = Dy / Dx;
b = y0 - (m*x0);
invm = 1 / m;
invmb = b* invm;
if (m<=1)
for x = x0:1:x1
y = (m*x) + b;
yp = floor(0.5 + y);
drawpixel(x,yp);
end
else
for y=y0:1:y1
x = invm*x - invmb;
xp = floor(0.5 + x);
drawpixel(xp,y);
end
end
How can I implement the drawpixel(xp, y) function ?
  댓글 수: 2
Guillaume
Guillaume 2018년 3월 22일
Wouldn't the drawpixel function be just a call to plot, exactly as you've done for the m<=1 case?
Note that much better line drawing algorithms exist. I'd recommend you do a search for bresenham's or Wu's line drawing algorithms.
Teo Protoulis
Teo Protoulis 2018년 3월 22일
I am studying for a university class and I have to go through different algorithms. Bresenham's is one I am going to have a look at. I should have also written drawpixel instead of plot.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2018년 3월 22일
If you want to set/write a value, say 255, into some digital matrix (like an image) then you can do this:
function yourImage = drawpixel(yourImage, xp, y)
column = round(xp);
row = round(y);
yourImage(row, column, :) = 255; % Or whatever value you want.
Then to call that function in your loop, do this:
yourImage = drawpixel(yourImage, xp, y);
Note that images and line plots have the y axis direction flipped compared to one another, so if you want the plot on top of the image, you'll have to set ydir().
  댓글 수: 2
Teo Protoulis
Teo Protoulis 2018년 3월 23일
So now I am able to create the image - matrix with the color values I want. Then how can I show the produced line ? Would a simple call to plot function be enough ?
Image Analyst
Image Analyst 2018년 3월 23일
No, you call imshow() instead of plot() because you have an image, not some x,y vectors. Since you created the matrix with the color values you want (i.e. the line is burned into the image with the right color), you simply call imshow() to display that matrix with the burned in line.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by