opencv的常见示例

#opencv是将图片打开为numpy array的形式
#PIL是将图片打开为PIL.Image的形式

#图片的读取、分割以及保存
import cv2
img = cv2.imread('test.png')
im_height, im_width, im_dep = img.shape
img_size = img.size #返回图像的像素数目,高*宽*深度
ROI = img[10:100, 20:50, :]
print(img_size)
cv2.imwrite('ROI_image.jpg', ROI)

#视频文件的读取与保存
import cv2
import numpy as np
cap = cv2.VideoCapture('video.mp4')
if cap.isOpened() is False:
print('Error')
exit(1)
frame_width = int(cap.get(3)) #获取图片帧宽度
frame_height = int(cap.get(4)) #获取图片帧高度

#创建保存视频,指定保存视频名称,指定视频编码器,视频帧率,图像帧尺寸
out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30, (frame_width, frame_height))
ret, frame = cap.read()
while ret:
cv2.imshow('frame', frame)
cv2.waitKey(20)
frame = cv2.flip(frame, 0)
out.write(frame)
ret, frame = cap.read()
cap.release()
out.release()

#图像常用变换
#色彩空间的变换

import cv2

img = cv2.imread('test.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Image",gray)
cv2.waitKey(0)

#图像的二值化
threshold = 155
_, binary_image = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY) # 将灰度转为二值图像
cv2.imshow("Image",binary_image)
cv2.waitKey(0)

#图像的旋转
img = cv2.imread('test.png')
width, height, depth = img.shape
angle = 60
scale = 1
M = cv2.getRotationMatrix2D((width/2, height/2), angle, scale)
# M为旋转矩阵,第一个参数是设定旋转中心,第二个参数是旋转角度(单位是度,逆时针为正),第三个参数是缩放比例
ratation = cv2.warpAffine(img, M, (width, height))
cv2.imshow('Image',ratation)
cv2.waitKey(0)

#图像的缩放
img = cv2.imread('test.png')
re_resize = cv2.resize(img, (600, 800), interpolation=cv2.INTER_NEAREST)
cv2.imshow('Image', re_resize)
cv2.waitKey(0)

#图像轮廓相关函数
#获取轮廓函数findCountours
import cv2
img = cv2.imread('test.png')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #图像灰度化
ret, thresh = cv2.threshold(imgray, 200, 255, cv2.THRESH_BINARY) #图像二值化
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #获取轮廓

扫描二维码关注公众号,回复: 8990536 查看本文章

#绘制轮廓函数drawContours
imm = cv2.drawContours(img, contours, -1, (100, 0, 0), 3) #将所有轮廓全都绘制到image上
#cv2.drawContours(image, [contours[1]], -1, (255, 0, 0), 3)
# cv2.imshow('Image', imm)
# cv2.waitKey(0)

#计算轮廓包围区域面积、行心
area = cv2.contourArea(contours[1])
M = cv2.moments(contours[1])
cx = M['m10'] / M['m00']
cy = M['m01'] / M['m00']
print(cx,cy)

#在图像上绘制矩形框(目标检测),和添加文字
import cv2
img = cv2.imread('test.png')
cv2.rectangle(img,(20,20),(60,60),(55,255,155),1) #参数:图片,左上角,右上角,线的颜色,线的粗细
cv2.putText(img, 'butterfly', (15, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.7,(0,255,0), 1, cv2.LINE_AA)
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.imwrite("add_text.jpg",img)
print(type(img))

#opencv读取视频

#视频总帧数为150,时长15秒,FPS为10,即总帧数=时长*FPS
import cv2
capture = cv2.VideoCapture("video.mp4")
fps = capture.get(cv2.CAP_PROP_FPS)
print('fps = ', fps)
total_s = capture.get(cv2.CAP_PROP_FRAME_COUNT)
print("total_s = ", total_s)
cv2.namedWindow("test", 0)
cv2.resizeWindow("test", 640, 480)
frame_index = 0
if capture.isOpened():
while True:
success, frame = capture.read()
wait_key = cv2.waitKeyEx(1)
if wait_key == 27:
break
if success:
frame_index += 1
cv2.imshow("test", frame)
else:
print("end")
break
print("frame_index = ", frame_index)

猜你喜欢

转载自www.cnblogs.com/xhw19950606/p/12273128.html