pip install opencv-contrib-python 需要安装此环境
import cv2
qq = cv2.imread('qq.jpg')
1 缩放图片
qq = cv2.resize(qq,None,fx=0.5,fy=0.5)
2 边缘识别
canny = cv2.Canny(qq,50,240)
参数一:原始图片数组
参数二:阈值1
参数三:阈值2
import cv2 qq = cv2.imread('qq.jpg') print(qq.shape) qq =cv2.resize(qq,None,fx=0.5,fy=0.5) canny = cv2.Canny(qq,40,40) cv2.imshow('1',canny) cv2.waitKey()
3 直方图均衡化
equalized_gray = cv2.equalizeHist(gray)
参数一:图片数组
暗变亮
# python 3.7 import cv2 qq = cv2.imread('qq.jpg') print(qq.shape) qq =cv2.resize(qq,None,fx=0.5,fy=0.5) cv2.imshow('1.',qq) # canny = cv2.Canny(qq,40,40) gray = cv2.cvtColor(qq,cv2.COLOR_RGB2GRAY) cv2.imshow('2',gray) equalized_gray = cv2.equalizeHist(gray) cv2.imshow('1',equalized_gray) yuv = cv2.cvtColor(qq,cv2.COLOR_BGR2YUV) cv2.imshow('5',yuv) yuv[...,0]=cv2.equalizeHist(yuv[...,0]) equalizd_color = cv2.cvtColor(yuv,cv2.COLOR_YUV2BGR) cv2.imshow('4',equalizd_color) cv2.waitKey()
4 结构特征
# python 3.7 import cv2 qq = cv2.imread('qq.jpg') print(qq.shape) qq =cv2.resize(qq,None,fx=0.5,fy=0.5) cv2.imshow('1.',qq) # canny = cv2.Canny(qq,40,40) gray = cv2.cvtColor(qq,cv2.COLOR_RGB2GRAY) cv2.imshow('1',gray) # 创建STAR特征点检测器 star =cv2.xfeatures2d.StarDetector_create() keypoints = star.detect(gray) mixture = qq.copy() # 特征点和原始图像的结合 cv2.drawKeypoints(qq,keypoints,mixture,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ) cv2.imshow('mixshow',mixture) cv2.waitKey()
5 图像的特征描述矩阵
图像的特征描述矩阵来源于反映图像中物体的结构特征关键点的空间直方图。
6 基于特征描述矩阵的图像物体识别
7
from PIL import Image,ImageFont,ImageDraw import numpy as np from pylab import * import cv2 import random def get_for(): a='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' a_list=[x for x in a] _ = random.choice(a_list) return _ def get_co(): while True: a='abcdef0123456789' a_list=[x for x in a] _ = [random.choice(a_list) for i in range(6)] if _ == 'ffffff' or _ =='#9c9c9c': continue break return '#%s'%''.join(_) def get_num(): a= [x for x in range(10)] b= [-x for x in range(1,10)] b.extend(a) return b font = ImageFont.truetype('CHILLER.TTF',40)#使用自定义的字体,第二个参数表示字符大小 im = Image.new("RGB",(100,50),'#ffffff')#生成空白图像 draw = ImageDraw.Draw(im)#绘图句柄 x,y=(0,0)#初始左上角的坐标 for i in range(4): x=x+10+abs(random.choice(get_num())) y = 0+random.choice(get_num()) if y == 0: y+=10 c = get_for() c1 = get_co() draw.text((x,y), c, font=font,fill=c1)#绘图 offsetx,offsety=font.getoffset('abc')#获得文字的offset位置 width,height=font.getsize('abc')#获得文件的大小 # im.show() im=np.array(im) #cv2.rectangle(im,(offsetx+x,offsety+y),(offsetx+x+width,offsety+y+height),(0,0,0),1)#绘出矩形框 #Image._show(im) cv2.imshow('11',im) cv2.waitKey(0)