CV extracts the part of the picture with the specified color
How to extract the red, blue and green colors of the image below?
1. Import the library first
import cv2
import numpy as np
2. Read picture
The conversion between BGR and HSV uses CV2. Color_ BGR2HSV
img = cv2.imread("3.png")
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)#HSV空间
(in HSV space, h is color/chroma, value range [0179], s is saturation, value range [0255], V is brightness, value range [0255].)
3. The threshold values of three colors are set and extracted
cv2.inRange(src, lowerb, upperb[, dst]) → dst
SRC – first input array. Input matrix (image)
lowerb – inclusive lower boundary array or a scalar. Lower threshold
upperrb – inclusive upper boundary array or a scalar. Upper threshold
DST – output array of the same size as SRC and CV_ 8U type. Output the same matrix as Src
lower_blue=np.array([110,100,100])#blue
upper_blue=np.array([130,255,255])
lower_green=np.array([60,100,100])#green
upper_green=np.array([70,255,255])
lower_red=np.array([0,100,100])#red
upper_red=np.array([10,255,255])
red_mask=cv2.inRange(hsv,lower_red,upper_red)
blue_mask=cv2.inRange(hsv,lower_blue,upper_blue)
green_mask=cv2.inRange(hsv,lower_green,upper_green)
4. Process the original image
red=cv2.bitwise_and(img,img,mask=red_mask)
green=cv2.bitwise_and(img,img,mask=green_mask)
blue=cv2.bitwise_and(img,img,mask=blue_mask)
res=green+red+blue
cv2.bitwise_ And() function is used to “and” binary data, that is, to binary “and” each pixel value of image (gray image or color image), 1 & amp; 1 = 1, 1 & amp; 0 = 0, 0 & amp; 1 = 0, 0 & amp; 0 = 0.
5. Output display
cv2.imshow('img',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
6. Results display
All code packed
import cv2
import numpy as np
img = cv2.imread("3.png")
hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower_blue=np.array([110,100,100])#blue
upper_blue=np.array([130,255,255])
lower_green=np.array([60,100,100])#green
upper_green=np.array([70,255,255])
lower_red=np.array([0,100,100])#red
upper_red=np.array([10,255,255])
red_mask=cv2.inRange(hsv,lower_red,upper_red)
blue_mask=cv2.inRange(hsv,lower_blue,upper_blue)
green_mask=cv2.inRange(hsv,lower_green,upper_green)
red=cv2.bitwise_and(img,img,mask=red_mask)
green=cv2.bitwise_and(img,img,mask=green_mask)
blue=cv2.bitwise_and(img,img,mask=blue_mask)
res=green+red+blue
cv2.imshow('img',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
Read More:
- Python: How to parses HTML, extracts data, and generates word documents
- Python: How to get the size of the picture (byte/kb/mb)
- How to Solve cv2.applyColorMap Error
- How to Solve Opencv Reads Video Error: cv2.error
- Opencv Python realizes the paint filling function in PS, one click filling color and the possible reasons for opencv’s frequent errors
- [Solved] cv2.error: (-215:Assertion failed) encoder->isFormatSupported(CV_8U) in function ‘imwrite_‘
- [Solved] cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function ‘circle‘
- [Solved] cv2.xfeatures2d.SIFT_create() Error: module ‘cv2.cv2’ has no attribute ‘xfeatures2d’
- [Python] Right-click Selenium to Save the picture error: attributeerror: solution to module ‘pyscreen’ has no attribute ‘locationonwindow’
- Opencv: How to Draw Palette
- [Solved] Huawei OBS Python SDK download picture error: nosuchkey
- [Solved] error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor‘
- [Mac Pro M1] Python3.9 import cv2 Error: Reason: image not found
- Import CV2 & Numpy report red Warning and Error [How to Solve]
- [Solved] cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\……
- [Solved] cv2.error: OpenCV(4.5.1) XXX\shapedescr.cpp:315: error: (-215:Assertion failed) npoints >= 0 &&……
- [Solved] Python opencv imshow Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow
- Python+OpenCV: How to Use Background Subtraction Methods
- [Solved] Cv2.imshow Error: window.cpp:1274: error: (-2:Unspecified error) The function is not implemented.