当前位置:网站首页>[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 .

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 :
- The input image can be a single channel grayscale image , It can also be a multi-channel color image .
- 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.
- 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 .
- 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 .

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()

【 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
边栏推荐
猜你喜欢

EasyBypass

降低pip到指定版本(通過PyCharm昇級pip,在降低到原來版本)

Data link layer & some other protocols or technologies

【OpenCV 例程200篇】209. HSV 颜色空间的彩色图像分割

C language - keyword 1

Graduation design of phase 6 of the construction practice camp

Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)

2022国际女性工程师日:戴森设计大奖彰显女性设计实力

手动事务的几个类

传输层 udp && tcp
随机推荐
【OpenCV 例程200篇】209. HSV 颜色空间的彩色图像分割
力扣每日一题-第25天-496.下一个更大元素Ⅰ
(待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识
leetcode-201_2021_10_17
壹沓科技签约七匹狼,助力「中国男装领导者」数字化转型
985测试工程师被吊打,学历和经验到底谁更重要?
leetcode_1470_2021.10.12
多线程收尾
#国企央企结构化面试#国企就业#墨斗互动就业服务管家
多路转接select
【吴恩达笔记】卷积神经网络
ST表+二分
leetcode:1504. 统计全 1 子矩形的个数
基于 KubeSphere 的分级管理实践
TKKC round#3
《各行业零代码企业应用案例集锦》正式发布
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
介绍BootLoader、PM、kernel和系统开机的总体流程
煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导
02--- impossible phenomenon of longitudinal wave