赞
踩
为了方便复制粘贴,汇总一下基础图像处理代码(如有遗漏欢迎指出,后续再添加修改)没有原理讲解,我也是个小白,方便日后写代码直接复制使用做的笔记
目录
3、旋转 getRotationMatrix2D(), warpAffine()
2、反二进制阈值化 cv2.THRESH_BINARY_INV
5、反阈值化为0 cv2.THRESH_TOZERO_INV
2、中值滤波 cv2.medianBlur(src, ksize)
3、高斯滤波 cv2.GaussianBlur(src, ksize, sigmaX)
1、腐蚀 cv2.erode(src, kernel, iterations)
2、膨胀 cv2.dilate(src, kernel, iterations)
3、开运算 cv2.morphologyEx(src, cv2.MORPH_OPEN, kernel)
4、闭运算 cv2.morphologyEx(src, cv2.MORPH_CLOSE, kernel)
- import numpy as np
- import matplotlib.pyplot as plt
- import cv2
img = cv2.imread("图像路径")
cv2.imshow("指定显示图片名称",读取的图片对象)
cv2.imwrite(‘保存路径',读取的图片对象)
cv2.waitKey(delay)
delay > 0
- 等待 delay 毫秒delay < 0
- 等待键盘单击delay = 0
- 无限等待delay = null
- 等待键盘单击cv2.destroyAllWindows()
- g = cv2.imread(r"路径",图像名称) # 灰度图像,单通道
-
- i = cv2.imread(r"路径",图像名称) # BGR图像,三通道
- print(g.shape)
- print(i.shape)
-
- print(g.size)
- print(i.size)
-
- print(g.dtype)
- print(i.dtype)
px = image[水平方向像素, 竖直方向像素]
- print("R:", image[水平像素, 竖直像素 -1])
- print("G:", image[水平像素, 竖直像素, 1])
- print("B:", image[水平像素, 竖直像素, 0])
R通道,G通道,B通道、红绿蓝三种不同的颜色通道
image[像素横坐标, 像素纵坐标] = [255, 255, 255]
- result = cv2.resize(src, (160,160))
- result = cv2.resize(src, (int(cols * 0.6), int(rows * 1.2)))
- result = cv2.resize(src, None, fx=0.5, fy=0.5)
src 表示原始图像;
dsize 表示缩放大小;
fx和fy 也可以表示缩放大小倍数,他们两个(dsize或fx/fy)设置一个即可实现图像缩放
- M = cv2.getRotationMatrix2D((cols/2, rows/2), 30, 1)
- #参数分别为:旋转中心、旋转度数、scale缩放比例
-
- rotated = cv2.warpAffine(src, M, (cols, rows))
- #参数分别为:原始图像、旋转参数 和 原始图像宽高
dst = cv2.flip(src, flipCode)
src 表示原始图像
flipCode 表示翻转方向
- M = np.float32([[1, 0, x], [0, 1, y]])
- shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
ret,thresh1=cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY)
像素灰度值>thresh,设为最大灰度值
像素灰度值<thresh,设为0
ret,thresh2=cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY_INV)
与二进制阈值化相反,>thresh,设为0
<thresh,设为255
ret,thresh3=cv2.threshold(GrayImage,127,255,cv2.THRESH_TRUNC)
像素灰度值>thresh,设为thresh
像素灰度值<thresh,不变
ret,thresh4=cv2.threshold(GrayImage,127,255,cv2.THRESH_TOZERO)
>thresh,不变
<thresh,设为0
ret,thresh5=cv2.threshold(GrayImage,127,255,cv2.THRESH_TOZERO_INV)
与阈值化为0正相反, >thresh,设为0
<thresh,不变
result = cv2.blur(src, ksize)
result = cv2.medianBlur(src, ksize)
result = cv2.GaussianBlur(source, (3, 3), 0)
设置卷积核
用numpy生成
kernel = np.ones((3,3), np.uint8)
用opencv自带函数--getStructuringElement
- element = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5)) #十字型
-
- element = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) #矩形
-
- element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) #椭圆形
fushi = cv2.erode(src, kernel)
pengzhang = cv2.dilate(src, kernel)
kai = cv2.morphologyEx(src, cv2.MORPH_OPEN, kernel)
bi = cv2.morphologyEx(src, cv2.MORPH_CLOSE, kernel)
tophat = cv2.morphologyEx(src, cv2.MORPH_TOPHAT, kernel)
blackhat = cv2.morphologyEx(src, cv2.MORPH_BLACKHAT, kernel)
dst=cv2.equalizeHist(src)
(1)numpy
- f = np.fft.fft2(img) #傅里叶变换
- fshift = np.fft.fftshift(f) #将低频移到中心
- rows,cols = int(rows/2), int(cols/2) #获取图像中心点
- fshift[crow-30:crow+30, col-30:col+30] = 0 #将中心点周围30区域置为0,即将低频信息去掉,实现高通
- #滤波
- ishift = np.fft.ifftshift(fshift) #将低频移还原到左上角
- iimg = np.fft.ifft2(ishift) #逆傅里叶变换
- iimg = np.abs(iimg) #将逆傅里叶变换的结果转换为灰度值,显示为图像
(2)opencv cv2.dft()
- dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) #傅里叶变换
- dftShift = np.fft.fftshift(dft) #低频移到中心位置
- result = 20*np.log(cv2.magnitude(dftShift[:,:,0], dftShift[:,:,1])) #转换为灰度值,显示
- o = cv2.dft(np.float32(o), flags=cv2.DFT_COMPLEX_OUTPUT) #傅里叶变换
- dshift = np.fft.fftshift(dft) #将低频移到中间
- rs,cs = o.shape #获取图像大小
- cr,cc = int(rs/2), int(cs/2) #图像中心
- mask=np.zeros((rs,cs,2),np.int8) #构造掩摸图像,两个通道
- mask[cr-30:cr+30, cc-30:cc+30] = 1
- md = dshift*mask #消除高频,留着中间部分的低频部分
- imd = np.fft.ifftshift(md) #将低频还原到左上角
- io = cv2.idft(imd) #逆傅里叶变换
- io = cv2.magnitude(io[:,:,0], io[:,:,1]) #转换为灰度值,才能显示为图像
dst = cv2.Sobel(src, ddepth, dx, dy, [ksize])
- dst = Scharr(src, cv2.CV_64F, dx, dy)
- dst = cv2.convertScaleAbs(dst)
-
- laplacian = cv2.Laplacian(src, ddepth)
- laplacian = cv2.convertScaleAbs(laplacian)
edges = cv2.Canny(image, threshold1, threshold2)
- r1 = cv2.pyrDown(原图)
- r2 = cv2.pyrDown(r1)
- r3 = cv2.pyrDown(r2)
- r1 = cv2.pyrUp(原图)
- r2 = cv2.pyrUp(r1)
- r3 = cv2.pyrUp(r2)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。