OpenCV inRange function detailed explanation

        This article is the sixth article on the road to getting started with OpenCV image vision. I have solved the RGB to HSV conversion in detail. HSV uses AI to perform the range value operation of the HSV to inRange() function, which simply and comprehensively solves the problem of OpenCV for a certain value in the image. Color analysis work, this article writes sample programs and blogs by identifying red and blue areas, and also describes various operations, such as: RGB to HSV, conversion tools, formula conversion , code implementation , the use of ChatGPT in Yuanruyi, Identify red areas, adjust V brightness , adjust S saturation, adjust H hue, identify blue areas, etc.

        In the future, AI believes that it will appear more frequently in our daily work, and it can also help us improve work and study efficiency. Currently, the ChatGPT domestic server in VSCode has been discontinued. You can download Yuanruyi to experience using ChatGPT!

The author of this article is original, and reprinting is prohibited without permission.

OpenCV inRange function use detailed directory

1 RGB to HSV

1.1 Conversion tool

1.2 Formula conversion

2 code implementation

3 ChatGPT

4 Identify red areas

4.1 Red Range

4.2 V brightness adjustment

4.3 S saturation adjustment

4.4 H tone adjustment

5 Identify the blue area

5.1 Blue Range

5.2 Recognize blue

5.3 Recognizing blue


1 RGB to HSV

        Recently, I have been looking at the relevant parts of OpenCV image recognition. When I saw the inRange function, I was a little bit confused. The main reason is that the HSV value of a certain color in the HSV color area is completely confused. Looking at other blogs and articles is a funnel. The range of values ​​​​in the figure is completely ignorant.

1.1 Conversion tool

        The most important thing for a programmer is the idea and method of solving the problem. Here is the conversion tool of the rookie tutorial, which is very convenient, and there is also a corresponding comparison table:

RGB HSV Conversion | Novice Tools

1.2 Formula conversion

rgb 221,0,27 可以转换成 hsv 的格式,具体的方法如下:

首先,我们需要将 rgb 值转换成浮点数,方法是将每个值除以 255。因此,rgb(221,0,27) 可以转换成 (0.8666666666666667, 0.0, 0.10588235294117647)。

然后,我们可以使用以下公式来计算 hsv 值:

h = 色相,s = 饱和度,v = 明度

maxc = max(r, g, b)
minc = min(r, g, b)

if maxc == minc:
h = 0
elif maxc == r:
h = 60 * ((g - b) / (maxc - minc))
elif maxc == g:
h = 60 * (2 + (b - r) / (maxc - minc))
else:
h = 60 * (4 + (r - g) / (maxc - minc))

if h < 0:
h += 360

s = 0 if maxc == 0 else (1 - minc / maxc)
v = maxc

所以,rgb(221,0,27) 可以转换成 hsv(348.57142857142856, 1.0, 0.8666666666666667)。

注意:在计算 hsv 值时,色相 h 的单位是角度,饱和度 s 和明度 v 的单位都是百分比,它们的取值范围分别是 0 到 360、0 到 1 和 0 到 1。

2 code implementation

import cv2
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
    image = cv2.imread('D:/Jupyter_Notebooks/3.png')

    # 从RGB色彩空间转换到HSV色彩空间
    hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

    # 颜色范围下限
    lower_threshold = np.array([110, 254, 220])
    # 颜色范围上限
    upper_threshold = np.array([180, 255, 255])

    # 使用inRange函数检测颜色
    mask = cv2.inRange(hsv, lower_threshold, upper_threshold)

    # 对原图像和掩码进行位运算
    result = cv2.bitwise_and(image, image, mask=mask)

    # H、S、V范围二:
    cv2.imshow("result", mask)
    cv2.imshow("image", image)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

3 ChatGPT

        ChatGPT is used here. I am very grateful that the Yuanruyi tool can integrate ChatGPT and it can still be used at present (VSCode domestic version of ChatGPT can no longer be connected) , and AI programming needs to be learned in the future.

4 Identify red areas

        Here I intercepted a part of the application on the desktop and recognized that the RGB red color is 221,0,27. I first asked AI HSV to RGB after the rookie tutorial RGB to HSV color, and it answered H 0-180 S, V 0-255

4.1 Red Range

        We mainly want to identify the red color of Netease Cloud and Youdao. This is not ideal, so we need to adjust the value range.

    # 颜色范围下限
    lower_threshold = np.array([0, 255, 255])
    # 颜色范围上限
    upper_threshold = np.array([180, 255, 255])

4.2 V brightness adjustment

        Because our color is taken from Netease Cloud, so if we want to see Youdao at the same time, we need to adjust the S saturation

    # 颜色范围下限
    lower_threshold = np.array([0, 255, 200])
    # 颜色范围上限
    upper_threshold = np.array([180, 255, 255])

4.3 S saturation adjustment

        We have adjusted the V and S values, and found that the color of the tinder is also within the value range, so now we need to adjust the range of H to remove the yellow-red color of the tinder

    # 颜色范围下限
    lower_threshold = np.array([0, 254, 200])
    # 颜色范围上限
    upper_threshold = np.array([180, 255, 255])

4.4 H tone adjustment

        It can be found that the recognition of the red area of ​​Youdao NetEase Cloud has been perfectly realized now.

    # 颜色范围下限
    lower_threshold = np.array([120, 254, 200])
    # 颜色范围上限
    upper_threshold = np.array([180, 255, 255])

5 Identify the blue area

        Identify the blue color range of Tencent conference as RGB(3,131,254), and get 0.5817, 0.9882, 0.9961 ChatGPT conversion by converting to HSV

5.1 Blue Range

        We mainly want to identify Tencent Conference and Tencent Weiyun.
 

    # 颜色范围下限
    lower_threshold = np.array([104, 250, 250])
    # 颜色范围上限
    upper_threshold = np.array([105, 255, 255])

5.2 Recognize blue

        After adjustment, it can be seen that the Tencent meeting and some ToDesk have the same color, but they are all filtered out, and the redundant part can be intercepted by the ROI interception function.

OpenCV ROI interception, setting, saving_ Gemini Breakpoint Blog-CSDN Blog_opencv interception roi

    # 颜色范围下限    210
    lower_threshold = np.array([10, 250, 252])
    # 颜色范围上限
    upper_threshold = np.array([20, 255, 255])

5.3 Recognizing blue

        You can see that some of the noises that are finally recognized are still possible and can be filtered.

    # 颜色范围下限    210
    lower_threshold = np.array([10, 210, 255])
    # 颜色范围上限
    upper_threshold = np.array([20, 215, 255])

 

Guess you like

Origin blog.csdn.net/qq_37529913/article/details/128415136