当前位置:网站首页>[200 opencv routines] 209 Color image segmentation in HSV color space

[200 opencv routines] 209 Color image segmentation in HSV color space

2022-06-24 21:58:00 Xiaobai youcans

OpenCV routine 200 piece General catalogue


【youcans Of OpenCV routine 200 piece 】209. HSV Color image segmentation in color space

5.1 HSV Color image segmentation in color space

HSV Model is a kind of color model for users' perception .

HSV Each channel of the color space represents the hue (Hue)、 saturation (Saturation) And lightness (Value), Can intuitively express the brightness of color 、 Hue and brightness .

HSV Color space can be described by a cone space model . At the apex of a cone V=0,H and S No definition , For black ; At the center of the top surface of the cone V=max,S=0,H No definition , For white .

When S=1, V=1 when ,H Any color represented is called solid color ; When S=0 when , The saturation is 0, Lightest color , The lightest is described as gray , The brightness of gray is determined by V decision , here H meaningless ; When V=0 when , Darkest color , The darkest is described as black , here H and S It doesn't make sense , It is black in any case .

 Insert picture description here

The basic property of color is hue , Represent different colors , It can be used to describe and identify a certain color . for example , Green in HSV The range in space is H=35~77, And in the RGB Space is difficult to describe with expressions . Therefore, we often use HSV Color space is used to identify certain colors and compare different colors .

HSV The model is very effective in dividing the specified color . use H and S Component represents color distance , Color distance refers to the numerical difference between two colors . For different color areas , blend H And S Variable , Set a threshold , You can perform simple segmentation .


The function prototype

function inRange () Can be achieved by color area [lowerb,upperb] Binary segmentation of the image .

cv.inRange(src, lowerb, upperb[, dst]) → dst

function inRange() Check whether the array elements are within the set interval , Usually used in HSV The color range set by the space check . If a pixel value of the image is in [lowerb,upperb] Between , The output pixel value is set to 255, Otherwise, set 0.

Parameter description :

  • src: The input image ,nparray Array , Allow single channel or multi-channel images
  • lowerb: Lower boundary threshold , Scalar (src It's a single channel ) Or an array (src Multi channel )
  • upperb: Upper boundary threshold , Scalar (src It's a single channel ) Or an array (src Multi channel )
  • dst: Output image , Single channel binary image , Size and src identical , Depth is CV_8U

matters needing attention :

  1. The input image can be a single channel grayscale image , It can also be a multi-channel color image .
  2. Whether the input image is a single channel or a multi-channel image , The output images are all single channel binary images , Equivalent to the black and white mask of the input image mask.
  3. When the input image is a single channel grayscale image ,lower、upper For the scalar ; When the input image is a multi-channel color image ,lower, upper Is an array , The array length is the same as the number of channels , Represent the boundary threshold of each channel respectively .
  4. When the input image is multi-channel , Only if the values of each channel of the pixel are all in [lowerb(i),upperb(i)] The output is only when 255:

KaTeX parse error: Undefined control sequence: \and at position 39: …I_0) \lt u_0] \̲a̲n̲d̲ ̲[l_1 \lt src(I_…


Color threshold :

Hue means different colors , Each color has a specific hue value .

OpenCV in HSV The range of color space is :H [0,180],S [0,255],V [0,255].

Range of hue values of common colors , That is, the boundary threshold , As shown in the following table .

 Insert picture description here


routine 14.17:HSI Color space image segmentation

    # 14.17 HSI  Color space image segmentation 
    #  stay HSV Space threshold processing for green screen color area , Generate mask for matting 
    img = cv.imread("../images/lady983Green.png", flags=1)  #  Read color images 
    hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)  #  Convert pictures to  HSV  Color space 

    #  Use  cv.inrange  Function in  HSV  The color range set by the space check , Convert to binary image , Generate mask 
    lowerColor = np.array([35, 43, 46])  # ( Lower limit :  green 33/43/46)
    upperColor = np.array([77, 255, 255])  # ( ceiling :  green 77/255/255)
    binary = cv.inRange(hsv, lowerColor, upperColor)  #  Generate binary mask , Specifies the background color area white 
    binaryInv = cv.bitwise_not(binary)  #  Generate inverse mask , The foreground area has white windows , The background area is black 
    matting = cv.bitwise_and(img, img, mask=binaryInv)  #  Generate matting image  ( The prospect remains , Black background )

    #  Change the background color to red :  Modify the inverse mask  ( Areas other than matting are black )
    imgReplace = img.copy()
    imgReplace[binaryInv==0] = [0,0,255]  #  Black background area (0/0/0)  Change to red  (BGR:0/0/255)

    plt.figure(figsize=(9, 6))
    plt.subplot(221),plt.title("origin"), plt.axis('off')
    plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))
    plt.subplot(222), plt.title("binary mask"), plt.axis('off')
    plt.imshow(binary, cmap='gray')
    plt.subplot(223), plt.title("invert mask"), plt.axis('off')
    plt.imshow(binaryInv, cmap='gray')
    plt.subplot(224), plt.title("matting"), plt.axis('off')
    plt.imshow(cv.cvtColor(matting, cv.COLOR_BGR2RGB))
    plt.tight_layout()
    plt.show()

 Insert picture description here




【 At the end of this section 】

Copyright notice :
reference : Use the Photoshop Levels adjustment (adobe.com)
[email protected] Original works , Reprint must be marked with the original link :(https://blog.csdn.net/youcans/article/details/125389684)
Copyright 2022 youcans, XUPT
Crated:2022-6-20
Welcome to your attention 『youcans Of OpenCV routine 200 piece 』 series , Ongoing update
Welcome to your attention 『youcans Of OpenCV Learning lessons 』 series , Ongoing update

201. Image color space conversion
202. Look up table for quick replacement (cv.LUT)
203. Pseudo color image processing
204. Image color style filter
205. Adjust color balance / saturation / Lightness
206. Photoshop Color scale adjustment algorithm
207. Photoshop Automatic color scale adjustment algorithm
208. Photoshop Automatic contrast adjustment algorithm
209. HSV Color image segmentation in color space

原网站

版权声明
本文为[Xiaobai youcans]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241534499918.html