Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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 想要检测拼图中的边缘和角落部分,但可以';t找不到每件作品的4个角_Python_Opencv_Image Processing_Scikit Image_Ndimage - Fatal编程技术网

Python 想要检测拼图中的边缘和角落部分,但可以';t找不到每件作品的4个角

Python 想要检测拼图中的边缘和角落部分,但可以';t找不到每件作品的4个角,python,opencv,image-processing,scikit-image,ndimage,Python,Opencv,Image Processing,Scikit Image,Ndimage,我有一个拼图游戏,想自动区分拼图中的“正常”、“边缘”和“角”部分(我希望这些单词的定义对于任何曾经做过拼图游戏的人来说都是显而易见的) 为了让事情更简单,我从9个部分开始选择,其中4个是正常的,4个是边,一个是角。原始图像如下所示: 我现在的第一个想法是检测每个单件的4个“主要角”,然后按照以下步骤进行: 如果两个相邻的“主要角”之间的轮廓是一条直线,则为边 如果三个相邻的“主角”之间的两个轮廓是直线,则为角 如果两个相邻的“主角”之间没有直线,则为正常零件 但是,我在提取每件作品的四个

我有一个拼图游戏,想自动区分拼图中的“正常”、“边缘”和“角”部分(我希望这些单词的定义对于任何曾经做过拼图游戏的人来说都是显而易见的)

为了让事情更简单,我从9个部分开始选择,其中4个是正常的,4个是边,一个是角。原始图像如下所示:

我现在的第一个想法是检测每个单件的4个“主要角”,然后按照以下步骤进行:

  • 如果两个相邻的“主要角”之间的轮廓是一条直线,则为边
  • 如果三个相邻的“主角”之间的两个轮廓是直线,则为角
  • 如果两个相邻的“主角”之间没有直线,则为正常零件
但是,我在提取每件作品的四个“主要角点”时遇到问题(我尝试使用Harris角点)

下面附上我的代码,包括一些预处理,以及一些结果,包括我得到的Harris角点。欢迎您的任何意见

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

img = cv2.imread('image.png')
gray= cv2.imread('image.png',0)

# Threshold to detect rectangles independent from background illumination
ret2,th3 = cv2.threshold(gray,220,255,cv2.THRESH_BINARY_INV)

# Detect contours
_, contours, hierarchy = cv2.findContours( th3.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# Draw contours
h, w = th3.shape[:2]
vis = np.zeros((h, w, 3), np.uint8)
cv2.drawContours( vis, contours, -1, (128,255,255), -1)

# Print Features of each contour and select some contours
contours2=[]
for i, cnt in enumerate(contours):
    cnt=contours[i]
    M = cv2.moments(cnt)

    if M['m00'] != 0:
        # for definition of features cf http://docs.opencv.org/3.1.0/d1/d32/tutorial_py_contour_properties.html#gsc.tab=0
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])
        area = cv2.contourArea(cnt)
        x,y,w,h = cv2.boundingRect(cnt)
        aspect_ratio = float(w)/h
        rect_area = w*h
        extent = float(area)/rect_area        

        print i, cx, cy, area, aspect_ratio, rect_area, extent

        if area < 80 and area > 10:
            contours2.append(cnt)

# Detect Harris corners
dst = cv2.cornerHarris(th3,2,3,0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None, iterations=5)

# Threshold for an optimal value, it may vary depending on the image.
harris=img.copy()
print harris.shape
harris[dst>0.4*dst.max()]=[255,0,0]

titles = ['Original Image', 'Thresholding', 'Contours', "Harris corners"]
images = [img, th3, vis, harris]
for i in xrange(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()
导入cv2
将numpy作为np导入
从matplotlib导入pyplot作为plt
img=cv2.imread('image.png')
灰色=cv2.imread('image.png',0)
#用于检测与背景照明无关的矩形的阈值
ret2,th3=cv2.阈值(灰色,220255,cv2.阈值\u二进制\u INV)
#检测轮廓
_,轮廓,层次=cv2.findContours(th3.copy(),cv2.RETR\u外部,cv2.CHAIN\u近似值\u无)
#画轮廓
h、 w=th3.形状[:2]
vis=np.zero((h,w,3),np.uint8)
cv2.等高线图(vis,等高线-1,(128255255),-1)
#打印每个轮廓的特征并选择一些轮廓
轮廓2=[]
对于i,枚举中的cnt(等高线):
cnt=等高线[i]
M=cv2.力矩(cnt)
如果M['m00']!=0:
#有关功能的定义,请参见http://docs.opencv.org/3.1.0/d1/d32/tutorial_py_contour_properties.html#gsc.tab=0
cx=int(M['m10']/M['m00']
cy=int(M['m01']/M['m00']
面积=cv2。轮廓面积(cnt)
x、 y,w,h=cv2.boundingRect(cnt)
纵横比=浮动(宽)/h
矩形面积=w*h
范围=浮动(面积)/矩形面积
打印i、cx、cy、面积、纵横比、矩形面积、范围
如果面积<80且面积>10:
轮廓2.附加(cnt)
#检测Harris角点
dst=cv2.0(th3,2,3,0.04)
#标记拐角时,结果被放大,这并不重要
dst=cv2.扩张(dst,无,迭代次数=5)
#阈值为最佳值,它可能因图像而异。
harris=img.copy()
打印harris.shape
harris[dst>0.4*dst.max()]=[255,0,0]
标题=[‘原始图像’、‘阈值’、‘轮廓’、‘哈里斯角点’]
图像=[img,th3,vis,harris]
对于X范围内的i(4):
plt.subplot(2,2,i+1),plt.imshow(图像[i],'gray')
标题(标题[i])
plt.xticks([]),plt.yticks([])
plt.show()

我想你可以使用旋转的矩形轮廓。你事先知道所有不同形状和大小的零件吗?如果是这样的话,检测碎片,标准化它们的方向,然后进行模板匹配。如果你在4年后仍然对这个问题感兴趣,这可能会很有用:@AndréBaptista Cool,这看起来是关于这个主题的非常深入的工作。我们必须更详细地研究这个问题。非常感谢。