OpenCV3学习笔记整理(二期)
Posted 数据轩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV3学习笔记整理(二期)相关的知识,希望对你有一定的参考价值。
点击下方链接,回顾一期哦~
二
常用文件操作接口
2.3 捕获摄像头的帧
捕获摄像头的帧(也是使用VideoCapture类,但是不是传入视频文件名,而是需要传入设备索引)
import cv2cameraCapture = cv2.VideoCapture(0) # 从索引号为0的摄像头捕获图像
fps = 30 # an assumptionsize =(int(cameraCapture.
get(cv2.CAP_PROP_FRAME_WIDTH)),int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))) # camera捕获的宽高,get无法返回准确的摄像头帧速率,它总是返回0
videoWriter = cv2.VideoWriter
('MyOutputVid.avi',
cv2.VideoWriter_fourcc('I','4','2','0'), fps, size)
# 以什么样的方式写入特定文件名success,
frame = cameraCapture.read()numFramesRemaining =
10 * fps - 1 #总的帧数
while success and numFramesRemaining > 0:
videoWriter.write(frame)
# 捕获到的帧写入到文件里
success, frame = cameraCapture.read()
numFramesRemaining -= 1cameraCapture.release() # 释放摄像头同步一组摄像头或一个多头摄像头(例如立体摄像头),read()不再合适,一般使用如下方法:
success0 = cameraCapture0.grab()success1
= cameraCapture1.grab()if success0 and success1:
frame0 = cameraCapture0.retrieve()
frame1 = cameraCapture1.retrieve()
三
常用检测方法
3.1 Canny边缘检测
import cv2
import numpy as np
img = cv2.imread("../images/statue_small.jpg", 0)
cv2.imwrite("canny.jpg",
cv2.Canny(img, 200, 300))
cv2.imshow("canny", cv2.imread("canny.jpg"))
cv2.waitKey()
cv2.destroyAllWindows()
Canny边缘检测算法的五个步骤:
i.使用高斯滤波器对图像进行去燥
ii. 计算梯度
iii. 在边缘上使用非最大抑制(NMS)
iv. 在检测到的边缘上使用双(double)阈值去除假阳性(false postitive)
v. 分析所有的边缘及其之间的连接,以保留真知的边缘并消除不明显的边缘
3.2 轮廓检测
import cv2import numpy as npimg = np.zeros((200, 200), dtype=np.uint8)
# 设定一张全黑的200x200大小的
imgimg[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)
#直接调用
cv2.findContours
获取轮廓信息,该函数包含三个参数:输入图像、层次类型和轮廓逼近方法
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()
3.3 边界框、最小矩形区域和最小闭圆的轮廓
import cv2import numpy as npimg
= cv2.pyrDown(cv2.imread("hammer.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:
# find bounding box coordinates
x,y,w,h = cv2.boundingRect(c) # 先计算一个简单的边界框
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2) # 画出这个边界
# find minimum area
rect = cv2.minAreaRect(c) # 计算最小矩形区域
# calculate coordinates of the minimum area rectangle
box = cv2.boxPoints(rect)
# normalize coordinates to integers
box = np.int0(box)
# draw contours
cv2.drawContours(img, [box], 0, (0,0, 255), 3) # 画出这个矩形
# calculate center and radius of minimum enclosing circle
(x,y),radius = cv2.minEnclosingCircle(c) # 检查边界最小闭圆
# cast to integers
center = (int(x),int(y))
radius = int(radius)
# draw the circle
img = cv2.circle(img,center,radius
(0,255,0),2)cv2.drawContours(img, contours, -1, (255, 0, 0), 1)cv2.imshow("contours", img)
3.4 直线检测
import cv2import numpy as np
img = cv2.imread('lines.jpg')gray
= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)edges
= cv2.Canny(gray,50,120)minLineLength
= 20maxLineGap = 5lines
=cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap) # 调用cv2.HoughLinesP检测直线for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)cv2.imshow("edges", edges)cv2.imshow("lines", img)cv2.waitKey()cv2.destroyAllWindows()
3.5 圆检测
import cv2import numpy as np
planets =
cv2.imread('planet_glow.jpg')gray_img
= cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY)img
= cv2.medianBlur(gray_img, 5)cimg
= cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)circles
=cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,120,
param1=100,param2=30,minRadius=0,
maxRadius=0)circles = np.uint16(np.around(circles))for i in circles[0,:]:
# draw the outer circle
cv2.circle(planets,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(planets,(i[0],i[1]),2,(0,0,255),3)
cv2.imwrite("planets_circles.jpg", planets)
cv2.imshow("HoughCirlces", planets)
cv2.waitKey()cv2.destroyAllWindows()
四
总结
《OpenCV 3计算机视觉(python语言实现第2版)》早已看完,但是如不常用,一些东西可能记得不太清,俗话说好记性不如烂笔头,所以稍稍做个笔记,等哪天用到了当个工具查询使用。
END
数据轩
科技 | 应用 | 趋势
www.jx-lab.com
以上是关于OpenCV3学习笔记整理(二期)的主要内容,如果未能解决你的问题,请参考以下文章
《OpenCV3编程入门》学习笔记三:HighGUI图形用户界面