OpenCV
雨点ing
呆到深处自然萌
展开
-
运动目标检测OpenCV背景减除法
背景减除法:1.GMM MOG2算法,高斯混合模型分离算法,它为每个像素选择适当数量的高斯分布函数:cv2.createBackgroundSubtractorMOG2(int history=500,double varThread=16,bool detectShadows =true)2.KNN模型:cv2.createBackgroundSubtractorKNN()方法:通过视频中的背景进行建模,实现背景消除,生成mask图像,通过对mask二值图像分析实现对前景活动对象的区域原创 2021-01-26 17:45:34 · 2102 阅读 · 0 评论 -
OpenCV运动目标识别光流法
import cv2import numpy as npes = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10,10))kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))cap = cv2.VideoCapture("anew.flv")frame1 = cap.read()[1]prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)...原创 2021-01-26 16:59:39 · 492 阅读 · 0 评论 -
OpenCV运动目标识别
import cv2camera = cv2.VideoCapture('anew.flv')out_fps =12.0fourcc = cv2.VideoWriter_fourcc('M','P','4','2')out1 = cv2.VideoWriter('v1.avi',fourcc,out_fps,(500,400))out2 = cv2.VideoWriter('v2.avi',fourcc,out_fps,(500,400))lastFrame = Nonewhile came.原创 2021-01-26 16:08:32 · 768 阅读 · 1 评论 -
人脸检测
import cv2print(cv2.__file__)img = cv2.imread('test5.jpg')#加载人脸特征,该文件在Python安装目录下,下面是我的绝对路径face_cascade = cv2.CascadeClassifier(r'/Users/wing/anaconda3/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_default.xml')gray = cv2.cvtC...原创 2021-01-26 15:20:32 · 200 阅读 · 0 评论 -
OpenCV学习笔记
"""参数1 即将保存的文件路径参数2 VideoWriter_fourcc为视频编解码器 fourcc意为四字符代码(Four-Character Codes),顾名思义,该编码由四个字符组成,下面是VideoWriter_fourcc对象一些常用的参数,注意:字符顺序不能弄混 cv2.VideoWriter_fourcc('I', '4', '2', '0'),该参数是YUV编码类型,文件名后缀为.avi cv2.VideoWriter_fourcc('P', 'I', .原创 2021-01-26 15:17:07 · 529 阅读 · 0 评论 -
模板匹配
import cv2import numpy as npdef template_demo(tpl,target): methods = [cv2.TM_SQDIFF_NORMED,cv2.TM_CCORR_NORMED,cv2.TM_CCOEFF_NORMED] th,tw =tpl.shape[:2] for md in methods: result = cv2.matchTemplate(target,tpl,md) min_val,ma.原创 2021-01-25 23:07:38 · 133 阅读 · 0 评论 -
敲出了绝望感的LBP
import cv2import numpy as npdef LBP(src): height = src.shape[0] width = src.shape[1] dst = src.copy() lbp_value = np.zeros((1,8),dtype = np.uint8) neighbours = np.zeros((1,8),dtype =np.uint8) for x in range(1,width-1): fo.原创 2021-01-25 21:34:43 · 114 阅读 · 0 评论 -
Harris角点检测
Harris实现过程:计算图像在X和Y方向的梯度:计算图像两个方向梯度的乘积使用高斯函数对三者进行高斯加权,生成矩阵M的A,B,C;计算每个像素的Harris相应值R,并对小于某一阈值t的R置为零;在3*3或者5*5邻域内进行非最大值抑制,局部最大值点即为图像中的角点。cv2.cornerHarris()参数如下:img-数据类型为float32的输入图像blocksize-角点检测中的领域大小ksize-Sobel求导中使用的窗口大小k-Harris角点原创 2021-01-25 16:51:22 · 482 阅读 · 0 评论 -
图像特征与目标检测1-HOG
HOG实现过程:1灰度化(将图像看做一个x,y,z(灰度)的三维图像)2采用Gamma校正法对输入图像进行颜色空间的标准化(归一化)3计算图像每个像素的梯度(包括大小和方向)4将图像划分为cells5统计每个cell的梯度直方图(不同梯度的个数),得到cell的描述子6将每几个cell组成一个block,得到block的描述子7将图像image内的所有block的HOG特征descriptor串联起来就可以得到HOG特征,该特征向量就是用来目标检测或分类的特征。...原创 2021-01-25 15:46:06 · 220 阅读 · 0 评论 -
分水岭算法(代码有问题,谁看出来了,求评论,谢谢)
分水岭算法步骤:1加载原始图像2阈值分割,将图像分割为黑白两个部分3对图像进行开运算,即先腐蚀再膨胀4对开运算的结果再进行膨胀,得到大部分是背景的区域5通过距离变换Distance Transform获取前景区域6背景区域sure_bg和前景区域sure_fg相减,得到既有前景又有背景的重合区域7连通区域处理8最后使用分水岭算法代码有问题,不知道错哪里了。。import cv2import numpy as npimg = cv2.imread('test4原创 2021-01-25 13:38:48 · 226 阅读 · 0 评论 -
学习笔记——区域生长算法
区域生长原理: 1 2 1 1 1 2 2 2 1 2 1 1 2 2 基本思想:将具有相似性质的像素集合起来构成区域。步骤;1.对图像顺序扫描,找到第1个还没有归属的像素,...原创 2021-01-25 12:28:08 · 701 阅读 · 0 评论 -
OpenCV边缘检测
梯度算子:是一阶导数算子,是水平G(x),G(y)方向对应模板的组合,也有对角线方向。常见的一阶算子:Roberts交叉算子,Rrewitt算子,Sobel算子0 1 -1 0 =1 0 0 -1 Roberts交叉算子是对角线方向的梯度算子,对应的水平方向和竖直方向的梯度分别为:Robert算子优缺点:优点:边缘定位比较准,适用于边缘明显且噪声较少的图像。缺点:没有描述水平和竖直方向的灰度变化,只是关注了对角线方向,鲁棒性差,..原创 2021-01-24 22:59:09 · 158 阅读 · 0 评论 -
opencv自动阈值分割
#自适应阈值与固定阈值对比import cv2import matplotlib.pyplot as pltimg = cv2.imread('test.jpg',0)ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,55,4)th3 = cv2.adaptiveThresho.原创 2021-01-24 17:27:32 · 1095 阅读 · 0 评论 -
固定阈值分割
THRESH_BINARY:THRESH_BINARY_INV:THRESH_TRUNC:THRESH_TOZERO:THRESH_TOZERO_INV:import cv2import matplotlib.pyplot as pltimg = cv2.imread('test3.jpg',0)ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)ret,thr...原创 2021-01-24 12:00:46 · 665 阅读 · 0 评论 -
opencv黑帽
#黑帽:输入图像和图像闭运算之差import cv2 as cvimport numpy as npimport matplotlib.pyplot as pltimg = cv.imread('test9.jpg')img = cv.cvtColor(img,cv.COLOR_BGR2RGB)kernel = np.ones((9,9),np.uint8)tophat = cv.morphologyEx(img,cv.MORPH_BLACKHAT,kernel)plt.subplot(1.原创 2021-01-23 19:07:17 · 123 阅读 · 0 评论 -
OpenCV顶帽
#顶帽:输入图像和图像开运算之差,下面的示例针对9*9内核完成。import cv2 as cvimport numpy as npimport matplotlib.pyplot as pltimg = cv.imread('test9.jpg')img = cv.cvtColor(img,cv.COLOR_BGR2RGB)kernel = np.ones((5,5),np.uint8)tophat = cv.morphologyEx(img,cv.MORPH_TOPHAT,kernel).原创 2021-01-23 18:51:16 · 165 阅读 · 0 评论 -
OpenCV形态学梯度
#形态学梯度,这是图像膨胀和腐蚀之间的区别,结果将看起来像对象的轮廓import cv2 as cvimport numpy as npimport matplotlib.pyplot as pltimg = cv.imread('test9.jpg')img = cv.cvtColor(img,cv.COLOR_BGR2RGB)kernel = np.ones((3,3),np.uint8)gradient = cv.morphologyEx(img,cv.COLOR_BGR2RGB,ke.原创 2021-01-23 17:32:04 · 304 阅读 · 0 评论 -
opencv闭运算
#闭运算,闭运算与开运算相反,先扩张然后再侵蚀。在关闭前景对象内部的小孔或对象上的小黑点时很有用。import cv2import numpy as npimport matplotlib.pyplot as pltimg = cv2.imread('test9.jpg')kernel = np.ones((3,3),np.uint8)closing = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)plt.subplot(121),plt.imsh.原创 2021-01-23 16:53:46 · 750 阅读 · 0 评论 -
opencv的开运算
#开运算,腐蚀然后扩张的另一个名字,它对于消除噪音很有用import cv2import numpy as npimport matplotlib.pyplot as pltimg = cv2.imread('test9.jpg')img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))opening = cv2.morphologyEx(img,cv2..原创 2021-01-23 16:39:31 · 414 阅读 · 0 评论 -
OpenCV的膨胀
import matplotlib.pyplot as pltimport cv2img = cv2.imread('test9.jpg')img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))dilation = cv2.dilate(img,kernel,iterations =1)plt.subplot(121),plt.imshow(img),pl.原创 2021-01-23 16:14:50 · 224 阅读 · 0 评论 -
腐蚀
import cv2import numpy as npimport matplotlib.pyplot as pltimg = cv2.imread('test9.jpg')img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)kernel = np.ones((3,3),np.uint8)erosion = cv2.erode(img,kernel,iterations = 1)plt.subplot(121),plt.imshow(img),plt.titl.原创 2021-01-23 14:43:34 · 191 阅读 · 0 评论 -
Gamma变换
#Gamma变化import cv2import numpy as npimg = cv2.imread('test.jpg')def adjust_gamma(image,gamma = 1.0): invGamma = 1.0/gamma table = [] for i in range(256): table.append(((i/255.0)**invGamma)*255) table = np.array(table).astype...原创 2021-01-23 12:22:43 · 306 阅读 · 0 评论 -
彩色图像直方图均衡化
import cv2import numpy as npimg = cv2.imread("test.jpg",1)cv2.imshow("src",img)#彩色图像均衡化,需要分解通道,对每一个通道均衡化(b,g,r) = cv2.split(img)bH = cv2.equalizeHist(b)gH = cv2.equalizeHist(g)rH = cv2.equalizeHist(r)result = cv2.merge((bH,gH,rH))cv2.imshow("dst.原创 2021-01-23 11:00:22 · 301 阅读 · 0 评论 -
opencv直方图均衡化
import cv2img = cv2.imread('test9.jpg',0)cv2.imshow("dark",img)#调用cv2.equalizeHist函数进行直方图均衡化img_equal = cv2.equalizeHist(img)cv2.imshow("img_equal",img_equal)cv2.waitKey(0)cv2.destoryAllWindows()原创 2021-01-23 10:21:26 · 103 阅读 · 0 评论 -
双边滤波
#双边滤波import cv2from matplotlib import pyplot as pltimg = cv2.imread('test.jpg')img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)blur = cv2.bilateralFilter(img,-1,15,10)plt.subplot(121),plt.imshow(img),plt.title('Original')plt.xticks([]),plt.yticks([])plt..原创 2021-01-22 23:00:52 · 91 阅读 · 0 评论 -
opencv中值滤波
#中值import cv2 as cvimport numpy as npfrom matplotlib import pyplot as pltimg = cv.imread('test.jpg')median = cv.medianBlur(img,9)plt.subplot(121),plt.imshow(img),plt.title('Original')plt.xticks([]),plt.yticks([])plt.subplot(122),plt.imshow(median).原创 2021-01-22 22:44:46 · 176 阅读 · 0 评论 -
opencv高斯滤波
import cv2 as cvimport numpy as npfrom matplotlib import pyplot as pltimg = cv.imread('test.jpg')img = cv.cvtColor(img,cv.COLOR_BGR2RGB)blur = cv.GaussianBlur(img,(7,7),7)plt.subplot(121),plt.imshow(img),plt.title('original')plt.xticks([]),plt.ytic.原创 2021-01-22 22:19:01 · 136 阅读 · 0 评论 -
opencv滤波
#方框滤波import cv2import numpy as npimg = cv2.imread('test.jpg', cv2.IMREAD_UNCHANGED)r = cv2.boxFilter(img,-1,(3,3),normalize=1)d = cv2.boxFilter(img,-1,(3,3),normalize=0)cv2.namedWindow('img',cv2.WINDOW_AUTOSIZE)cv2.namedWindow('r',cv2.WINDOW_AUTOSI.原创 2021-01-22 21:45:36 · 91 阅读 · 0 评论 -
opencv透视变换
import cv2import numpy as npimport matplotlib.pyplot as pltsrc = cv2.imread('test.jpg')rows,cols = src.shape[:2]pos1 = np.float32([[114,82],[287,156],[8,100],[143,177]])pos2 = np.float32([[0,0],[188,0],[0,262],[188,262]])M = cv2.getPerspectiveTrans.原创 2021-01-22 16:06:00 · 95 阅读 · 0 评论 -
旋转
import cv2import numpy as npimg = cv2.imread('test.jpg',1)rows,cols = img.shape[:2]M = cv2.getRotationMatrix2D((cols/2,rows/2),45,2)print(M)dst = cv2.warpAffine(img,M,(4*cols,4*rows),borderValue=(25,255,255))while(1): cv2.imshow('img',img) .原创 2021-01-21 20:55:37 · 81 阅读 · 0 评论 -
OpenCV学习笔记4resize
import cv2import numpy as npimg = cv2.imread('test.jpg')res1 = cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_LINEAR)height,width = img.shape[:2]res2 = cv2.resize(img,(int(0.8*width),int(0.8*height)),interpolation=cv2.INTER_AREA)cv2.imshow('o.原创 2021-01-21 20:49:16 · 124 阅读 · 0 评论 -
OpenCV添加文字
import numpy as npimport cv2img = np.zeros((512,512,3),np.uint8)font = cv2.FONT_HERSHEY_SIMPLEXcv2.putText(img,'OpenCV',(50,200),font,3,(0,255,255),cv2.LINE_AA)winname = 'example'cv2.namedWindow(winname)cv2.imshow(winname,img)cv2.waitKey(0)cv2.de.原创 2021-01-21 20:44:35 · 164 阅读 · 0 评论