完成特定区域不同颜色的判别0pencv-python
采用hsv完成特定区域提取,inrange返回的是单通道的灰度图,numpty库中等于和函数传参时是引用传参,可使用.copy()函数。以下为代码。
下面展示一些 内联代码片
。
下面展示一些 内联代码片
。
import numpy as np
import cv2
def getpos(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
print("HSV is", HSV[y, x])
def getpos1(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
print("mask_image isy",y ,x , mask_image[y, x])
def getpos2(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
print("mask is", y, x,mask[y, x])
def calculateCountourArea(image, src):
print("hsv", image.shape)
image1 = image.copy()
# 如果color中定义了几种颜色区间,都可以分割出来
for (lower, upper) in color:
# 创建NumPy数组
lower = np.array(lower, dtype="uint8") # 颜色下限
upper = np.array(upper, dtype="uint8") # 颜色上限
# 根据阈值找到对应颜色
mask1 = cv2.inRange(image1, lower, upper) # 查找处于范围区间的
kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) # 构造腐蚀核
mask1 = cv2.erode(mask1, kernel1)
mask1 = cv2.dilate(mask1, kernel1)
ret1, mask1 = cv2.threshold(mask1, 0.5, 255, cv2.THRESH_BINARY)
cv2.imshow("形态学处理", mask1)
# 二值化
ret1, binarry1 = cv2.threshold(mask1, 0.5, 1, cv2.THRESH_BINARY)
mask_image1, contours1, hierarchy1 = cv2.findContours(binarry1, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
area1 = []
# 找到最大的轮廓
for k1 in range(len(contours1)):
area1.append(cv2.contourArea(contours1[k1]))
max_idx1 = np.argmax(np.array(area1))
print(len(contours1))
cv2.drawContours(src, contours1, -1, (0, 0, 255), 1)
for i in contours1:
print(cv2.contourArea(i)) # 计算缺陷区域面积
x, y, w, h = cv2.boundingRect(i) # 画矩形框
cv2.rectangle(src, (x, y), (x + w, y + h), (0, 255, 0), 1)
cv2.imshow("src", src)
# 读 取 图 片
img = cv2.imread("C:/Users/asus/Desktop/Inkedpaomo_LI.jpg", 1)# 直接读为灰度图像
# 可以缩小图像
height, width = img.shape[:2]
size = (int(width ), int(height )) # bgr
img = cv2.resize(img, size, interpolation=cv2.INTER_AREA)
#BGR转化为HSV
HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow("HSV", HSV)
print("hsv", HSV.shape)
cv2.setMouseCallback("imageHSV", getpos)
color = [
([0, 0, 90], [165, 30, 240]) # 蓝色范围~这个是我自己试验的范围,可根据实际情况自行调整~注意:数值按[b,g,r]排布
]
# 如果color中定义了几种颜色区间,都可以分割出来
for (lower, upper) in color:
# 创建NumPy数组
lower = np.array(lower, dtype="uint8") # 颜色下限
upper = np.array(upper, dtype="uint8") # 颜色上限
# 根据阈值找到对应颜色
mask = cv2.inRange(HSV, lower, upper) #查找处于范围区间的
#cv2.imshow("mask", mask)
print("mask", mask.shape)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) # 构造腐蚀核
mask = cv2.erode(mask, kernel)
mask = cv2.dilate(mask, kernel)
ret, mask = cv2.threshold(mask, 0.5, 255, cv2.THRESH_BINARY)
cv2.imshow("形态学处理", mask)
#二值化
ret, binarry = cv2.threshold(mask, 0.5, 1, cv2.THRESH_BINARY)
mask_image, contours, hierarchy = cv2.findContours(binarry, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
mask_image = mask.copy()
#mask = mask-mask
#cv2.imshow('mask_image1', mask_image)
#cv2.imshow('mask_', mask)
area = []
# 找到最大的轮廓
for k in range(len(contours)):
area.append(cv2.contourArea(contours[k]))
max_idx = np.argmax(np.array(area))
#填充最大的轮廓
for k in range(len(contours)):
if(k == max_idx):
mask = cv2.drawContours(mask, contours, max_idx, 255, cv2.FILLED)
else:
mask = cv2.drawContours(mask, contours, k, 0, cv2.FILLED)
mask = cv2.drawContours(mask, contours, max_idx, 255, cv2.FILLED)
cv2.imshow("mask", mask)
cv2.imshow('mask_image', mask_image)
cv2.setMouseCallback("mask", getpos2)
cv2.setMouseCallback("mask_image", getpos1)
output = cv2.bitwise_and(mask, mask_image)
cv2.imshow("output", output)
new_hsv = cv2.bitwise_and(HSV, HSV, mask=output)
cv2.imshow("img", new_hsv)
#bgroutput = cv2.cvtColor(output, cv2.COLOR_HSV2BGR)
# 展示图片
#cv2.imshow("images", np.hstack([img, output]))
calculateCountourArea(new_hsv, img)
cv2.waitKey(0)