在图像上绘制一条曲线?

5
假设我有一张带有黑色像素的图片。我知道所有黑色像素的坐标,现在我正在寻找黄色线。
已知:我的图片中黑色像素的坐标。
寻找:最适合黑色像素的黄色多项式。 enter image description here
import cv2
import numpy as np

cv2.imread("foo.jpg")
#search for the black pixels and save the coordinates.

#coordinates of all pixels (example)
x = np.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0]) 
y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0]) 
z = np.polyfit(x, y, 2)
p = np.poly1d(z)

如果我理解正确,现在我用 np.poly1d() 创建了一个多项式(图像上的黄线)。但是如何在我的bgr_img上绘制它呢?
编辑:
到目前为止这是我的代码:
import cv2
import numpy as np

img = cv2.imread("white_foo.jpg") #1000x1000 pixel

#lets say these are my black pixels in a white image.
x = np.array([122, 224, 210, 350,  380,  250, 490, 623, 711, 819, 900])
y = np.array([536, 480, 390, 366, 270, 240, 180, 210, 280, 400, 501])

#calculate the coefficients.
z = np.polyfit(x, y, 2)
lspace = np.linspace(0, 1000, 100)

#here I want to draw the polynomial which I calculated with polyfit on my image

cv2.imshow("test", img)
cv2.waitKey(0)


感谢您的提前支持。

请查看此链接 https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html - user8190410
或者,您可以查看我对此问题的回答。 - warped
@user8190410 我查看了文档,但我无法绘制多项式 :/ - jaques
@warped 我总是会遇到很多错误.. :/ - jaques
2
你应该提供一个最小、完整、可验证的示例代码 - https://stackoverflow.com/help/mcve 这意味着我们应该能够运行它。你不能希望将二次多项式拟合到正弦波上。 - Mark Setchell
显示剩余6条评论
2个回答

5
一个快速的谷歌搜索找到了以下内容:https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html#polylines以及这个:Opencv polylines function in python throws exception。无论如何,您需要在感兴趣的点上评估多项式,然后适当地格式化这些点(polylines希望它们在一个int32容器中,格式为[[x_1,y_1],[x_2,y_2],...,[x_n,y_n]])。然后只需调用函数即可。
draw_x = lspace
draw_y = np.polyval(z, draw_x)   # evaluate the polynomial

draw_points = (np.asarray([draw_x, draw_y]).T).astype(np.int32)   # needs to be int32 and transposed

cv2.polylines(img, [draw_points], False, (0,0,0))  # args: image, points, closed, color

enter image description here


1

解决方案1

我会通过创建并重复使用来自matplotlib.pyplot库的AxesSubplot对象来完成它。

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("white_foo.jpg") #1000x1000 pixel

#lets say these are my black pixels in a white image.
y = np.array([122, 224, 210, 350,  380,  250, 490, 623, 711, 819, 900])
x = np.array([536, 480, 390, 366, 270, 240, 180, 210, 280, 400, 501])

# Initilaize y axis
lspace = np.linspace(0,  out_img.shape[0]-1, out_img.shape[0])

#calculate the coefficients.
z = np.polyfit(x, y, 2)

#calculate x axis
line_fitx = z[0]*lspace**2 + z[1]*lspace+ z[2]

# Create axes figure
ax1 = plt.axes()

#Show image
ax1.imshow(out_img, aspect='auto')

#Draw Polynomial over image
ax1.plot(line_fitx,lspace, color='blue');

解决方案2

另一种方法是创建一个白色图像(2D NumPy数组),大小与原始图像相同,并使用绘图位置作为索引给像素上色。如下所示:

verts = np.array(list(zip(lspace.astype(int),line_fitx.astype(int))))
polynom_image[tuple(verts.T)] = [255,0,0]

# Create axes figure
ax2 = plt.axes()
#Show image
ax2.imshow(polynom_image)

然后可以使用cv2.addWeighted函数将它们合并成一张图片。

result = cv2.addWeighted(polynom_image, 1, und_images[0], 0.5, 0) 

我看到这第二种方法的缺点是对于绘图,特别是线条宽度方面缺乏控制。
解决方案3
使用cv2.polylines函数。请注意,它会修改原始的img(numpy数组)。
verts = np.array(list(zip(line_fitx.astype(int),lspace.astype(int))))
cv2.polylines(img,[verts],False,(0,255,255),thickness=3)
ax2 = plt.axes()
ax2.imshow(img)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接