4

I have a problem in my matlab project

I have a RGB color image, and I have two specified pixel (x1,y1) and (x2,y2) I want to check each pixel in the image and determine if pixel is belong to the line between (x1,y1) and (x2,y2)

I tried to use these functions

m = (y2-y1)/(x2-x1); b= y1 - m*x1; if (y==m*x+b) then TRUE

but it almost fails

anybody has another way to solve it ? please

Thanks

user504363
  • 541
  • 2
  • 11
  • 25

2 Answers2

3

bear in mind that pixels have area and aren't just points. depending on how you define your coordinates, you are checking something like "does the centre of my pixel lie exactly on the line between the centres of these other pixels"

i'm guessing that you may want to leave some leeway, ie set some tolerance and then check

if abs(m*x+b -y) < tolerance  

instead of straight equality

second
  • 28,029
  • 7
  • 75
  • 76
3

You are, though you may not know it, trying to implement Bresenham's Algorithm or a similar algorithm.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161