opencv库的使用

# -*- coding: utf-8 -*- 
import cv2 as cv 
import numpy as np 
from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)

def zh_ch(string):
	return string.encode("gbk").decode(errors="ignore")
	
#读取图片
src = cv.imread("E:/opencv/image/test.png")
#cv.imshow("input",src)
#灰度图
gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY)
#cv.imshow("gray",gray)
print(src.shape)
print(gray.shape)
#创建空白图像 大小和src一样
black = np.zeros_like(src)
#cv.imshow("black",black)
#调节亮暗
black[:,:,:] = 50
lighter = cv.add(src,black)
darker = cv.subtract(src,black)
#cv.imshow("lightness",lighter)
#cv.imshow("darkness",darker)

#调节对比度
#dst = cv.addWeighted(src,1.2,black,0.0,0)
#cv.imshow("contrast",dst)
#放大/缩小 图像
h,w,c = src.shape
dst1 = cv.resize(src,(h//2,w//2))
#cv.imshow("resize-images",dst1)
#左右翻转
dst2 = cv.flip(src,1)
#cv.imshow("flip",dst2)
#上下翻转
dst3 = cv.flip(src,0)
#cv.imshow("filp0",dst3)
#旋转
M = cv.getRotationMatrix2D((w//2,h//2),45,1)
dst4 = cv.warpAffine(src,M,(w,h))
#cv.imshow("rotate",dst4)
#色彩变换 
#hsv
hsv = cv.cvtColor(src,cv.COLOR_BGR2HSV)
#cv.imshow("hsv",hsv)
#色彩表 -支持14种色彩变换
dst5 = cv.applyColorMap(src,cv.COLORMAP_AUTUMN)
#cv.imshow("color table",dst5)
#模糊处理
blur = cv.blur(src,(15,15))
#cv.imshow("blur",blur)
#高斯模糊  图像高斯模糊操作,sigma = 15
gblur = cv.GaussianBlur(src,(0,0),15)
#cv.imshow(u'高斯模糊',gblur)
#自定义滤波器
k = np.ones(shape=[5,5],dtype = np.float32)/5
dst6 = cv.filter2D(src,-1,k)
#cv.imshow("custom blur",dst6)
#高斯双边滤波
dst7 = cv.bilateralFilter(src,0,100,10)
#cv.imshow("bi-filter",dst7)

#图像梯度  图像梯度提取,基于一阶导数算子sobel
dx = cv.Sobel(src,cv.CV_32F,1,0)
dy = cv.Sobel(src,cv.CV_32F,0,1)
dx = cv.convertScaleAbs(dx)
dy = cv.convertScaleAbs(dy)
#cv.imshow("grad-x",dx)
#cv.imshow("gray-y",dy)

#边缘检测 图像边缘提取,Canny是经典的图像边缘提取算法。100表示低阈值,300表示高阈值。
edge = cv.Canny(src,100,300)
#cv.imshow("edge",edge)
#直方图均衡化
eh = cv.equalizeHist(gray)
#cv.imshow("eH",eh)

#角点检测 基于shi-tomas角点检测算法
corners = cv.goodFeaturesToTrack(gray,100,0.05,10)
print(len(corners))
for pt in corners:
	b = np.random.random_integers(0,256)
	g = np.random.random_integers(0,256)
	r = np.random.random_integers(0,256)
	r = np.random.random_integers(0,256)
	x = np.int32(pt[0][0])
	y = np.int32(pt[0][1])
	cv.circle(src,(x,y),5,(int(b),int(g),int(r)),2)
#cv.imshow("corners detection",src)

#二值化加载图像
gray1 = cv.cvtColor(src,cv.COLOR_BGR2GRAY)
#cv.imshow("binary input",gray1)
#固定阈值
#ret,binary = cv.threshold(gray,127,255,cv.THRESH_BINARY_INV,25,10)
#cv.imshow("binary",binary)
#全局阈值
binary = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY_INV,25,10)
#cv.imshow("ada",binary)
#轮廓分布
contours,hireachy = cv.findContours(binary,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
result = np.zeros_like(src)
for cnt in range(len(contours)):
	cv.drawContours(result,contours,cnt,(0,0,255),2,8)
#cv.imshow("contour",result)
#膨胀与腐蚀操作  二值图像的形态学基础操作,腐蚀与膨胀
se = cv.getStructuringElement(cv.MORPH_RECT,(5,5),(-1,-1))
d = cv.dilate(binary,se)
e = cv.erode(binary,se)
#cv.imshow("dilate",d)
#cv.imshow("erode",e)
#开闭操作 二值图像的形态学组合操作,开操作与闭操作
op = cv.morphologyEx(binary,cv.MORPH_OPEN,se)
c1 = cv.morphologyEx(binary,cv.MORPH_CLOSE,se)
cv.imshow("open",op)
cv.imshow("close",c1)

cv.waitKey(0)
cv.destroyAllWindows()

 

你可能感兴趣的:(opencv)