ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org

# Opencv python LineIterator returning position information?

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

>>> import cv
>>> 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 close merge delete

Sort by ยป oldest newest most voted

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

more

## Stats

Seen: 2,466 times

Last updated: Jun 04 '11