Python-Opencv 基本操作
程序员文章站
2022-06-27 23:31:07
下面完整代码在github仓库:传送门文章目录一、在图片上画线二、图像通道BGR转换RGB三、画矩形框四、图片变形五、opencv转换到PIL六、利用opencv播放视频七、PIL转换到Opencv八、合成图片通道九、给视频图像加滤波十、视频图像变灰度十一、找视频图像轮廓十二、通道分离十三、通道填值一、在图片上画线import cv2img = cv2.imread("1.jpg", 0) # 默认1为真彩色,0为灰色# opencv的图像模式是BGRcv2.line(img, (10...
下面完整代码在github仓库:传送门
文章目录
一、在图片上画线
import cv2
img = cv2.imread("1.jpg", 0) # 默认1为真彩色,0为灰色
# opencv的图像模式是BGR
cv2.line(img, (10, 10), (100, 100), [0, 0, 255], 3)
cv2.imshow("", img)
cv2.waitKey(10000)
cv2.destroyAllWindows()
二、图像通道BGR转换RGB
import cv2
from PIL import Image
img = cv2.imread("1.jpg")
cv2.line(img, (10, 10), (100, 100), [0, 0, 255], 3)
cv2.rectangle(img, (10, 10), (100, 100), [0, 0, 255], 3)
# h, w, c
# img = img[:, :, ::-1] # ::-1表示将RGB倒序输出
# BGR通道的图像转换成RGB的图像
img = img[..., ::-1]
img = Image.fromarray(img)
img.show()
三、画矩形框
import cv2
from PIL import Image
img = cv2.imread("1.jpg")
cv2.line(img, (10, 10), (100, 100), [0, 0, 255], 3)
cv2.rectangle(img, (10, 10), (100, 100), [0, 0, 255], 3)
cv2.imshow("", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
四、图片变形
import cv2
img = cv2.imread("1.jpg")
# print(type(img))
# print(img)
img_size = img.shape
print(img_size)
img = cv2.resize(img, (img_size[1]//2, img_size[0]//2))
cv2.imshow("img", img)
cv2.waitKey(10000)
cv2.destroyAllWindows()
cv2.imwrite("2.jpg", img)
五、opencv转换到PIL
import cv2
from PIL import Image
import numpy as np
# 从opencv转换到PIL
img_arr = cv2.imread("3.jpeg")
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2RGB) # 改变通道
img = Image.fromarray(img_arr) # 改变图像数据类型
img.show()
六、利用opencv播放视频
import cv2
path = "1.mp4" # 本地视频路径
# path = r"http://vfx.mtime.cn/Video/2019/03/19/mp4/190319125415785691.mp4" # 在线视频路径
# cap = cv2.VideoCapture(0) # 调取内置摄像头
cap = cv2.VideoCapture(path) # 获取视频对象
fps = cap.get(cv2.CAP_PROP_FPS) # 从视频对象中获取帧数
print(fps)
w = int(cap.get(3)) # 获取图片的宽度
h = int(cap.get(4)) # 获取图片的高度
# print(w)
# print(h)
fourc = cv2.VideoWriter_fourcc(*"DVIX") # 视频格式
out = cv2.VideoWriter("2.mp4", fourc, fps, (w, h)) # 写入视频格式
font = cv2.FONT_HERSHEY_COMPLEX
# frame表示读出的每一张图片, ret表示这一张图片是否存在
while True:
ret, frame = cap.read()
# 将十六进制数据转成 二进制数据
if cv2.waitKey(int(1000 / fps)) & 0xFF == ord("q"): # 视频在播放的过程中按键,循环会中断)。
break
elif ret == False: # 视频播放完了,循环自动中断。
break
cv2.rectangle(frame, (200, 50), (300, 150), [0, 0, 255], 3)
out.write(frame)
# 前面的1表示字体大小,后面的1表示字体厚度, lineType抗锯齿
cv2.putText(frame, "beautiful girl 啊", (100, 100), font, 1, (0, 0, 255), 1, lineType=cv2.LINE_AA)
cv2.imshow("", frame)
cap.release() # 将视频关了
cv2.destroyAllWindows()
七、PIL转换到Opencv
import cv2
from PIL import Image
import numpy as np
# 从PIL转换到opencv
img = Image.open("3.jpeg")
img_arr = np.array(img) # 从图像数据转成numpy数据类型
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_RGB2BGR) # 转通道
cv2.imshow("", img_arr)
cv2.waitKey(0)
img_arr = img_arr[..., ::-1]
cv2.imshow("", img_arr)
cv2.waitKey(0)
cv2.destroyAllWindows()
八、合成图片通道
import cv2
from PIL import Image
img = cv2.imread("1.jpg")
b, g, r = cv2.split(img)
cv2.imshow("b", b) # 显示灰度图
cv2.imshow("g", g)
cv2.imshow("r", r)
img = cv2.merge([g, r, b]) # 合成通道
cv2.imshow("", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
九、给视频图像加滤波
import cv2
import numpy as np
from PIL import Image, ImageFilter
cap = cv2.VideoCapture("1.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
while True:
ret, frame = cap.read()
img_arrs = frame[..., ::-1]
imgs = Image.fromarray(img_arrs)
img = imgs.filter(ImageFilter.MinFilter(5))
img_arr = np.array(img)[..., ::-1]
cv2.imshow("", img_arr)
if cv2.waitKey(int(1000/fps)) & 0xFF == ord("q"):
break
elif ret == False:
break
cap.release() # 关闭视频
cv2.destroyAllWindows() # 关闭窗口
十、视频图像变灰度
import cv2
import numpy as np
from PIL import Image, ImageFilter
cap = cv2.VideoCapture("1.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
while True:
ret, frame = cap.read()
# img1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
img1 = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # HSV是连续的色彩空间
cv2.imshow("", img1)
if cv2.waitKey(int(1000/fps)) & 0xFF == ord("q"):
break
elif ret == False:
break
cap.release() # 关闭视频
cv2.destroyAllWindows() # 关闭窗口
十一、找视频图像轮廓
import cv2
import numpy as np
from PIL import Image, ImageFilter
cap = cv2.VideoCapture("1.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
while True:
ret, frame = cap.read()
img_arr = frame[..., ::-1]
img1 = Image.fromarray(img_arr)
# img2 = img1.filter(ImageFilter.CONTOUR) # 图像轮廓
# img2 = img1.filter(ImageFilter.EMBOSS)
img2 = cv2.cvtColor(img_arr, cv2.COLOR_RGB2GRAY)
img3_arr = np.array(img2)[..., ::-1] # 转数据类型和通道
cv2.imshow("", img3_arr)
if cv2.waitKey(int(1000/fps)) & 0xFF == ord("q"):
break
elif ret == False:
break
cap.release() # 关闭视频
cv2.destroyAllWindows() # 关闭窗口
十二、通道分离
import cv2
import numpy as np
img_arr = np.empty([400, 400, 3], np.uint8) # 生成空矩阵
print(img_arr)
# [0, 0, 255]
img_arr[..., 0] = 0
img_arr[..., 1] = 0
img_arr[..., 2] = 255 # 将第三个通道的像素值填为255
cv2.imshow("", img_arr)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
from PIL import Image
# 将图像变为单通道
img = cv2.imread("1.jpg")
img[..., 1] = 0
img[..., 2] = 0
cv2.imshow("", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
十三、通道填值
import cv2
import numpy as np
img_arr = np.zeros([400, 400, 3], np.uint8)
# img_arr = np.ones([400, 400, 3])
# img_arr = np.arange(400*400*3).reshape([400, 400, 3]) / (400*400*3)
# img_arr = np.random.rand(400, 400, 3)
# img_arr = np.random.randn(400, 400, 3)
# img_arr = np.random.normal(0, 0.5, (400, 400, 3))
cv2.imshow("", img_arr) # opencv中 像素值填0-1之间的值
cv2.waitKey(0)
cv2.destroyAllWindows()
本文地址:https://blog.csdn.net/liu1073811240/article/details/109902964
上一篇: k-近邻(knn)算法