0
$\begingroup$

Rosanswers logo

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?


Originally posted by Marrok on ROS Answers with karma: 38 on 2011-06-04

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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 = 0.5:
         y += y_step
         error -= 1

   if also_steep:
       ret.reverse()

   return ret

Originally posted by Marrok with karma: 38 on 2011-06-04

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.