Python OpenCV 3.0行迭代器
我想使用Python在OpenCV 3.0中使用,它在为Python构建的OpenCV 3.0中仍然可用吗?互联网上的答案似乎都指向Python OpenCV 3.0行迭代器,python,opencv,opencv3.0,Python,Opencv,Opencv3.0,我想使用Python在OpenCV 3.0中使用,它在为Python构建的OpenCV 3.0中仍然可用吗?互联网上的答案似乎都指向cv.InitLineIterator,它是cv模块的一部分。我已尝试导入此模块,但它似乎不包含在当前构建中。它是被重命名还是被严格删除了?我已经解决了自己的问题。行迭代器似乎在cv2库中不可用。因此,我制作了自己的行迭代器。没有使用循环,所以应该非常快。如果有人需要,下面是代码: def createLineIterator(P1, P2, img): "
cv.InitLineIterator
,它是cv
模块的一部分。我已尝试导入此模块,但它似乎不包含在当前构建中。它是被重命名还是被严格删除了?我已经解决了自己的问题。行迭代器似乎在cv2库中不可用。因此,我制作了自己的行迭代器。没有使用循环,所以应该非常快。如果有人需要,下面是代码:
def createLineIterator(P1, P2, img):
"""
Produces and array that consists of the coordinates and intensities of each pixel in a line between two points
Parameters:
-P1: a numpy array that consists of the coordinate of the first point (x,y)
-P2: a numpy array that consists of the coordinate of the second point (x,y)
-img: the image being processed
Returns:
-it: a numpy array that consists of the coordinates and intensities of each pixel in the radii (shape: [numPixels, 3], row = [x,y,intensity])
"""
#define local variables for readability
imageH = img.shape[0]
imageW = img.shape[1]
P1X = P1[0]
P1Y = P1[1]
P2X = P2[0]
P2Y = P2[1]
#difference and absolute difference between points
#used to calculate slope and relative location between points
dX = P2X - P1X
dY = P2Y - P1Y
dXa = np.abs(dX)
dYa = np.abs(dY)
#predefine numpy array for output based on distance between points
itbuffer = np.empty(shape=(np.maximum(dYa,dXa),3),dtype=np.float32)
itbuffer.fill(np.nan)
#Obtain coordinates along the line using a form of Bresenham's algorithm
negY = P1Y > P2Y
negX = P1X > P2X
if P1X == P2X: #vertical line segment
itbuffer[:,0] = P1X
if negY:
itbuffer[:,1] = np.arange(P1Y - 1,P1Y - dYa - 1,-1)
else:
itbuffer[:,1] = np.arange(P1Y+1,P1Y+dYa+1)
elif P1Y == P2Y: #horizontal line segment
itbuffer[:,1] = P1Y
if negX:
itbuffer[:,0] = np.arange(P1X-1,P1X-dXa-1,-1)
else:
itbuffer[:,0] = np.arange(P1X+1,P1X+dXa+1)
else: #diagonal line segment
steepSlope = dYa > dXa
if steepSlope:
slope = dX.astype(np.float32)/dY.astype(np.float32)
if negY:
itbuffer[:,1] = np.arange(P1Y-1,P1Y-dYa-1,-1)
else:
itbuffer[:,1] = np.arange(P1Y+1,P1Y+dYa+1)
itbuffer[:,0] = (slope*(itbuffer[:,1]-P1Y)).astype(np.int) + P1X
else:
slope = dY.astype(np.float32)/dX.astype(np.float32)
if negX:
itbuffer[:,0] = np.arange(P1X-1,P1X-dXa-1,-1)
else:
itbuffer[:,0] = np.arange(P1X+1,P1X+dXa+1)
itbuffer[:,1] = (slope*(itbuffer[:,0]-P1X)).astype(np.int) + P1Y
#Remove points outside of image
colX = itbuffer[:,0]
colY = itbuffer[:,1]
itbuffer = itbuffer[(colX >= 0) & (colY >=0) & (colX<imageW) & (colY<imageH)]
#Get intensities from img ndarray
itbuffer[:,2] = img[itbuffer[:,1].astype(np.uint),itbuffer[:,0].astype(np.uint)]
return itbuffer
def createLineIterator(P1、P2、img):
"""
生成一个数组,该数组由两点之间的直线上每个像素的坐标和强度组成
参数:
-P1:由第一个点(x,y)的坐标组成的numpy阵列
-P2:由第二个点(x,y)的坐标组成的numpy数组
-img:正在处理的图像
返回:
-它:一个numpy数组,由半径中每个像素的坐标和强度组成(形状:[numPixels,3],行=[x,y,强度])
"""
#为可读性定义局部变量
imageH=图像形状[0]
imageW=图像形状[1]
P1X=P1[0]
P1Y=P1[1]
P2X=P2[0]
P2Y=P2[1]
#点间差和绝对差
#用于计算点之间的坡度和相对位置
dX=P2X-P1X
dY=P2Y-P1Y
dXa=np.abs(dX)
dYa=np.abs(dY)
#根据点之间的距离预定义用于输出的numpy数组
itbuffer=np.empty(shape=(np.maximum(dYa,dXa),3),dtype=np.float32)
itbuffer.fill(np.nan)
#使用Bresenham算法获得沿直线的坐标
negY=P1Y>P2Y
negX=P1X>P2X
如果P1X==P2X:#垂直线段
itbuffer[:,0]=P1X
如果是negY:
itbuffer[:,1]=np.arange(P1Y-1,P1Y-dYa-1,-1)
其他:
itbuffer[:,1]=np.arange(P1Y+1,P1Y+dYa+1)
elif P1Y==P2Y:#水平线段
itbuffer[:,1]=P1Y
如果为负:
itbuffer[:,0]=np.arange(P1X-1,P1X-dXa-1,-1)
其他:
itbuffer[:,0]=np.arange(P1X+1,P1X+dXa+1)
其他:#对角线线段
陡坡=dYa>dXa
如果坡度较陡:
斜率=dX.astype(np.float32)/dY.astype(np.float32)
如果是negY:
itbuffer[:,1]=np.arange(P1Y-1,P1Y-dYa-1,-1)
其他:
itbuffer[:,1]=np.arange(P1Y+1,P1Y+dYa+1)
itbuffer[:,0]=(斜率*(itbuffer[:,1]-P1Y)).astype(np.int)+P1X
其他:
斜率=dY.astype(np.float32)/dX.astype(np.float32)
如果为负:
itbuffer[:,0]=np.arange(P1X-1,P1X-dXa-1,-1)
其他:
itbuffer[:,0]=np.arange(P1X+1,P1X+dXa+1)
itbuffer[:,1]=(斜率*(itbuffer[:,0]-P1X)).astype(np.int)+P1Y
#移除图像外部的点
colX=itbuffer[:,0]
colY=itbuffer[:,1]
itbuffer=itbuffer[(colX>=0)和(colY>=0)和(colX编辑:
scikit图像中的函数行可以产生相同的效果,并且比我们可以编写的任何代码都要快
from skimage.draw import line
# being start and end two points (x1,y1), (x2,y2)
discrete_line = list(zip(*line(*start, *end)))
而且timeit的结果也相当快。所以,使用这个
旧的“不推荐”答案:
正如前面的回答所说,它没有实现,所以您必须自己完成。
我不是白手起家的,我只是用一种更奇特、更现代的方式重写了函数的一些部分,它应该正确地处理所有情况,而不是像大多数投票的答案那样对我来说都不正确。我从中吸取了一些例子,做了一些清理和样式设计。
请随意评论。另外,我在源代码中添加了clipline测试,可以在OpenCv 4.x的源代码中找到
谢谢大家的推荐和辛勤工作
def bresenham_三月(img、p1、p2):
x1=p1[0]
y1=p1[1]
x2=p2[0]
y2=p2[1]
#测试是否有坐标位于图像之外
如果(
x1>=图像形状[0]
或x2>=图像形状[0]
或y1>=图像形状[1]
或y2>=图像形状[1]
):#测试线条是否在图像中,这是必要的,因为线条的某些部分必须在内部,它考虑两点在外部的情况
如果不是cv2.clipLine((0,0,*img.shape),p1,p2):
打印(“不在区域内”)
返回
陡峭=数学晶圆(y2-y1)>数学晶圆(x2-x1)
如果陡峭:
x1,y1=y1,x1
x2,y2=y2,x2
#从左到右
同样,_陡峭=x1>x2
如果还需要:
x1,x2=x2,x1
y1,y2=y2,y1
dx=x2-x1
dy=数学晶圆厂(y2-y1)
误差=0.0
delta_误差=0.0
#如果dx为零,则为默认值
如果dx!=0:
delta_error=math.fabs(dy/dx)
如果y1=0.5:
y+=y_步
错误-=1
如果还陡峭:#因为我们改为从左向右走
反向检索()
回程网
这不是一种奇特的方法,而是一种有效且非常简单的单行线:
points_on_line = np.linspace(pt_a, pt_b, 100) # 100 samples on the line
如果要大致获得沿途的每个像素
points_on_line = np.linspace(pt_a, pt_b, np.linalg.norm(pt_a - pt_b))
(例如,采样数作为点A和点B之间的像素数)
例如:
pt_a = np.array([10, 11])
pt_b = np.array([45, 67])
im = np.zeros((80, 80, 3), np.uint8)
for p in np.linspace(pt_a, pt_b, np.linalg.norm(pt_a-pt_b)):
cv2.circle(im, tuple(np.int32(p)), 1, (255,0,0), -1)
plt.imshow(im)
我比较了本页提供的4种方法: 使用Python2.7.6和scikit image 0.9.3,并对代码进行一些小改动。
图像输入通过OpenCV进行。
线段(1,76)到(867190) 方法1:Sci试剂盒图像行
计算时间:0.568毫秒
找到的像素数:867
正确的起始像素:ye
# being start and end two points (x1,y1), (x2,y2)
discrete_line = list(zip(*line(*start, *end)))