OpenCV章提取
前言
大一误入学生会,现在经常被指派各种奇奇怪怪的事情。
比如一觉醒来,打开手机一看,13:30
晚六点以前要提交我院的一个比赛报名表
前几天都想着不急还早明天再找导员盖章,
结果导员趁我睡觉的时候偷偷跑出去找男人出差了
要是我一个人的比赛那无所谓;
但是是学院级的话,报名不通过我会死的很惨
以及我并不会使用PS工具(没对象,不自拍,没有P图需求)
我仿佛已经看到了社死+学业警告的结果在等着我
思考这些我花去了一个小时,而且一直躺在床上。
突然
作为一个未来的外卖小哥程序员,我的脑海中突然蹦出来opencv这个词。
OpenCV可以做物体颜色识别+摄像头跟踪,
提取个章肯定不是问题;
垂死病中惊坐起谈笑风生又一年,马上打开CSDN Pycharm开干。
不想看我瞎扯可以直接跳到这
直接上代码。
不是教程或者实验博客我就不放代码解析了,各位直接拿去用就行,应付紧急情况还是可以的(乐
#coding:utf-8
"""
提取红色印章
"""
import cv2
import numpy as np
# 图片路径,自行添加
imgpath = ""
#加载图片
image = cv2.imread(imgpath)
#统一处理图片大小
img_w = 650 if image.shape[1] > 600 else 400
image = cv2.resize(image, (img_w,int(img_w*image.shape[0]/image.shape[1])), interpolation=cv2.IMREAD_COLOR)
impng=cv2.cvtColor(image.copy(), cv2.COLOR_RGB2RGBA)
hue_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
low_range = np.array([130, 43, 46])
high_range = np.array([180, 255, 255])
th = cv2.inRange(hue_image, low_range, high_range)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
th = cv2.dilate(th, element)
index1 = th == 255
print1 = np.zeros(impng.shape, np.uint8)
print1[:,:,:] = (255, 255, 255,0)
print1[index1] = impng[index1] # (0,0,255)
low_range = np.array([0, 43, 46])
high_range = np.array([9, 255, 255])
th = cv2.inRange(hue_image, low_range, high_range)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
th = cv2.dilate(th, element)
index1 = th == 255
print2 = np.zeros(impng.shape, np.uint8)
print2[:,:,:] = (255, 255, 255,0)
print2[index1] = impng[index1]
# 合并图像以增强效果
imgreal=cv2.add(print2,print1)
white_px = np.asarray([255, 255, 255,255])
(row, col, _) = imgreal.shape
for r in range(row):
for c in range(col):
px = imgreal[r][c]
if all(px == white_px):
imgreal[r][c] = impng[r][c]
#扩充图片防止截取部分
print4=cv2.copyMakeBorder(imgreal, 50, 50, 50, 50,cv2.BORDER_CONSTANT, value=[255, 255, 255,0])
print2gray = cv2.cvtColor(print4, cv2.COLOR_RGBA2GRAY)
retval, grayfirst = cv2.threshold(print2gray, 254, 255, cv2.THRESH_BINARY_INV)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (22, 22))
img6 = cv2.dilate(grayfirst, element)
c_canny_img = cv2.Canny(img6, 10, 10)
contours, hierarchy = cv2.findContours(c_canny_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
areas = []
for i, cnt in enumerate(contours):
rect = cv2.minAreaRect(cnt)
x, y, w, h = cv2.boundingRect(cnt)
area = w * h
ars = [area, i]
areas.append(ars)
areas = sorted(areas, reverse=True)
print(areas)
maxares = areas[:1]
x, y, w, h = cv2.boundingRect(contours[maxares[0][1]])
print5 = print4[y:(y + h), x:(x + w)]
#高小于宽
print(print5.shape)
if print5.shape[0]<print5.shape[1]:
zh=int((print5.shape[1]-print5.shape[0])/2)
print5 = cv2.copyMakeBorder(print5, zh, zh, 0, 0, cv2.BORDER_CONSTANT, value=[255, 255, 255,0])
else:
zh = int((print5.shape[0] - print5.shape[1]) / 2)
print5 = cv2.copyMakeBorder(print5, 0,0,zh, zh, cv2.BORDER_CONSTANT, value=[255, 255, 255,0])
realprint = cv2.resize(print5, (150, 150))
cv2.imshow('result', realprint)
cv2.imwrite('result.png', realprint)
cv2.waitKey()
原图
效果
仅供学习
说明:
如果章是别的颜色(蓝色等),可以用测色计获取章的RGB范围,并调整low_range和high_range的值;
灰色/黑色章的无法提取。