【问题标题】:OpenCV color detection without knowing colors a priori在不知道颜色的情况下进行 OpenCV 颜色检测
【发布时间】:2018-02-21 16:27:38
【问题描述】:

我想在一些颜色斑点周围绘制边界框,我事先不知道它们的颜色。图像如下所示:

场景中的每种颜色代表一个不同的对象。我已经在图像的灰度版本上尝试了 findContours,但是如果它们重叠,则以这种方式获得的轮廓包含多个对象。我的愿望是获得单个对象的轮廓,或者如果一个对象被场景中的另一个对象分割,则获得多个轮廓。有没有办法在 OpenCV 中实现这一点? 非常感谢您的关注和时间!

编辑:按照建议,这里是我的代码

img = cv2.imread(img_path)

imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

kernel = np.ones((5,5), np.uint8)

im2, contours, hierarchy = cv2.findContours(imgray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

bboxes = []

for c in contours:
    x, y, w, h = cv2.boundingRect(c)
    M = cv2.moments(c)
    if M["m00"]:
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])
        area = cv2.contourArea(c)
        if area >= 25:
            colorHash = img[cy, cx]
            bboxes.append((Box(Point(x, y), Point(x+w, y+h)), colorHash, area))
            cv2.drawContours(img, [c], -1, (0, 0, 255), 1)

cv2.imshow("Image", img)
cv2.waitKey(0)   

return bboxes, contours

这是我要解决的问题的图像(标记为蓝色,轮廓为红色,对象应具有单独的轮廓)

【问题讨论】:

  • 如果物体的颜色不同,可以将每个颜色分量视为独立的obj。

标签: python image opencv image-processing


【解决方案1】:

您可以对图像的 HSV 颜色空间进行光栅扫描,并根据其色调值对每个像素进行分类,并考虑一定的色调范围。 然后使用不同的色调值类对图像进行遮罩,从而分割不同颜色的每个单独对象。

【讨论】:

  • 感谢您的快速回答!我已经有了这样的解决方案,但是我想通过在对象被图像中的其他东西分割的情况下获得单独的边界框来改进它(例如,当被遮挡时,遮挡物左侧可见手臂和身体的其余部分在它的右边)。在这种情况下,我想要对象的非连续部分使用单独的边界框。我希望在 openCV 中有一个舒适的解决方案
  • 如果是不连续的对象(具有相同色调值的多个部分),您将在基于色调的蒙版图像中获得多个连接组件,因此您可以合并这些连接组件的边界框以获得单个边界框.
【解决方案2】:
  1. 请同时发布您的代码,以便我们查看错误所在或为您的方法提供更好的建议。
  2. 以下代码为图像中的各个对象绘制轮廓:

`

# -i : image path

# import stuff
import numpy as np
import argparse
import cv2

# Construct argument 
ap = argparse.ArgumentParser()
ap.add_argument( "-i", "--image", required = True, help = "Path to the image")
ap.add_argument("-t", "--threshold", type = int, default = 100, help = "Enter threshold value")
args = vars(ap.parse_args())

# load image / grayscale
image = cv2.imread(args["image"])
print (image.shape)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

kernel = np.ones((5,5), np.uint8)
# blur the image for a mask
img = cv2.erode(gray, kernel, iterations=1)
img = cv2.dilate(img, kernel, iterations=1)
blurred = cv2.blur(img, (3,3))

# Threshold image to segment the objects
# requires grayscale image
methods = [
    ("THRESH_BINARY", cv2.THRESH_BINARY)
    #("THRESH_BINARY_INV", cv2.THRESH_BINARY_INV)
    ]

# loop for each threshold method
# (T, threshImage) = cv2.threshold(src, thresh, maxval, type)
for (threshName, threshMethod) in methods:
    (T, threshImage) = cv2.threshold(blurred, args["threshold"], 255, threshMethod)
    cv2.namedWindow(threshName,cv2.WINDOW_NORMAL)
    cv2.resizeWindow(threshName, 600,600)
    cv2.imshow(threshName, threshImage)
    cv2.waitKey(0)

# # Adaptive thresholding
# adaptiveThresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 4)

# Crop target image using thresh as mask
masked = cv2.bitwise_and(gray, gray, mask = threshImage)
# cv2.imshow("Mased Image", masked)
# cv2.waitKey(0)

# find contours
(_, cnts, _) = cv2.findContours(masked.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print (" No. of Contours ={}".format(len(cnts)))

# draw the contours on top of the original image
drops = image.copy()
cv2.drawContours(drops, cnts, -1, (0, 255, 0), 2)
cv2.namedWindow("Contours",cv2.WINDOW_NORMAL)
cv2.resizeWindow("Contours", 600,600)
cv2.imshow("Contours", drops)
cv2.waitKey(0)

cv2.imwrite('contours.png',drops)

【讨论】:

  • 感谢您的回答,我已编辑问题以提供我的代码。
【解决方案3】:

两种方法:

  • 像您一样使用轮廓绘制方法并保留斑点(轮廓中的任何像素)以及边界框的颜色。然后合并相同颜色的边界框。这可以在按颜色索引的边界框字典的帮助下方便地完成。

  • 扫描图像,从空字典开始。每次遇到非黑色像素时,查找其颜色并创建或更新边界框。

请注意,无需转换为其他颜色系统,保留原始 RGB。

【讨论】:

    猜你喜欢
    • 2011-11-18
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 2014-03-15
    • 2018-01-09
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多