用于日常处理图片的opencv-python小程序

用于视频读帧,并对视频某块像素点区域像素值进行处理

path = "C:/Users/Pictures/Saved Pictures/lu.mp4"                 # 视频路径
cap = cv2.VideoCapture(path)
count = 1
cnt = 0
while cv2.waitKey(50) != 27:
    t1 = time.time()
    ret, img = cap.read()
    fps = cap.get(cv2.CAP_PROP_FPS)
    print("fps", fps)
    count += 1                                                  # 计数器,计帧
    print("count", count)
    # 对视频某块像素点区域进行像素值处理,可用于隐藏信息遮挡
    """img[280:320, 1247:1370] = (255, 255, 255)
    img[320:400, 1247:1400] = (255, 255, 255)"""
    if count % 7 == 0:                                           # 每隔多少帧读取一张图片
        cnt += 1
        cv2.imwrite("C:/UsersDesktop/img/"+str(cnt)+"o13.jpg", img)    # 可自定义依次命名图片
    cv2.imshow("video", img)
    # cv2.waitKey(1)

用于视频分辨率缩放和帧率的控制

path = "C:/Users/Pictures/Saved Pictures/read.mp4"     # 原视频路径
cap = cv2.VideoCapture(path)
fps = 25                # 需要的帧率大小
size = (1844, 766)      # 需要的分辨率大小
getvideo = cv2.VideoWriter("C:/Users/Pictures/Saved Pictures/readi.avi",
                           cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), fps, size)
while cv2.waitKey(1) != 27:
    t1 = time.time()
    ret, img = cap.read()
    if ret:
        # 可对视频某块像素点区域进行像素值处理
        """img[280:320, 1247:1370] = (255, 255, 255)
        img[320:400, 1247:1400] = (255, 255, 255)"""
        dst = cv2.resize(img, size)
        getvideo.write(dst)
    cv2.imshow("video", img)

用于图片合成(也可以进行图像二值化,通道分离,保存处理过后的图片)

src = cv2.imread("C:/Users/Pictures/work2/45.jpg")
dst = cv2.imread("C:/Users/Pictures/work2/46.jpg")
# src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# gray = cv2.equalizeHist(src)
# src = src.resize(1920, 1080)
# bh, bs, bv = cv2.split(src)
# rh, rs, rv = cv2.split(dst)
# src = cv2.resize(src, (1920, 1080))
# cv2.imshow("gray", gray)
img = src[15:75, 170:210]
dmg = dst[770:1270, 420:750]
getimg = cv2.resize(dmg, (40, 60))
# src[280:320, 800:900] = (255, 255, 255)
# cv2.threshold(src[280:400, 1250:1400], 0, 255, cv2.THRESH_BINARY)
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.namedWindow("src", cv2.WINDOW_NORMAL)
# cv2.imwrite("C:/Users/Pictures/Saved Pictures/21.jpg", src)
cv2.imshow("src", dst)
cv2.imshow("img", getimg)
print("img", getimg.shape[0], getimg.shape[1])
src[15:75, 170:210] = getimg
cv2.imshow("ppt", src)
cv2.imwrite("C:/Users/Pictures/work2/47.jpg", src)
cv2.waitKey(0)
cv2.destroyAllWindows()

本文仅作小白日常外部处理图片方便用途,不用于技术探讨,本人才疏学浅望读者见谅

你可能感兴趣的:(opencv,python,计算机视觉)