Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 有没有办法增加cv2.findContours返回的点数?_Python 3.x_Opencv_Computer Vision_Contour_Image Segmentation - Fatal编程技术网

Python 3.x 有没有办法增加cv2.findContours返回的点数?

Python 3.x 有没有办法增加cv2.findContours返回的点数?,python-3.x,opencv,computer-vision,contour,image-segmentation,Python 3.x,Opencv,Computer Vision,Contour,Image Segmentation,对于一个我尝试匹配拼图的项目,我编写了一些代码来确定拼图每一面的缩进(向内和向外)。我添加了两个显示结果的示例图像 所有缩进都能很好地检测到,但其准确性并不总是如预期的那样。我认为这是由于cv2.findContours()只提供了有限的点数。我提供了cv2.findContours()的代码 我选择只使用带有RETR_external的外部线条,因为这些是唯一需要的轮廓。我还决定用CHAIN_About_NONE保存每个点,尽管我并不需要关于侧面直线部分的大量信息。但是,CHAIN_Abr

对于一个我尝试匹配拼图的项目,我编写了一些代码来确定拼图每一面的缩进(向内和向外)。我添加了两个显示结果的示例图像

所有缩进都能很好地检测到,但其准确性并不总是如预期的那样。我认为这是由于cv2.findContours()只提供了有限的点数。我提供了cv2.findContours()的代码

我选择只使用带有RETR_external的外部线条,因为这些是唯一需要的轮廓。我还决定用CHAIN_About_NONE保存每个点,尽管我并不需要关于侧面直线部分的大量信息。但是,CHAIN_Abrox_SIMPLE还删除了关键(压痕)部分的一些点,从而进一步降低了精度

第一个函数getImageShape以类似的方式获取形状,但我发现我提前使用它获得了更好的结果,而不是在以前的代码中将源图像作为cv2.threshold函数的输入参数

def getImageShape(img):
    gray   = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray   = cv2.medianBlur(gray, ksize=5)
    thresh = cv2.threshold(gray, 0, 254, cv2.THRESH_BINARY)[1]
    thresh = cv2.blur(thresh, ksize=(3, 3))
    return thresh
由于精度不高(拼图一侧的宽度/高度误差约为4%),因此仅根据压痕位置将两侧相互匹配会导致多个“可能”解决方案。我希望有一种方法可以增加cv2.getContours返回的点数,但在openCV文档中没有找到任何相关信息

如果这是不可能的,我将不得不添加一些图像连续性检测算法(但这将使其无法仅通过形状匹配片段)


提前谢谢

如果我可以建议一个替代方案: 您可以尝试通过使用
Sobel
Prewitt
过滤器计算图像梯度,并在执行一些简单的后处理后使用
Canny
边缘检测,使轮廓可见

这里是我使用它得到的,注意我使用了你提供的图片,轮廓上有标记,所以忽略这一点不一致


我尝试了你的代码,它确实更有效。我还发现findContours实际上返回了足够的点数。问题是有多个相邻点被视为“最大值”,而我的算法只是取第一个而不是中间的一个。谢谢你的帮助!
def getImageShape(img):
    gray   = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray   = cv2.medianBlur(gray, ksize=5)
    thresh = cv2.threshold(gray, 0, 254, cv2.THRESH_BINARY)[1]
    thresh = cv2.blur(thresh, ksize=(3, 3))
    return thresh