目录
  • hsv
  • canny边缘检测

已经给大家介绍了opencv python图片基本操作的相关内容,这里继续介绍图像处理算法,下面来一起看看吧

将图片转为灰度图

import cv2 #opencv读取的格式是bgr
img=cv2.imread('cat.jpg')
# 将图片转为灰度图像操作
img_gray = cv2.cvtcolor(img,cv2.color_bgr2gray)
img_gray.shape

hsv

h – 色调(主波长)。

s – 饱和度(纯度/颜色的阴影)。

v值(强度)

import cv2
hsv=cv2.cvtcolor(img,cv2.color_bgr2hsv)
cv2.imshow("hsv", hsv)
cv2.waitkey(0)    
cv2.destroyallwindows()

图像阈值

ret, dst = cv2.threshold(src, thresh, maxval, type)

src: 输入图,只能输入单通道图像,通常来说为灰度图

dst: 输出图

thresh: 阈值

maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值

type:二值化操作的类型,包含以下5种类型: cv2.thresh_binary; cv2.thresh_binary_inv; cv2.thresh_trunc; cv2.thresh_tozero;cv2.thresh_tozero_inv

cv2.thresh_binary 超过阈值部分取maxval(最大值),否则取0

cv2.thresh_binary_inv thresh_binary的反转

cv2.thresh_trunc 大于阈值部分设为阈值,否则不变

cv2.thresh_tozero 大于阈值部分不改变,否则设为0

cv2.thresh_tozero_inv thresh_tozero的反转

ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.thresh_binary)
ret, thresh2 = cv2.threshold(img_gray, 127, 255, cv2.thresh_binary_inv)
ret, thresh3 = cv2.threshold(img_gray, 127, 255, cv2.thresh_trunc)
ret, thresh4 = cv2.threshold(img_gray, 127, 255, cv2.thresh_tozero)
ret, thresh5 = cv2.threshold(img_gray, 127, 255, cv2.thresh_tozero_inv)

titles = ['original image', 'binary', 'binary_inv', 'trunc', 'tozero', 'tozero_inv']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]

for i in range(6):
    plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()

图像平滑

使用均值滤波实现图像平滑

# 均值滤波
# 简单的平均卷积操作
# 使用3*3的卷积和
blur = cv2.blur(img, (3, 3))

cv2.imshow('blur', blur)
cv2.waitkey(0)
cv2.destroyallwindows()

使用方框滤波实现图像平滑:

# 方框滤波
# 基本和均值一样,可以选择归一化
box = cv2.boxfilter(img,-1,(3,3), normalize=true)  

cv2.imshow('box', box)
cv2.waitkey(0)
cv2.destroyallwindows()
# 方框滤波
# 基本和均值一样,可以选择归一化,容易越界,越界后值为255
box = cv2.boxfilter(img,-1,(3,3), normalize=false)  

cv2.imshow('box', box)
cv2.waitkey(0)
cv2.destroyallwindows()

使用高斯滤波实现图像平滑:

# 高斯滤波
# 高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视距离
aussian = cv2.gaussianblur(img, (5, 5), 1)  

cv2.imshow('aussian', aussian)
cv2.waitkey(0)
cv2.destroyallwindows()

使用中值滤波实现图像平滑:

# 中值滤波
# 相当于用中值代替
median = cv2.medianblur(img, 5)  # 中值滤波

cv2.imshow('median', median)
cv2.waitkey(0)
cv2.destroyallwindows()

使用np将所有处理图片拼接显示:

# 展示所有的
res = np.hstack((blur,aussian,median))
#print (res)
cv2.imshow('median vs average', res)
cv2.waitkey(0)
cv2.destroyallwindows()

形态学-腐蚀操作

腐蚀操作可以用于去除图像中的毛刺

# iterations为腐蚀操作的迭代次数
kernel = np.ones((3,3),np.uint8) 
erosion = cv2.erode(img,kernel,iterations = 1)

cv2.imshow('erosion', erosion)
cv2.waitkey(0)
cv2.destroyallwindows()

形态学-膨胀操作

膨胀操作通常与腐蚀操作配合使用

# 先对图像进行腐蚀操作去除干扰信息
# kernel 为卷积核大小
kernel = np.ones((3,3),np.uint8) 
dige_erosion = cv2.erode(img,kernel,iterations = 1)

cv2.imshow('erosion', erosion)
cv2.waitkey(0)
cv2.destroyallwindows()
# 对图像进行膨胀操作将干扰信息以外的腐蚀部分复原
kernel = np.ones((3,3),np.uint8) 
dige_dilate = cv2.dilate(dige_erosion,kernel,iterations = 1)

cv2.imshow('dilate', dige_dilate)
cv2.waitkey(0)
cv2.destroyallwindows()

开运算与闭运算

开运算:先腐蚀,再膨胀

闭运算:先膨胀,再腐蚀

# 开:先腐蚀,再膨胀
img = cv2.imread('dige.png')

kernel = np.ones((5,5),np.uint8) 
opening = cv2.morphologyex(img, cv2.morph_open, kernel)

cv2.imshow('opening', opening)
cv2.waitkey(0)
cv2.destroyallwindows()

# 闭:先膨胀,再腐蚀
img = cv2.imread('dige.png')

kernel = np.ones((5,5),np.uint8) 
closing = cv2.morphologyex(img, cv2.morph_close, kernel)

cv2.imshow('closing', closing)
cv2.waitkey(0)
cv2.destroyallwindows()

梯度运算

提取图片边缘信息

# 梯度=膨胀-腐蚀
pie = cv2.imread('pie.png')
kernel = np.ones((7,7),np.uint8) 
dilate = cv2.dilate(pie,kernel,iterations = 5)
erosion = cv2.erode(pie,kernel,iterations = 5)

res = np.hstack((dilate,erosion))

cv2.imshow('res', res)
cv2.waitkey(0)
cv2.destroyallwindows()

gradient = cv2.morphologyex(pie, cv2.morph_gradient, kernel)

cv2.imshow('gradient', gradient)
cv2.waitkey(0)
cv2.destroyallwindows()

礼帽与黑帽

礼帽 = 原始输入-开运算结果

黑帽 = 闭运算-原始输入

#礼帽
img = cv2.imread('dige.png')
tophat = cv2.morphologyex(img, cv2.morph_tophat, kernel)
cv2.imshow('tophat', tophat)
cv2.waitkey(0)
cv2.destroyallwindows()
#黑帽
img = cv2.imread('dige.png')
blackhat  = cv2.morphologyex(img,cv2.morph_blackhat, kernel)
cv2.imshow('blackhat ', blackhat )
cv2.waitkey(0)
cv2.destroyallwindows()

图像梯度处理

通过像素差异提取图片边缘

sobel算子

scharr算子

laplacian算子

对于梯度更敏感

检测图像像素梯度变换gx为水平梯度检测,gy为垂直梯度检测。gx与gy相当于前面提到的卷积和。

dst = cv2.sobel(src, ddepth, dx, dy, ksize)
# ddepth:图像的深度
# dx和dy分别表示水平和竖直方向
# ksize是sobel算子的大小
# 在opencv中像素小于0的点直接被认为是0
# 计算gx
sobelx = cv2.sobel(img,cv2.cv_64f,1,0,ksize=3)
# 将负数部分转为正数
sobelx = cv2.convertscaleabs(sobelx)
cv_show(sobelx,'sobelx')
# 计算gy
sobelx = cv2.sobel(img,cv2.cv_64f,0,1,ksize=3)
# 将负数部分转为正数
sobelx = cv2.convertscaleabs(sobelx)
cv_show(sobelx,'sobelx')
# 计算gx与gy的加和
sobelxy = cv2.addweighted(sobelx,0.5,sobely,0.5,0)
cv_show(sobelxy,'sobelxy')

不同算子之间的差异

#不同算子的差异
img = cv2.imread('lena.jpg',cv2.imread_grayscale)
sobelx = cv2.sobel(img,cv2.cv_64f,1,0,ksize=3)
sobely = cv2.sobel(img,cv2.cv_64f,0,1,ksize=3)
sobelx = cv2.convertscaleabs(sobelx)   
sobely = cv2.convertscaleabs(sobely)  
sobelxy =  cv2.addweighted(sobelx,0.5,sobely,0.5,0)  

scharrx = cv2.scharr(img,cv2.cv_64f,1,0)
scharry = cv2.scharr(img,cv2.cv_64f,0,1)
scharrx = cv2.convertscaleabs(scharrx)   
scharry = cv2.convertscaleabs(scharry)  
scharrxy =  cv2.addweighted(scharrx,0.5,scharry,0.5,0) 

laplacian = cv2.laplacian(img,cv2.cv_64f)
laplacian = cv2.convertscaleabs(laplacian)   

res = np.hstack((sobelxy,scharrxy,laplacian))
cv_show(res,'res')

canny边缘检测

使用高斯滤波器,以平滑图像,滤除噪声。

计算图像中每个像素点的梯度强度和方向。

应用非极大值(non-maximum suppression)抑制,以消除边缘检测带来的杂散响应。

应用双阈值(double-threshold)检测来确定真实的和潜在的边缘。

通过抑制孤立的弱边缘最终完成边缘检测。

1:高斯滤波器

卷积核为符合高斯分布的数据,主要将图像平滑。

2:梯度和方向

3:非极大值抑制

4:双阈值检测

img=cv2.imread("lena.jpg",cv2.imread_grayscale)

v1=cv2.canny(img,80,150)
v2=cv2.canny(img,50,100)

res = np.hstack((v1,v2))
cv_show(res,'res')

图像金字塔

高斯金字塔

拉普拉斯金字塔

主要用于特征提取

高斯金字塔:向下采样方法(缩小)

高斯金字塔:向上采样方法(放大)

# 向上变换
up=cv2.pyrup(img)
# 向下变换
down=cv2.pyrdown(img)

拉普拉斯金字塔

down=cv2.pyrdown(img)
down_up=cv2.pyrup(down)
l_1=img-down_up
cv_show(l_1,'l_1')

图像轮廓

cv2.findcontours(img,mode,method)

mode:轮廓检索模式

retr_external :只检索最外面的轮廓;

retr_list:检索所有的轮廓,并将其保存到一条链表当中;

retr_ccomp:检索所有的轮廓,并将他们组织为两层:顶层是各部分的外部边界,第二层是空洞的边界;

retr_tree:检索所有的轮廓,并重构嵌套轮廓的整个层次;

method:轮廓逼近方法

chain_approx_none:以freeman链码的方式输出轮廓,所有其他方法输出多边形(顶点的序列)。

chain_approx_simple:压缩水平的、垂直的和斜的部分,也就是,函数只保留他们的终点部分。

img = cv2.imread('contours.png')
gray = cv2.cvtcolor(img, cv2.color_bgr2gray)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.thresh_binary)
cv_show(thresh,'thresh')
# 提取轮廓
binary, contours, hierarchy = cv2.findcontours(thresh, cv2.retr_tree, cv2.chain_approx_none)
# 绘制轮廓
#传入绘制图像,轮廓,轮廓索引,颜色模式,线条厚度
# 注意需要copy,要不原图会变。。。
draw_img = img.copy()
res = cv2.drawcontours(draw_img, contours, -1, (0, 0, 255), 2)
cv_show(res,'res')

轮廓特征

# 选取轮廓     0表示第一个轮廓
cnt = contours[0]
#面积
cv2.contourarea(cnt)
#周长,true表示闭合的
cv2.arclength(cnt,true)

轮廓近似

epsilon = 0.15*cv2.arclength(cnt,true) 
approx = cv2.approxpolydp(cnt,epsilon,true)

draw_img = img.copy()
res = cv2.drawcontours(draw_img, [approx], -1, (0, 0, 255), 2)
cv_show(res,'res')
# 外接矩形
img = cv2.imread('contours.png')

gray = cv2.cvtcolor(img, cv2.color_bgr2gray)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.thresh_binary)
binary, contours, hierarchy = cv2.findcontours(thresh, cv2.retr_tree, cv2.chain_approx_none)
cnt = contours[0]

x,y,w,h = cv2.boundingrect(cnt)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv_show(img,'img')
# 外接圆
(x,y),radius = cv2.minenclosingcircle(cnt) 
center = (int(x),int(y)) 
radius = int(radius) 
img = cv2.circle(img,center,radius,(0,255,0),2)
cv_show(img,'img')

直方图

用于统计图片像素值分布,x轴表示像素值(0-255),y轴表示该像素值对应个数。

cv2.calchist(images,channels,mask,histsize,ranges)

images: 原图像图像格式为 uint8 或 float32。当传入函数时应 用中括号 [] 括来例如[img]

channels: 同样用中括号括来它会告函数我们统幅图 像的直方图。如果入图像是灰度图它的值就是 [0]如果是彩色图像 的传入的参数可以是 [0][1][2] 它们分别对应着 bgr。

mask: 掩模图像。统整幅图像的直方图就把它为 none。但是如 果你想统图像某一分的直方图的你就制作一个掩模图像并 使用它。

histsize:bin 的数目。也应用中括号括来

ranges: 像素值范围常为 [0256]

# 统计灰度图的直方图
img = cv2.imread('cat.jpg',0) #0表示灰度图
hist = cv2.calchist([img],[0],none,[256],[0,256])
hist.shape
# 统计三通道直方图
img = cv2.imread('cat.jpg') 
color = ('b','g','r')
for i,col in enumerate(color): 
    histr = cv2.calchist([img],[i],none,[256],[0,256]) 
    plt.plot(histr,color = col) 
    plt.xlim([0,256])

mask操作:

# 创建mask
mask = np.zeros(img.shape[:2], np.uint8)
print (mask.shape)
mask[100:300, 100:400] = 255
cv_show(mask,'mask')
# 将mask与图像融合
masked_img = cv2.bitwise_and(img, img, mask=mask)#与操作
cv_show(masked_img,'masked_img')
# 使用mask进行直方图统计与非mask进行直方图统计
hist_full = cv2.calchist([img], [0], none, [256], [0, 256])
hist_mask = cv2.calchist([img], [0], mask, [256], [0, 256])

直方图均衡化:

是图像像素分布更加均匀。

# 直方图均衡化
equ = cv2.equalizehist(img) 
plt.hist(equ.ravel(),256)
plt.show()

自适应均衡化:

通过将图片划分为局部图片,然后进行直方图均衡化处理。

clahe = cv2.createclahe(cliplimit=2.0, tilegridsize=(8,8)) 

傅里叶变换

时域-》频域

傅里叶变换的作用

高频:变化剧烈的灰度分量,例如边界

低频:变化缓慢的灰度分量,例如一片大海

滤波

低通滤波器:只保留低频,会使得图像模糊,相当于对于边界的处理。

高通滤波器:只保留高频,会使得图像细节增强,相当于对于非边界的处理。

opencv中主要就是cv2.dft()和cv2.idft(),输入图像需要先转换成np.float32 格式。

得到的结果中频率为0的部分会在左上角,通常要转换到中心位置,可以通过shift变换来实现。

cv2.dft()返回的结果是双通道的(实部,虚部),通常还需要转换成图像格式才能展示(0,255)。

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('lena.jpg',0)

img_float32 = np.float32(img)

dft = cv2.dft(img_float32, flags = cv2.dft_complex_output)
dft_shift = np.fft.fftshift(dft)
# 得到灰度图能表示的形式
magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))

plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('input image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('magnitude spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
# 低频滤波
import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('lena.jpg',0)

img_float32 = np.float32(img)

dft = cv2.dft(img_float32, flags = cv2.dft_complex_output)
dft_shift = np.fft.fftshift(dft)

rows, cols = img.shape
crow, ccol = int(rows/2) , int(cols/2)     # 中心位置

# 低通滤波
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1

# idft
fshift = dft_shift*mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])

plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('input image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
plt.title('result'), plt.xticks([]), plt.yticks([])

plt.show()                

结果(低通对边界值不友好)

# 高频滤波
img = cv2.imread('lena.jpg',0)

img_float32 = np.float32(img)

dft = cv2.dft(img_float32, flags = cv2.dft_complex_output)
dft_shift = np.fft.fftshift(dft)

rows, cols = img.shape
crow, ccol = int(rows/2) , int(cols/2)     # 中心位置

# 高通滤波
mask = np.ones((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 0

# idft
fshift = dft_shift*mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])

plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('input image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
plt.title('result'), plt.xticks([]), plt.yticks([])

plt.show()    

结果(高通对非边界值不友好)

模板匹配

模板匹配和卷积原理很像,模板在原图像上从原点开始滑动,计算模板与(图像被模板覆盖的地方)的差别程度,这个差别程度的计算方法在opencv里有6种,然后将每次计算的结果放入一个矩阵里,作为结果输出。假如原图形是axb大小,而模板是axb大小,则输出结果的矩阵是(a-a+1)x(b-b+1)

# 模板匹配
img = cv2.imread('lena.jpg', 0)
template = cv2.imread('face.jpg', 0)
h, w = template.shape[:2] 

tm_sqdiff:计算平方不同,计算出来的值越小,越相关

tm_ccorr:计算相关性,计算出来的值越大,越相关

tm_ccoeff:计算相关系数,计算出来的值越大,越相关

tm_sqdiff_normed:计算归一化平方不同,计算出来的值越接近0,越相关

tm_ccorr_normed:计算归一化相关性,计算出来的值越接近1,越相关

tm_ccoeff_normed:计算归一化相关系数,计算出来的值越接近1,越相关

methods = ['cv2.tm_ccoeff', 'cv2.tm_ccoeff_normed', 'cv2.tm_ccorr',
           'cv2.tm_ccorr_normed', 'cv2.tm_sqdiff', 'cv2.tm_sqdiff_normed']
for meth in methods:
    img2 = img.copy()

    # 匹配方法的真值
    method = eval(meth)
    print (method)
    res = cv2.matchtemplate(img, template, method)
    min_val, max_val, min_loc, max_loc = cv2.minmaxloc(res)

    # 如果是平方差匹配tm_sqdiff或归一化平方差匹配tm_sqdiff_normed,取最小值
    if method in [cv2.tm_sqdiff, cv2.tm_sqdiff_normed]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)

    # 画矩形
    cv2.rectangle(img2, top_left, bottom_right, 255, 2)

    plt.subplot(121), plt.imshow(res, cmap='gray')
    plt.xticks([]), plt.yticks([])  # 隐藏坐标轴
    plt.subplot(122), plt.imshow(img2, cmap='gray')
    plt.xticks([]), plt.yticks([])
    plt.suptitle(meth)
    plt.show()

总结

到此这篇关于opencv python简易文档之图像处理算法的文章就介绍到这了,更多相关opencv python图像处理算法内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!