在两个给定点之间画线(OpenCV,Python)
Draw line between two given points (OpenCV, Python)
我现在已经为这个问题苦苦挣扎了一个小时...
我有一张图片,里面有一个矩形:
这是我为寻找角点而编写的代码:
import cv2
import numpy as np
img = cv2.imread('rect.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
points = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
points = np.int0(points)
for point in points:
x, y = point.ravel()
cv2.circle(img, (x, y), 3, (0, 255, 0), -1)
print(points[0])
print(points[1])
print(points[2])
print(points[3])
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.imwrite('rect.png', img)
这是结果:
如您所见,它运行完美。我想要的是沿着 upper/lower 点 (x1,x2 - x3,x4) 画一条线。
我从现在开始制作的是这个...
cv2.line(img, (points[0]), (points[1]), (0, 255, 0), thickness=3, lineType=8)
cv2.imshow('img', img)
cv2.waitKey(0)
但是不行。
有什么想法吗?
结果应该是这样的:
这两条线必须经过点的坐标。 print(points[0])
上面给出了下一个输出,例如:
[[561 168]]
[[155 168]]
[[561 53]]
[[155 53]]
谢谢
points = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
points = np.int0(points).reshape(-1,2)
for point in points:
x, y = point.ravel()
cv2.circle(img, (x, y), 3, (0, 255, 0), -1)
y1 = min(points[:,1])
y2 = max(points[:,1])
## small and big enough
cv2.line(img, (0, y1), (1000, y1), (0, 255, 0), thickness=3, lineType=8)
cv2.line(img, (0, y2), (1000, y2), (0, 255, 0), thickness=3, lineType=8)
所以首先,让我们看看你的印刷品,它说 points[0] 是
[[561 168]]
但是opencv点像
(561, 168)
你可以像处理圆一样解压它,然后做元组
x, y = points[0].ravel()
(x,y)
或者您可以使用
tuple(points[0].ravel())
或
tuple(points[0][0])
编辑
你想从屏幕的一侧到另一侧,那也很容易。您需要做的是在一个点将 x 值更改为 0,在另一点将列值更改为 0。我认为最简单的方法是这样做:
y = points[0].ravel()[1]
cv2.line(img, (0, y), (img.shape[1], y), (0, 255, 0), thickness=3, lineType=8)
这里要注意两点:
如你所见,我并不关心第二点,因为我假设
它会在同一条水平线上,如果不是,它会得到一个
稍微复杂一点,但并不难。
img.shapereturns
包含图像细节的元组(行、列、通道),因为我们
需要我们采取的列 [1].
我现在已经为这个问题苦苦挣扎了一个小时...
我有一张图片,里面有一个矩形:
这是我为寻找角点而编写的代码:
import cv2
import numpy as np
img = cv2.imread('rect.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
points = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
points = np.int0(points)
for point in points:
x, y = point.ravel()
cv2.circle(img, (x, y), 3, (0, 255, 0), -1)
print(points[0])
print(points[1])
print(points[2])
print(points[3])
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.imwrite('rect.png', img)
这是结果:
如您所见,它运行完美。我想要的是沿着 upper/lower 点 (x1,x2 - x3,x4) 画一条线。
我从现在开始制作的是这个...
cv2.line(img, (points[0]), (points[1]), (0, 255, 0), thickness=3, lineType=8)
cv2.imshow('img', img)
cv2.waitKey(0)
但是不行。
有什么想法吗?
结果应该是这样的:
这两条线必须经过点的坐标。 print(points[0])
上面给出了下一个输出,例如:
[[561 168]]
[[155 168]]
[[561 53]]
[[155 53]]
谢谢
points = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
points = np.int0(points).reshape(-1,2)
for point in points:
x, y = point.ravel()
cv2.circle(img, (x, y), 3, (0, 255, 0), -1)
y1 = min(points[:,1])
y2 = max(points[:,1])
## small and big enough
cv2.line(img, (0, y1), (1000, y1), (0, 255, 0), thickness=3, lineType=8)
cv2.line(img, (0, y2), (1000, y2), (0, 255, 0), thickness=3, lineType=8)
所以首先,让我们看看你的印刷品,它说 points[0] 是
[[561 168]]
但是opencv点像
(561, 168)
你可以像处理圆一样解压它,然后做元组
x, y = points[0].ravel()
(x,y)
或者您可以使用
tuple(points[0].ravel())
或
tuple(points[0][0])
编辑
你想从屏幕的一侧到另一侧,那也很容易。您需要做的是在一个点将 x 值更改为 0,在另一点将列值更改为 0。我认为最简单的方法是这样做:
y = points[0].ravel()[1]
cv2.line(img, (0, y), (img.shape[1], y), (0, 255, 0), thickness=3, lineType=8)
这里要注意两点:
如你所见,我并不关心第二点,因为我假设 它会在同一条水平线上,如果不是,它会得到一个 稍微复杂一点,但并不难。
img.shapereturns 包含图像细节的元组(行、列、通道),因为我们 需要我们采取的列 [1].