检测纸牌号码python和opencv

时间:2018-08-22 08:33:00

标签: python opencv

不幸的是,我正在尝试创建纸牌号码检测器。

我创建了一个包含数字图片的文件夹。使用树莓派相机,我拍摄了一张卡片的照片,然后剪出了数字。然后,我将切割的数字与文件夹中的数字图片进行比较。

不幸的是,整个事情无法可靠地进行。我究竟做错了什么 ? 还有其他方法吗?

代码:

def detectCard(self):
        listindex = 0
        cardplace = 1000
        similarity = 0

        while (listindex <= 8):
            # Load image and template
            image = cv2.imread("/home/pi/Desktop/Pythonprojekte/tmp/pycharm_project_712/Cards/picture.png")
            template = cv2.imread(self.cardlink[listindex])


            # Convert to grayscale for faster detection
            imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)


            imageGray = cv2.adaptiveThreshold(imageGray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
            templateGray = cv2.adaptiveThreshold(templateGray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)


            cv2.imwrite("/home/pi/Desktop/Pythonprojekte/tmp/pycharm_project_712/Cards/imageGray.png", imageGray)
            cv2.imwrite("/home/pi/Desktop/Pythonprojekte/tmp/pycharm_project_712/Cards/templateGray.png", templateGray)

            # Analysis
            result = cv2.matchTemplate(imageGray, templateGray, cv2.TM_CCOEFF_NORMED)
            min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
            print("Value at Index ", listindex,"=",max_val)

            if (max_val > similarity):
                cardplace = listindex
                similarity = max_val
                #print("Similarity =", similarity)

            listindex = listindex + 1

            if (listindex == 9):
                print("The Card is in the Index", cardplace)
                print (similarity)

0 个答案:

没有答案