首先我们需要获取LOGO图片中含有的色素信息:
通过肉眼可以看到处理亲图片中的LOGO中包含三种颜色、白色、黄色、绿色。
这时我们需要精确获取这这三种颜色的HSV的值。
我们需要选择一张图片,然后点击图片上的LOGO位置的色素信息,之后采取鼠标位置点的BGR的值,然后转化成HSV值,代码如下:
import cv2
# 定义鼠标交互函数
def mouseColor(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print('HSV:', hsv[y, x]) #输出图像坐标(x,y)处的HSV的值
img = cv2.imread('/Users/peng/Desktop/sy/[email protected]') #读进来是BGR格式
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #变成HSV格式
cv2.namedWindow("Color Picker")
cv2.setMouseCallback("Color Picker", mouseColor)
cv2.imshow("Color Picker", img)
if cv2.waitKey(0):
cv2.destroyAllWindows()
分别得到白色、黄色、绿色HSV如下:
# 处理白色
0, 0, 240 #上限
255, 10, 255 #下限
# 处理绿色
80, 190, 134 #上限
85, 235, 182 #下限
# 处理黄色
20, 220, 220 #上限
33, 255, 255 #下限
得到颜色数据后我们需要对图片上的三种颜色进行处理依次处理
# 读取图片
img = '/Users/peng/Desktop/sy/1608197551135.jpg'
img = cv2.imread(img)
x = 0
y = 0
w = img.shape[1]
h = img.shape[0]
#print(roi)
roi = img[y:y + h, x:x + w]
# 处理白色
lowerb = np.array([0, 0, 240]) # 白色下限
upperb = np.array([255, 10, 255]) # 白色上限
roi_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # BGR转HSV,处理更精确
mask_blue = cv2.inRange(roi_hsv, lowerb, upperb) # 蒙版
cv2.imshow('2', mask_blue)
kernel = np.ones((20, 20), np.uint8) # 卷积核
mask_blue = cv2.dilate(mask_blue, kernel, iterations=1) # 膨胀
roi = cv2.inpaint(roi, mask_blue, 5, flags=cv2.INPAINT_TELEA) # 重绘
lowerb = np.array([80, 190, 134])
upperb = np.array([85, 235, 182])
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask_red = cv2.inRange(roi_hsv, lowerb, upperb)
mask_red = cv2.dilate(mask_red, kernel, iterations=1)
roi = cv2.inpaint(roi, mask_red, 3, flags=cv2.INPAINT_TELEA)
# 处理黄色
lowerb = np.array([20, 220, 220]) # 黄色下限
upperb = np.array([33, 255, 255]) # 黄色上限
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) # BGR转HSV,处理更精确
mask_blue = cv2.inRange(roi_hsv, lowerb, upperb) # 蒙版
#cv2.imshow('2', mask_blue)
kernel = np.ones((20, 20), np.uint8) # 卷积核
mask_blue = cv2.dilate(mask_blue, kernel, iterations=1) # 膨胀
roi = cv2.inpaint(roi, mask_blue, 5, flags=cv2.INPAINT_TELEA) # 重绘
# 重新填充
img[y:y + h, x:x + w] = roi
#cv2.imshow('after', img)
cv2.imwrite('/Users/peng/Desktop/sy/result13.jpg', img)
#获取文件下所有文件
def get_img_list(dir, firelist, ext=None):
newdir = dir
if os.path.isfile(dir): # 如果是文件
firelist.append(dir)
elif os.path.isdir(dir): # 如果是目录
for s in os.listdir(dir):
newdir = os.path.join(dir, s)
get_img_list(newdir, firelist, ext)
return firelist
def read_img():
image_path = '/opt/sy/bip/car1/product_image'
imglist = get_img_list(image_path, [], 'bmp')
imgall = []
for imgpath in imglist:
# print(imgpath)
try:
imgpath2 = imgpath.replace("car1", "car3")
temppath = imgpath2[0:imgpath2.rfind("/",1)]
if not os.path.exists(temppath):
os.makedirs(temppath)
img = cv2.imread(imgpath)
#cv2.imshow('original', img)
# 选择ROI
# roi = cv2.selectROI(windowName="original", img=img, showCrosshair=False, fromCenter=False)
# x, y, w, h = roi
x = 0
y = 0
w = img.shape[1]
h = img.shape[0]
# print(roi)
roi = img[y:y + h, x:x + w]
# cv2.imshow('1', roi)
# 处理百色
lowerb = np.array([0, 0, 240]) # 百色下限
upperb = np.array([255, 10, 255]) # 百色上限
roi_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # BGR转HSV,处理更精确
mask_blue = cv2.inRange(roi_hsv, lowerb, upperb) # 蒙版
#cv2.imshow('2', mask_blue)
kernel = np.ones((20, 20), np.uint8) # 卷积核
mask_blue = cv2.dilate(mask_blue, kernel, iterations=1) # 膨胀
roi = cv2.inpaint(roi, mask_blue, 5, flags=cv2.INPAINT_TELEA) # 重绘
# cv2.imshow('2', roi)
# 处理绿色
lowerb = np.array([80, 190, 134]) # 绿色下限
upperb = np.array([85, 235, 182]) # 绿色上限
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask_red = cv2.inRange(roi_hsv, lowerb, upperb)
mask_red = cv2.dilate(mask_red, kernel, iterations=1)
roi = cv2.inpaint(roi, mask_red, 5, flags=cv2.INPAINT_TELEA)
# 处理黄色
lowerb = np.array([20, 220, 220]) # 黄色下限
upperb = np.array([33, 255, 255]) # 黄色上限
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) # BGR转HSV,处理更精确
mask_blue = cv2.inRange(roi_hsv, lowerb, upperb) # 蒙版
# cv2.imshow('2', mask_blue)
kernel = np.ones((20, 20), np.uint8) # 卷积核
mask_blue = cv2.dilate(mask_blue, kernel, iterations=1) # 膨胀
roi = cv2.inpaint(roi, mask_blue, 5, flags=cv2.INPAINT_TELEA) # 重绘
# 重新填充
img[y:y + h, x:x + w] = roi
# cv2.imshow('after', img)
cv2.imwrite(imgpath2, img)
print(imgpath2)
except:
print("错误异常:"+imgpath)
return imgall
read_img()
cv2.waitKey(0)
cv2.destroyAllWindows()