首页 > 解决方案 > 分类黑色和银色图像

问题描述

我正在努力对黑色和银色的图像进行分类 如何使用 OpenCV 对它们进行分类?黑色图像 银色图像?下面有什么错误?

import numpy as np
import cv2


# RGB color boundaries
black = ([0, 0, 0], [50, 50, 50])

boundaries = [black]

# 
# Load an color image in grayscale
img = cv2.imread('zmFf4.jpg')
print(img.shape)

breakpoint()
for (lower, upper) in boundaries:

    # create NumPy arrays from the boundaries
    lower = np.array(lower, dtype="uint8")
    upper = np.array(upper, dtype="uint8")


    # find the colors within the specified boundaries and apply
    # the mask
    mask = cv2.inRange(img, lower, upper)
    output = cv2.bitwise_and(img, img, mask=mask)

    # show the images
    cv2.imshow("images", np.hstack([img, output]))
    cv2.waitKey(0)

标签: pythonopencvimage-processing

解决方案


你不是在寻找银色范围做如下

import numpy as np
import cv2


# RGB color boundaries
black = ([0, 0, 0], [50, 50, 50])
silver = ([192, 192, 192], [212, 212, 212])
boundaries = [black, silver]

#
# Load an color image in grayscale
img = cv2.imread('zmFf4.jpg')
print(img.shape)

breakpoint()
for (lower, upper) in boundaries:

    # create NumPy arrays from the boundaries
    lower = np.array(lower, dtype="uint8")
    upper = np.array(upper, dtype="uint8")


    # find the colors within the specified boundaries and apply
    # the mask
    mask = cv2.inRange(img, lower, upper)
    output = cv2.bitwise_and(img, img, mask=mask)

    # show the images
    cv2.imshow("images", np.hstack([img, output]))
    cv2.waitKey(0)


推荐阅读