ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Opencv python LineIterator returning position information?

asked 2011-06-04 07:58:56 -0600

Marrok gravatar image

updated 2014-01-28 17:09:47 -0600

ngrennan gravatar image

In the python sample for InitLineIterator here: Copied below for convenience:

>>> import cv
>>> img = cv.LoadImageM("building.jpg", cv.CV_LOAD_IMAGE_COLOR)
>>> li = cv.InitLineIterator(img, (100, 100), (125, 150))
>>> red_sum = 0
>>> green_sum = 0
>>> blue_sum = 0
>>> for (r, g, b) in li:
...     red_sum += r
...     green_sum += g
...     blue_sum += b
>>> print red_sum, green_sum, blue_sum
10935.0 9496.0 7946.0

Is there a way to also get the pixel positon?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2011-06-04 08:01:52 -0600

Marrok gravatar image

I couldn't find an existing one, so I reimplemented the algorithm here; but I still feel like the built-in line iterator should return position information.

def bresenham_march(img, p1, p2):
   x1 = p1[0]
   y1 = p1[1]
   x2 = p2[0]
   y2 = p2[1]
   steep = math.fabs(y2 - y1) > math.fabs(x2 - x1)
   if steep:
      t = x1
      x1 = y1
      y1 = t

      t = x2
      x2 = y2
      y2 = t
   also_steep = x1 > x2
   if also_steep:

      t = x1
      x1 = x2
      x2 = t

      t = y1
      y1 = y2
      y2 = t

   dx = x2 - x1
   dy = math.fabs(y2 - y1)
   error = 0.0
   delta_error = 0.0; # Default if dx is zero
   if dx != 0:
       delta_error = math.fabs(dy/dx)

   if y1 < y2:
      y_step = 1 
   else:
      y_step = -1

   y = y1
   ret = list([])
   for x in range(x1, x2):
      if steep:
         p = (y, x)
      else:
         p = (x, y)

      (b,r,g,a) = (-1,)*4
      if p[0] < img.width and p[1] < img.height:
         (b,r,g, a) = cv.Get2D(img, p[1], p[0])

      ret.append((p,(b,r,g)))

      error += delta_error
      if error >= 0.5:
         y += y_step
         error -= 1

   if also_steep:
       ret.reverse()

   return ret
edit flag offensive delete link more

Question Tools

Stats

Asked: 2011-06-04 07:58:56 -0600

Seen: 4,256 times

Last updated: Jun 04 '11