转载来自https://blog.csdn.net/retacn_yue/article/details/53608368
第三章 Opencv3处理图像
1 不同色彩空间的转换
计算机视觉中三种常见的色彩空间:
灰度
BGR
HSV(hue色调 saturation饱合度 value黑暗程度)
2 傅里叶变换
快速傅里叶变换fft
离散傅里叶变换dft
高通滤波器heigh passfilter
检测图像的某个区域,根据像素和周围像素的亮度差值来提升该像素亮度的滤波器
示例代码如下:
import cv2
import numpy as np
from scipy import ndimage
kernel_3x3 = np.array([[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]])
kernel_5x5 = np.array([[-1, -1, -1, -1, -1, ],
[-1, 1, 2, 1, -1],
[-1, 2, 4, 2, -1],
[-1, 1, 2, 1, -1],
[-1, -1, -1, -1, -1]])
img=cv2.imread(‘../test.jpg’,cv2.IMREAD_GRAYSCALE)
k3=ndimage.convolve(img,kernel_3x3)
k5=ndimage.convolve(img,kernel_5x5)
blurred=cv2.GaussianBlur(img,(11,11),0)
g_hpf=img-blurred
cv2.imshow(‘3x3’,k3)
cv2.imshow(‘5x5’,k5)
cv2.imshow(‘g_hpf’,g_hpf)
cv2.waitKey()
cv2.destroyAllWindows()
低通滤波器low pass filter
在像素与周围像素的亮度差值小于一个特定值时,平滑该像素的亮度
3 创建模块
Filters.py文件,示例代码如下:
import cv2
import numpy as np
import Three.utils #自定义工具类
Utils.py文件
import cv2
import numpy as np
from scipy import interpolate
4 边缘检测
常用函数
def Laplacian(src,
ddepth,
dst=None,
ksize=None,
scale=None,
delta=None,
borderType=None)
def Sobel(src,
ddepth,
dx,
dy,
dst=None,
ksize=None,
scale=None,
delta=None,
borderType=None)
def Scharr(src,
ddepth,
dx,
dy,
dst=None,
scale=None,
delta=None,
borderType=None)
模糊滤波函数
1 平均
函数原型
def blur(src, #源图像
ksize, #内核大小
dst=None, #输出图像
anchor=None, #中心锚点
borderType=None)# 边界模式
2 高斯模糊
函数原型
def GaussianBlur(src, #输入图像
ksize, #高斯滤波模版大小
sigmaX, #横向滤波系数
dst=None, #输出图像
sigmaY=None,#纵向滤波系数
borderType=None)
3 中值模糊
def medianBlur(src, #源图像
ksize, #中值滤波器的模版的大小
dst=None)#输出图像
4 双边滤波
def bilateralFilter(src, #输入图像
d, #每个像素邻域的直径
sigmaColor, #颜色空间的标准偏差
sigmaSpace, #坐标空间的标准偏差
dst=None, #输出图像
borderType=None)#边缘点插值类型
示例代码如下:
import cv2
import numpy as np
import Three.utils #自定义工具类
def strokeEdges(src,
dst,
blurKsize=7,#中值滤波ksize
edgeKsize=5):#Laplacian算子ksize
if blurKsize>=3:
#中值滤波
blurredSrc=cv2.medianBlur(src,blurKsize)
#修改为灰度颜色空间
graySrc=cv2.cvtColor(blurredSrc,cv2.COLOR_BGR2GRAY)
else:
graySrc=cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
cv2.Laplacian(graySrc,cv2.CV_8U,graySrc,ksize=edgeKsize)
normalizedInverseAlpha=(1.0/255)*(255-graySrc)
channels=cv2.split(src)
for channel in channels:
channel[:]=channel*normalizedInverseAlpha
cv2.merge(channels,dst)
5 用定制内核作卷积
def filter2D(src, #输入图像
ddepth, #图像深度
kernel, #卷积核,单通道浮点矩阵
dst=None, #输出图像
anchor=None, #一个被滤波的点在核内的位置(中心)
delta=None,
borderType=None)#边界类型
如果要对每个通道使用不同的核,必须用split()和merge()
示例代码如下:
import cv2
import numpy as np
import Three.utils # 自定义工具类
class VConvolutionFilter(object):
def init(self, kernel):
self._kernel = kernel
def apply(self, src, dst):
cv2.filter2D(src, -1, self._kernel, dst)
class SharpenFilter(VConvolutionFilter):
def init(self):
kernel = np.array([[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]])
VConvolutionFilter.init(self, kernel)
class FindEdgesFilter(VConvolutionFilter):
def init(self):
kernel = np.array([[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]])
VConvolutionFilter.init(self, kernel)
class BlurFilter(VConvolutionFilter):
def init(self):
kernel = np.array([[0.04, 0.04, 0.04, 0.04, 0.04],
[0.04, 0.04, 0.04, 0.04, 0.04],
[0.04, 0.04, 0.04, 0.04, 0.04],
[0.04, 0.04, 0.04, 0.04, 0.04],
[0.04, 0.04, 0.04, 0.04, 0.04]])
VConvolutionFilter.init(self, kernel)
class EmbossFilter(VConvolutionFilter):
def init(self):
kernel = np.array([[-2, -1, 0],
[-1, 1, 1],
[0, 1, 2]])
VConvolutionFilter.init(self, kernel)
def strokeEdges(src,
dst,
blurKsize=7, # 中值滤波ksize
edgeKsize=5): # Laplacian算子ksize
if blurKsize >= 3:
# 中值滤波
blurredSrc = cv2.medianBlur(src, blurKsize)
# 修改为灰度颜色空间
graySrc = cv2.cvtColor(blurredSrc, cv2.COLOR_BGR2GRAY)
else:
graySrc = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
cv2.Laplacian(graySrc, cv2.CV_8U, graySrc, ksize=edgeKsize)
normalizedInverseAlpha = (1.0 / 255) * (255 - graySrc)
channels = cv2.split(src)
for channel in channels:
channel[:] = channel * normalizedInverseAlpha
cv2.merge(channels, dst)
6 修改应用
import cv2
from Three import filters
from Two.cameo.managers importWindowManager,CaptureManager
class Cameo(object):
def init(self):
self._windowManager=WindowManager('Cameo',self.onkeypress)
self._captureManager=CaptureManager(cv2.VideoCapture(0),self._windowManager,True)
# self._curveFilter=filters.BGRPortraCurveFilter()
def run(self):
self._windowManager.createWindow()
while self._windowManager.isWindowCreated:
self._captureManager.enterFrame()
frame=self._captureManager.frame
# filters.strokeEdges(frame,frame)
# self._curveFilter.apply(frame,frame)
self._captureManager.exitFrame()
self._windowManager.processEvents()
def onkeypress(self,keycode):
'''
space-> 载图
tab->启动和停止视频录制
esc->退出应用
:param keycode:
:return:
'''
if keycode==32:#space
self._captureManager.writeImage('screenshot.png')
elif keycode==9:#tab
if not self._captureManager.isWritingVideo:
self._captureManager.startWritingVideo('screencast.avi')
else:
self._captureManager.stopWritingVideo()
elif keycode==27:#esc
self._windowManager.destroyWindow()
if name==’main‘:
Cameo().run()
7 canny边缘检测
示例代码如下:
import cv2
import numpy as np
img=cv2.imread(‘../test.jpg’,cv2.IMREAD_GRAYSCALE)
cv2.imwrite(‘../canny.jpg’,cv2.Canny(img,200,300))
cv2.imshow(‘canny’,cv2.imread(‘../canny.jpg’))
cv2.waitKey()
cv2.destroyAllWindows()
8 轮廓检测
import cv2
import numpy as np
img=np.zeros((200,200,),dtype=np.uint8)
img[50:150,50:150]=255
ret,thresh=cv2.threshold(img,127,255,0)
image,contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
color=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
img=cv2.drawContours(color,contours,-1,(0,255,0),2)
cv2.imshow(‘contours’,color)
cv2.waitKey()
cv2.destroyAllWindows()
9 边界框,最小矩形和最小闭圆的轮廓
import cv2
import numpy as np
img = cv2.pyrDown(cv2.imread(‘../contours.jpg’, cv2.IMREAD_UNCHANGED))
ret, thresh = cv2.threshold(cv2.cvtColor(img.copy(),
cv2.COLOR_BGR2GRAY),
127,
255,
cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(thresh,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
#绘制矩形边界框
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(img, (x, y), (x + w, x + y), (0, 255, 0), 2)
#绘制最小矩形(红色)
rect=cv2.minAreaRect(c)
box=cv2.boxPoints(rect)
box=np.int0(box)
cv2.drawContours(img,[box],0,(0,0,255),3)
#绘制小最闭圆
(x,y),radius=cv2.minEnclosingCircle(c)
center=(int(x),int(y))
radius=int(radius)
img=cv2.circle(img,center,radius,(0,255,0),2)
cv2.drawContours(img,contours,-1,(255,0,0),1)
cv2.imshow(‘contours’,img)
cv2.waitKey()
cv2.destroyAllWindows()
10 凸轮廓与douglas-peucker
示例代码如下:
import cv2
import numpy as np
img=cv2.pyrDown(cv2.imread(‘../contours.jpg’),cv2.IMREAD_UNCHANGED)
ret,thresh=cv2.threshold(cv2.cvtColor(img.copy(),cv2.COLOR_BGR2GRAY),
127,
255,
cv2.THRESH_BINARY)
black=cv2.cvtColor(np.zeros((img.shape[0],img.shape[1]),
dtype=np.uint8),
cv2.COLOR_GRAY2BGR)
image,contours,hier=cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
#轮廓的周长
epsilon=0.01*cv2.arcLength(cnt,True)
approx=cv2.approxPolyDP(cnt,epsilon,True)
hull=cv2.convexHull(cnt)
cv2.drawContours(black,[cnt],-1,(0,255,0),2)#绿,精确的轮廓
cv2.drawContours(black,[approx],-1,(255,255,0),2)#蓝色 近似多边形
cv2.drawContours(black,[hull],-1,(0,0,255),2)#红
cv2.imshow(‘hull’,black)
cv2.waitKey()
cv2.destroyAllWindows()
11 直线和圆检测
函数原型:
def HoughLinesP(image, #源图像
rho, #线段的几何表示1
theta, #np.pi/180
threshold, #阈值
lines=None,
minLineLength=None, #最小直线长度
maxLineGap=None)#最大线段间隙
直线检测,示例代码如下:
import cv2
import numpy as np
img=cv2.imread(‘../contours.jpg’)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges=cv2.Canny(gray,50,120)
minLineLength=100
maxLineGap=5
lines=cv2.HoughLinesP(edges,#需要处理的图像
1,
np.pi/180,
100,
minLineLength,
maxLineGap)
for x1,y1,x2,y2 in lines[1]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imshow(‘edges’,edges)
cv2.imshow(‘lines’,img)
cv2.waitKey()
cv2.destroyAllWindows()
圆检测,示例代码如下:
import cv2
import numpy as np
img=cv2.imread(‘../circles.jpg’)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgMb=cv2.medianBlur(gray,5)
circles=cv2.HoughCircles(imgMb,
cv2.HOUGH_GRADIENT,
1,
120,
param1=100,
param2=30,
minRadius=0,
maxRadius=0)
circles=np.uint16(np.around(circles))
for i in circles[0,:]:
cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)
cv2.imwrite(‘../houghCircles.jpg’,img)
cv2.imshow(‘../houghCircles.jpg’,img)
cv2.waitKey()
cv2.destroyAllWindows()