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:检测黑色方块_Python_Opencv_Image Processing - Fatal编程技术网

Python:检测黑色方块

Python:检测黑色方块,python,opencv,image-processing,Python,Opencv,Image Processing,我在试着探测黑方块 这是我目前的代码 frame=cv2.imread('squares.jpg') img=cv2.GaussianBlur(frame, (5,5), 0) img=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower=np.array([0, 0, 0],np.uint8) upper=np.array([10, 50, 50],np.uint8) separated=cv2.inRan

我在试着探测黑方块

这是我目前的代码

    frame=cv2.imread('squares.jpg')
    img=cv2.GaussianBlur(frame, (5,5), 0)

    img=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower=np.array([0, 0, 0],np.uint8)
    upper=np.array([10, 50, 50],np.uint8)
    separated=cv2.inRange(img,lower,upper)


    #this bit draws a red rectangle around the detected region
    contours,hierarchy=cv2.findContours(separated,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    max_area = 0
    largest_contour = None
    for idx, contour in enumerate(contours):
        area = cv2.contourArea(contour)
        if area > max_area:
            max_area = area
            largest_contour=contour
            if not largest_contour==None:
                moment = cv2.moments(largest_contour)
                if moment["m00"] > 1000:
                    rect = cv2.minAreaRect(largest_contour)
                    rect = ((rect[0][0], rect[0][1]), (rect[1][0], rect[1][1]), rect[2])
                    (width,height)=(rect[1][0],rect[1][1])
                    print str(width)+" "+str(height)
                    box = cv2.cv.BoxPoints(rect)
                    box = np.int0(box)
                    if(height>0.9*width and height<1.1*width):
                            cv2.drawContours(frame,[box], 0, (0, 0, 255), 2)

    cv2.imshow('img',frame)
我就是不能让黑人或白人去工作


有什么想法吗?

这里的关键直觉是,黑色位于图像中所有色调和饱和度值,但仅位于低值。我发现一个下限
[0,0,0]
和一个上限
[180,255,50]
将定位黑色方块,如下所示:

我还应该提到,由于以下几个原因,您的方法无法找到白色方块:

  • 存在多个白色正方形。您的方法仅选择最大轮廓,这意味着每种颜色只能检测到一个正方形
  • 很难区分“白色方块”和打印它们的纸张的颜色。你可能会得到一个单一的轮廓连接广场和纸条的两侧

我想你可以尝试切换黑白!我对opencv没有太多经验,但黑色的值应该大致为(假设图片为8位):上限:
[180,10,50]
,下限:
[0,0,0]
;白色:
[180,10255]
,下部:
[0,0205]
。微调是必要的
colours=['yellow','orange','red','green','black','white']
uppers=[[20,100,100],[5,100,100],[0,100,100],[???,???,???],[???,???,???]]
lowers=[[30,255,255],[15,255,255],[6,255,255],[???,???,???],[???,???,???]]