hsv原理
hsv2
import cv2
import numpy as np
def Process(img):
# 高斯平滑
gaussian = cv2.GaussianBlur(img, (3, 3), 0, 0, cv2.BORDER_DEFAULT)
# 中值滤波
median = cv2.medianBlur(gaussian, 5)
# Sobel算子
# 梯度方向: x
# sobel = cv2.Sobel(median, cv2.CV_8U, 1, 0, ksize=3)
## # 二值化
# ret, binary = cv2.threshold(sobel, 170, 255, cv2.THRESH_BINARY)
## # 核函数
# element1 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
# element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 7))
## # 膨胀
# dilation = cv2.dilate(binary, element2, iterations=1)
# # 腐蚀
# erosion = cv2.erode(dilation, element1, iterations=1)
# # 膨胀
# dilation2 = cv2.dilate(erosion, element2, iterations=3)
return median
frame = cv2.imread('cc.jpeg')
frame=Process(frame)
cv2.imshow("MyWindow", frame)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 将读取的BGR转换为HSV
lower1 = np.array([35, 43, 46]) # 所要检测的像素范围
upper1 = np.array([77, 255, 255]) # 此处检测绿色区域
lower = np.array([165, 43, 46]) # 所要检测的像素范围
upper = np.array([173, 255, 255]) # 此处检测绿色区域
mask = cv2.inRange(hsv, lowerb=lower, upperb=upper)
mask1 = cv2.inRange(hsv, lowerb=lower1, upperb=upper1)
cv2.imshow("mask", mask)
cv2.imwrite('red.jpg', mask)
cv2.imwrite('blue.jpg', mask1)
#success, frame = cameraCapture.read()
#frame = cv2.flip(frame, 1)
#cv2.imshow("img", img)
cv2.waitKey(0)