简体   繁体   English

无法使我的多色检测阵列正常工作(Python和OpenCV)

[英]Can't get my multicolor detection array to work(Python & OpenCV)

I'm trying to use a numpty array to store certain colors (HSV values) in order to be able to detect them later on by using tests. 我正在尝试使用一个numpty数组来存储某些颜色(HSV值),以便以后可以通过使用测试来检测它们。 However, when I run my test it doesn't succeed. 但是,当我运行测试时不会成功。 Here's the code: 这是代码:

#!/usr/bin/env python
import cv2
import numpy as np

class DetectAColor(object):

    @staticmethod
    def detect_a_color(image):
        img_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

        boundaries = {
            'red': ([0, 100, 100],[10, 255, 255]),
            'brown': ([14, 100, 100],[15, 255, 255]),
            'orange': ([16, 100, 100],[17, 255, 255]),
            'yellow': ([29, 100, 100],[31, 255, 255]),
            'green': ([50, 100, 100],[60, 255, 255]),
            'lightblue': ([50, 100, 100],[60, 255, 255]),
            'darkblue': ([115, 100, 100],[125, 255, 255]),
            'purple': ([149, 150, 100],[150, 255, 255]),
            'pink': ([149, 100, 150],[149, 100, 150])
            }

        for k,v in boundaries.iteritems():
            lower_color = np.array(v[0])
            upper_color = np.array(v[1])

            mask = cv2.inRange(img_hsv, lower_color, upper_color)
            amount_not_zero = cv2.countNonZero(mask)
            if amount_not_zero > 9000:
                return k
            else:
                return "Not found"

I'm testing this code with this simple test (and display the image to be 100% sure): 我正在使用此简单测试来测试此代码(并以100%确定的方式显示图像):

def test_detect_red(self):
    image = np.zeros((512, 512, 3), np.uint8)
    image[:,0:512] = (0, 0, 255) # B G R values
    color = DetectAColor.detect_a_color(image)
    cv2.imshow('image',image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # Controle dat de kleur gevonden wordt
    self.assertEqual("red", color)

I find it quite odd that this code isn't working, since at first it was working using the code to just detect 1 color: 我发现此代码无法正常工作很奇怪,因为起初它是使用代码来检测一种颜色的:

#!/usr/bin/env python
import cv2
import numpy as np

class DetectAColor(object):

    @staticmethod
    def detect_a_color(image):
        img_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        TRACK_COLOR_MIN = np.array([0, 100, 100], np.uint8)
        TRACK_COLOR_MAX = np.array([15, 255, 255], np.uint8)

        mask = cv2.inRange(img_hsv, TRACK_COLOR_MIN, TRACK_COLOR_MAX)

        cv2.imshow('mask',mask)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

        amount_not_zero =  cv2.countNonZero(mask)
        if amount_not_zero > 9000:
            return "Found"
        else:
            return "Not found"

I understand the the value for H is a bit different, but I tried it with the same value as in the 1 color code and it failed as well. 我知道H的值有些不同,但是我尝试使用与1颜色代码相同的值进行了尝试,但它也失败了。 We've already calculated the values we needed and tested them with the 1 color method and it worked. 我们已经计算了所需的值,并使用1色方法对其进行了测试,并且该方法有效。 So I need to know why I can't get it to work using the array and for loop. 所以我需要知道为什么不能使用数组和for循环来使其工作。

The problem is solved by getting rid of the else statement within the for loop. 通过摆脱for循环中的else语句,可以解决该问题。 Put the return "Not found" statement outside the loop. 将返回“ Not found”语句放在循环外部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM