当前位置:网站首页>3H proficient in opencv (VII) - color detection
3H proficient in opencv (VII) - color detection
2022-06-29 18:01:00 【Hua Weiyun】
| 0、 preparation |
|---|
Right click the new project , choice Python File, Create a new one Python file , Then at the beginning import cv2 Import cv2 library ,import numpy And rename to np.
import cv2import numpy as np
We also need to know where OpenCV in , The direction of the coordinate axis is x Shaft to the right ,y Axis down , The coordinate origin is in the upper left corner , For example, the following one is 640 Pixels , Wide for 480 Pixel image .OK, Let's begin with this section .
| 1、 Implementation code |
|---|
- stackImages() It is the library function written by ourselves that we used in the previous section , The following lines createTrackbar() Is defining windows and sliders .
createTrackbar("Hue Min","TrackBars",0,179,empty)in 0 Is the default value of the slider ,179 Is its range of values ,empty If it is empty pass Whatever it is . - Hue It's hue ,Sat It's saturation ,Val Is the pixel values . The image will show the pixels between the minimum and maximum values of these three attributes .
- while Loop is when the slider is dragged , The image will also change the display , At the same time, the value will also update the output .
def empty(a): passdef stackImages(scale,imgArray): rows = len(imgArray) cols = len(imgArray[0]) rowsAvailable = isinstance(imgArray[0], list) width = imgArray[0][0].shape[1] height = imgArray[0][0].shape[0] if rowsAvailable: for x in range ( 0, rows): for y in range(0, cols): if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]: imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale) else: imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale) if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR) imageBlank = np.zeros((height, width, 3), np.uint8) hor = [imageBlank]*rows hor_con = [imageBlank]*rows for x in range(0, rows): hor[x] = np.hstack(imgArray[x]) ver = np.vstack(hor) else: for x in range(0, rows): if imgArray[x].shape[:2] == imgArray[0].shape[:2]: imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale) else: imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale) if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR) hor= np.hstack(imgArray) ver = hor return verpath="Resources/lambo.png"cv2.namedWindow("TrackBars")cv2.resizeWindow("TrackBars",640,240)cv2.createTrackbar("Hue Min","TrackBars",0,179,empty)cv2.createTrackbar("Hue Max","TrackBars",19,179,empty)cv2.createTrackbar("Sat Min","TrackBars",110,255,empty)cv2.createTrackbar("Sat Max","TrackBars",240,255,empty)cv2.createTrackbar("Val Min","TrackBars",153,255,empty)cv2.createTrackbar("Val Max","TrackBars",255,255,empty)while True: img = cv2.imread(path) imgHSV = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) h_min = cv2.getTrackbarPos("Hue Min","TrackBars") h_max = cv2.getTrackbarPos("Hue Max", "TrackBars") s_min = cv2.getTrackbarPos("Sat Min", "TrackBars") s_max = cv2.getTrackbarPos("Sat Max", "TrackBars") v_min = cv2.getTrackbarPos("Val Min", "TrackBars") v_max = cv2.getTrackbarPos("Val Max", "TrackBars") print(h_min,h_max,s_min,s_max,v_min,v_max) lower = np.array([h_min,s_min,v_min]) upper = np.array([h_max,s_max,v_max]) mask = cv2.inRange(img,lower,upper) imgResult = cv2.bitwise_and(img,img,mask=mask) imgStack = stackImages(0.6,([img,imgHSV],[mask,imgResult])) cv2.imshow("Stacked Images", imgStack) cv2.waitKey(1)| 2、 Running effect |
|---|
Run to see the effect , At first, it's the default value , We change the minimum and maximum hue values to 0, It must be dark .
We will the hue of Max Drag onto 116, I found the color of the vehicle displayed .
Let's change the saturation Min, It's on 110, Many color dots in the image have disappeared .
We're going to saturate Max Set it to the maximum , At this time, all the images are displayed again .
Finally, adjust the pixel value , The car color display is very comprehensive , That means orange and white are in this range .
Reduce the pixel value range , The trolley will only display the colors in this range .
边栏推荐
- codeforces每日5题(均1700)-第二天
- 使用autoIt 上传文件
- Relationship among controller, service and Dao
- Professor of Cambridge University: eating breakfast often is harmful and dangerous. - you know what
- Automatic software test - read SMS verification code using SMS transponder and selenium
- Industry application of smart city based on GIS 3D visualization
- Maximum length of palindrome substring (string hash + binary)
- 分布式 | 几步快速拥有读写分离
- 字典树(随学)
- selenium上传文件
猜你喜欢

What technology is an applet container? Can it help Internet of things enterprises break through the red sea?

Automatic software test - read SMS verification code using SMS transponder and selenium

Spingmvc requests and responses

Teach you how to install the latest version of mysql8.0 database on windows, nanny level teaching

Niuke Xiaobai monthly race 52 E group logarithmic sum (inclusion exclusion theorem + dichotomy)

How to use the chart control of the b/s development tool devextreme - customize the axis position?

Abc253 D fizzbuzz sum hard (tolerance exclusion theorem)

Opencv+YOLO-V3实现目标跟踪

Xiaobai yuesai 51 supplement e g f

Parental delegation mechanism
随机推荐
最长异或路径(dfs+01trie)
小程序容器是什么技术?能助力物联网企业红海突围?
selenium 文件上传方法
Premature end of script headers 或 End of script output before headers
Xiaobai yuesai 51 supplement e g f
Force deduction daily question 06.29 add two numbers
金鱼哥RHCA回忆录:DO447构建高级作业工作流--使用事实缓存提高性能
Segment tree and tree array template (copy and paste are really easy to use)
Top 30 open source software
/usr/bin/ld: warning: **libmysqlclient.so.20**, needed by //usr/
3h精通OpenCV(六)-图像堆叠
3H proficient in opencv (IX) - the simplest face detection
MySql存储过程循环的使用分析详解
Mongotemplate - distinct use
两种Controller层接口鉴权方式
国外LEAD赚钱,做个网站真的很简单
Digital twin energy system, creating a "perspective" in the low-carbon era
3h精通OpenCV(八)-形状检测
selenium 组合键操作
Automatic software test - read SMS verification code using SMS transponder and selenium