opencv学习(五)图像格式转换

import cv2 as cv


def color_space_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    cv.imshow("gray", gray)
    rgb = cv.cvtColor(image, cv.COLOR_BGR2RGB)
    cv.imshow("rgb", rgb)
    hsv = cv.cvtColor(image, cv.COLOR_RGB2HSV)
    cv.imshow("hsv", hsv)
    hls = cv.cvtColor(image, cv.COLOR_RGB2HLS)
    cv.imshow("hls", hls)
    ycrcb = cv.cvtColor(image, cv.COLOR_RGB2YCrCb)
    cv.imshow("ycrcb", ycrcb)
    yuv = cv.cvtColor(image, cv.COLOR_BGR2YUV)
    cv.imshow("yuv", yuv)


src = cv.imread("D:\ophotos\out3.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
color_space_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

结果如下opencv学习(五)图像格式转换


案例:播放视频


import cv2 as cv
import numpy as np


def extract_object_demo():
    caputer = cv.VideoCapture("D:/opencv_study/12345.mp4")
    while (True):
        ret, frame = caputer.read()
        if ret == False:
            break
        hsv = cv.cvtColor(frame, cv.COLOR_RGB2HSV)
        cv.imshow("vedio", frame)
        lower_hsv = np.array([37, 43, 46])
        upper_hsv = np.array([77, 255, 255])
        mask = cv.inRange(hsv, lower_hsv, upper_hsv)
        dst = cv.bitwise_and(frame,frame, mask=255-mask)
        cv.imshow("mask", dst)
        c = cv.waitKey(40)
        if c == 27:
            break


extract_object_demo()
cv.waitKey(0)
cv.destroyAllWindows()

将图片格式拆开(b,g,r),再合回来

import cv2 as cv

src = cv.imread("D:\ophotos\m.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
b, g, r = cv.split(src)  # 拆
cv.imshow("blue", b)
cv.imshow("green", g)
cv.imshow("red", r)
src[:, :, 0] = 0 # 将b层所有点值赋为0
cv.imshow("changed image1", src)
src1 = cv.merge([b, g, r])  # 和
cv.imshow("changed image2", src1)
cv.waitKey(0)
cv.destroyAllWindows()

结果:无,可自行下载运行

上一篇:Nullable 结构 |T? 可为空的值类型


下一篇:INTEWORK-EAS—可靠、完善的AUTOSAR解决方案