使用 cv2 findContours,图像的轮廓显得非常草率。如何提高?
Contours from a image appear very sloppy with cv2 findContours. How to improve?
我正在尝试使用 cv2 查找图像的轮廓。有很多相关的问题,但答案总是显得非常具体,并不适用于我的情况。
我有一张黑白图像,我将其更改为彩色。
thresh = cv2.cvtColor(thresh, cv2.COLOR_RGB2GRAY)
plt.imshow(thresh)
接下来,我试着找出轮廓。
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
然后我通过在黑色背景上绘制它来可视化它。
blank_image = np.zeros((thresh.shape[0],thresh.shape[1],3), np.uint8)
img = cv2.drawContours(blank_image, contours, 0, (255,255,255), 3)
plt.imshow(img)
轮廓遵循实际轮廓,即围绕整个事物。我如何获得这种非常糟糕的油漆印象:
您可以使用 Canny 边缘检测来执行此操作:
import cv2
frame = cv2.imread("iCyrOT3.png") # read a frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # turn it gray
edges = cv2.Canny(gray, 100, 200) # get canny edges
cv2.imshow('Test', edges) # display the result
cv2.waitKey(0)
我正在尝试使用 cv2 查找图像的轮廓。有很多相关的问题,但答案总是显得非常具体,并不适用于我的情况。
我有一张黑白图像,我将其更改为彩色。
thresh = cv2.cvtColor(thresh, cv2.COLOR_RGB2GRAY)
plt.imshow(thresh)
接下来,我试着找出轮廓。
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
然后我通过在黑色背景上绘制它来可视化它。
blank_image = np.zeros((thresh.shape[0],thresh.shape[1],3), np.uint8)
img = cv2.drawContours(blank_image, contours, 0, (255,255,255), 3)
plt.imshow(img)
轮廓遵循实际轮廓,即围绕整个事物。我如何获得这种非常糟糕的油漆印象:
您可以使用 Canny 边缘检测来执行此操作:
import cv2
frame = cv2.imread("iCyrOT3.png") # read a frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # turn it gray
edges = cv2.Canny(gray, 100, 200) # get canny edges
cv2.imshow('Test', edges) # display the result
cv2.waitKey(0)