opencv API

https://blog.csdn.net/JsonD/article/details/84983702
https://blog.csdn.net/Barry_J/article/details/98730132
https://blog.csdn.net/jiaoyangwm/article/details/79961778?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
https://blog.csdn.net/jiaoyangwm/article/details/81088578
https://www.cnblogs.com/denny402/p/5166258.html
https://www.cnblogs.com/Undo-self-blog/p/8448500.html
http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/erosion_dilatation/erosion_dilatation.html#morphology-1 官方文档

import cv2
import numpy as np

vc = cv2.VideoCapture("")
if vc.isOpened():
	open,frame = vc.read()
else:
	open = False;
	break
while open:
	ret,frame = vc.read()
	if frame is None:
		break;
	if ret == True:
		gray = cv2.cvtCOlOR(frame,CV_BGR2GRAY)
		cv2.imshow("gray",gray)
		if cv2.waitKey(1)&0xFF==27:
			break;
vc.release()
import cv2
import numpy as np
pic = cv2.imread("",0) #0表示只读灰度图,1表示读BGR图像
cv2.imshow("pic",pic)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("pic",pic)

cap = cv2.VideoCapture("test.avi")
while(cap.isOpened()):
    ret,frame = cap.read()
    if ret:
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        cv2.imshow(frame)
    if cv2.waitKey(1) & 0xFF==ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

#cap = cv2.VideoCapture(-1) 摄像头编号
# Define the codec and create VideoWrite object
#fourcc = cv2.VideoWrite_fourcc(*'XVID') #注意编码器
#out = cv2.VideoWrite('ouput.avi',fourcc,20.0,(640,480))

#Draw a diagonal blue line with thickness of 5 px
cv2.line(img,(0,0),(50,50),(255,0,0),5)
cv2.rectangle(img,(0,0),(400,400),(0,0,255),3)
cv2.circle(img,(70,70),63,(0,0,255),-1)
cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
#多边形
pts = np.array([[10,5],[20,30],[70,20],[50,10]],np.int32)
#文字
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'opencv',(100,500),font,4,(255,25,255),6)
#对某个窗口应用鼠标事件函数
cv2.namedWindow('image')
cv2.setMouseCallback('img',draw_circle)
#图像代数运算
cv2.add()
cv2.subtract(img,80)
cv2.multiply(img,1.5)
cv2.addWeighted(img1,0.7,img2,0.3,0)

img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret,mask_front = cv2.threshold(img2gray,175,255,cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask_front)#取反
img1_bg = cv2.bitwise_and(img1,img1,mask=mask_front) #图片1扣背景
img2_fg = cv2.bitwise_and(img2,img2,mask = mask_inv) #图片2扣内容(背景为白色,内容灰度值较低)
result = cv2.add(img1_bg,img2_fg) #合并
#几何变换
res =cv2.resizee(img,None,fx =2,fy=2,interpolation = cv2.INTER_CUBIC)
M = cv2.getRotationMatrix2D((cols/2,rows/2),45,0.6)# 获得对应的旋转矩阵
#仿射变换
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)
#透视
pts1 = np.float32([[56,56],[368,52],[28,287],[389,390]])
pts2=  nnp.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv2.getPerspectiveTransform(pts1,pts2)
#直方图
#cv2:calcHist(image,channels,mask,histSize,ranges[;hist[;accumulate]])
hist = cv2.calcHist([img],[0],None,[256],[0,256])
hist,bins = np.histogram(img.ravel(),256,[0,256])
#均衡化灰度图
equ = cv2.equalizeHist(img)

#卷积
dst = cv2.filter2D(img,-1,kernel)
blur3 = cv2.blur(img,(3,3)) #均值
median = cv2.medianBlur(img,5)#中值
blur = cv2.GaussianBlur(img,(5,5),0)#高斯
laplacian = cv2.Laplacian(img,-1)#拉普拉斯
sobelx = cv2.Sobel(img,-1,1,0,ksize=5)#梯度
#复变
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift)) #进行对数处理,可视化
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
#形态学
kernel = np.ones((5,5),np.uint8)
#kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
erosion = cv2.erode(img,kernel,iterations = 1)#腐蚀
dilation = cv2.dilate(img,kernel,iterations =1)#膨胀
opening = cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)#开运算
closeing = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)#闭运算
gradient = cv2.morphologyEX(img,cv2.MORPH_GRADIENT,kernel)#膨胀-腐蚀 result = cv2.absdiff(dilate,erode)
#形态学阈值
ret,thresh1 = cv2.threshold(img,cv2.THRESH_BINARY)
#自适应阈值
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)

cv2.cvtColor(input_image,flag)#色彩转换
mask = cv2.inRange(hsv,lower_blue,upper_blue) #构建掩模
#参数1:图像
#参数2:提取规则,cv2.RETR_EXTERNAL:只找外轮廓,cv2.RETR_TREE:内外轮廓都找
#参数3:输出轮廓内容格式。cv2.CHAIN_APPROX_SIMPLE:输出少量轮廓点。cv2.CHAIN_APPROX_NONE:输出大量轮廓点
#输出参数1:图像
#输出参数2:轮廓列表
#输出参数3:层级
#cnts = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[1]
contours_map,contours,hierarchy = cv2.findContours(image,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)#找出轮廓
#参数1:图像
#参数2:轮廓列表
#参数3:轮廓索引,如果负数则画出所有轮廓
#参数4:轮廓颜色
#参数5:轮廓粗细
res = cv2.drawContours(draw_img,cnts,-1,(0,0,255),1)#画出轮廓
#参数1:某一轮廓列表
#参数2:像素点坐标
#参数3如果为True则输出该像素点到轮廓最近距离。如果为False,则输出为正表示在轮廓内,0为轮廓上,负为轮廓外。
result = cv2.pointPolygonTest(biggest,(w,h),False)#判断某点是否在某一轮廓内
#参数1:某一轮廓
cv2.contourArea(contours[i])#求轮廓面积
#参数1:某一轮廓
#输出参数1:四个角点坐标和偏移角度
#求最小方框并将其画出:
rect = cv2.minAreaRect(contours[i])
box = np.int0(cv2.boxPoints(rect)) # boxPoints()是opencv3的函数
cv2.drawContours(image,[box],0,(0,255,255),2#参数1:某一轮廓
x, y, w, h = cv2.boundingRect(contours[i])#求包含轮廓的正方框


#背景去除
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
 
fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows = False)
# fgbg = cv2.createBackgroundSubtractorKNN()
while(cap.isOpened()):
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    cv2.imshow('frame',fgmask)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
cv2.destroyAllWindows()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值