2022-04-06 动态界定图像HSV颜色范围
2022-04-06 本文已影响0人
颈椎以上瘫痪
有时候通过颜色范围处理图像时,如果对颜色范围界限要求不高时,可以参考一些常规颜色范围表就够了,但是有时候图像中颜色比较丰富,有时候颜色分量会与肉眼看到的不一样,基础颜色的范围并不能很好的区分出图像中的颜色,所以需要针对该场景下的兔相机进行范围缩放。在调试过程中如果改变一个值运行一次程序的效率是很低的,所以可以使用动态的方式改变HSV范围值来界定图像中的颜色范围。
笔者找到一个滑块找颜色的程序,在输入位置改成你的图像即可。这个程序中的UI不是很理想,图像显示不能缩放,后面有时间再研究一下修改图像为全尺寸显示会好很多。
from __future__ import division
import cv2
import numpy as np
from src.com.hf.opencv import u
def nothing(*arg):
pass
# Initial HSV GUI slider values to load on program start.
icol = (0, 0, 0, 255, 255, 255) # Green
# icol = (18, 0, 196, 36, 255, 255) # Yellow
# icol = (89, 0, 0, 125, 255, 255) # Blue
# icol = (0, 100, 80, 10, 255, 255) # Red
cv2.namedWindow('colorTest')
# Lower range colour sliders.
cv2.createTrackbar('lowHue', 'colorTest', icol[0], 255, nothing)
cv2.createTrackbar('lowSat', 'colorTest', icol[1], 255, nothing)
cv2.createTrackbar('lowVal', 'colorTest', icol[2], 255, nothing)
# Higher range colour sliders.
cv2.createTrackbar('highHue', 'colorTest', icol[3], 255, nothing)
cv2.createTrackbar('highSat', 'colorTest', icol[4], 255, nothing)
cv2.createTrackbar('highVal', 'colorTest', icol[5], 255, nothing)
# Raspberry pi file path example.
# frame = cv2.imread('/home/pi/python3/opencv/color-test/colour-circles-test.jpg')
# Windows file path example.
frame = cv2.imread('images/Image_20220401090044200(1).png')
while True:
# Get HSV values from the GUI sliders.
lowHue = cv2.getTrackbarPos('lowHue', 'colorTest')
lowSat = cv2.getTrackbarPos('lowSat', 'colorTest')
lowVal = cv2.getTrackbarPos('lowVal', 'colorTest')
highHue = cv2.getTrackbarPos('highHue', 'colorTest')
highSat = cv2.getTrackbarPos('highSat', 'colorTest')
highVal = cv2.getTrackbarPos('highVal', 'colorTest')
# Show the original image.
# cv2.imshow('frame', frame)
# Blur methods available, comment or uncomment to try different blur methods.
frameBGR = cv2.GaussianBlur(frame, (7, 7), 0)
# frameBGR = cv2.medianBlur(frameBGR, 7)
# frameBGR = cv2.bilateralFilter(frameBGR, 15 ,75, 75)
"""kernal = np.ones((15, 15), np.float32)/255
frameBGR = cv2.filter2D(frameBGR, -1, kernal)"""
# Show blurred image.
# cv2.imshow('blurred', frameBGR)
# HSV (Hue, Saturation, Value).
# Convert the frame to HSV colour model.
hsv = cv2.cvtColor(frameBGR, cv2.COLOR_BGR2HSV)
# HSV values to define a colour range.
colorLow = np.array([lowHue, lowSat, lowVal])
colorHigh = np.array([highHue, highSat, highVal])
mask = cv2.inRange(hsv, colorLow, colorHigh)
# Show the first mask
# cv2.imshow('mask-plain', mask)
kernal = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernal)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernal)
# Show morphological transformation mask
# cv2.imshow('mask', mask)
# Put mask over top of the original image.
result = cv2.bitwise_and(frame, frame, mask=mask)
# Show final output image
# cv2.imshow('colorTest', result)
u.show('colorTest', result)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()