【OpenCV-Python】——Basic Image Processing & Reading, Writing, and Displaying Images & Reading, Reading, and Playing Videos & Grayscale/Color Images/Image Channel Operations and Operations

Table of contents

Foreword:

1. Read and display images, write images

2. Read and play video, write video

3. Manipulate grayscale and color images, image channel operations, and calculations

Summarize:


Foreword:

In Python, OpenCV uses NumPy arrays to store images. Numpy is a software package for array calculations using Python. It provides powerful N-dimensional array objects, supports complex broadcast functions (array operations), and integrates C/C++ and Fortran code tools. It supports features such as linear algebra, Fourier transform, and random numbers, and can also be used as an efficient multi-dimensional container for general data, such as displaying images in OpenCV.

See the data processing column for Numpy's knowledge points, mainly including data types, creating arrays, array shapes, index slicing and iteration, array operations, etc.

1. Read and display images, write images

imread reads the image of the folder into the memory, and supports BMP, PNG, JPEG, TIFF and other static image formats. This function returns a numpy.ndarray object (that is, a NumPy array), the elements are image pixels, and the shape of the array (shape) , dtype (data type), size (number of elements) and other attributes represent the relevant attributes of the image.

Common basic reading formats include cv2.IMREAD_UNCHANGE (load as it is), cv2.IMREAD_GRAYSCALE (convert image to single-channel grayscale image), cv2.IMREAD_COLOR (convert image to 3-channel BGR color map), etc.

imwrite writes the image saved in the Numpy array to the file. The function returns a Boolean value. If the write is successful, it returns true, otherwise false.

import cv2
import numpy as np
img1=cv2.imread('autumn.jpg')#可加读取格式参数
cv2.imshow('autumn',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

#也可打包成一个函数
def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

cv_show('autumn',img1)

#写入一个50x50的黑色正方形图像
img2=numpy.zeros((50,50),dtype=numpy.uint8)
cv2.imwrite("rectangle.png",img2)

2. Read and play video, write video

The basic steps of video processing: use the video file or camera as the data source to create a VideoCapture object; call the read() method of the VideoCapture object to obtain frames in the video, each frame is an image; call the write() method of the VideoWriter object to Write the frame to the video file, or call the cv2.imshow() function to display the frame directly in the window.

#播放文件已有的视频
import cv2
cap=cv2.VideoCapture("test.mp4")#创建对象
#fps=cap.get(cv2.CAP_PROP_FPS)#读取帧速率
#size=(cap.get(cv2.CAP_PROP_FRAME_HEIGHT),cap.get(cv2.CAP_PROP_FRAME_WIDTH))#读取视频大小
#print('帧速率:',fps)
#print('大小:',size)
ret, frame = cap.read()#从视频中读取一帧数据,frame存储了此一帧数据
while True:
    cv2.imshow('img',frame)
    ret, frame = cap.read()#从视频中读取一帧数据,frame存储了此一帧数据
    key = cv2.waitKey(1000//24)#延时时间:1000ms显示24张图片,最多等待1000//24毫秒
    if key == ord('q'):#按q退出
        break
cap.release()#关闭视频
#将已有的视频写入文件
import cv2
cap=cv2.VideoCapture("test.mp4")#创建对象,test.mp4是已存在的视频
fps=cap.get(cv2.CAP_PROP_FPS)#读取帧速率
size=(int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)))#读取视频大小,int强制转换
wrt=cv2.VideoWriter('test2.avi',cv2.VideoWriter_fourcc('X','V','I','D'),fps,size)#保存文件名,视频解码器格式,帧速率,大小
ret, frame = cap.read()#读取一帧数据,frame存储了此一帧数据,ret是返回的布尔值
while ret:
    wrt.write(frame)
    ret, frame = cap.read()
cap.release()#关闭视频
#捕获摄像头视频,保存并播放
import cv2
cap=cv2.VideoCapture(0)#创建对象,0代表视频源是摄像头
fps=30                 #预设帧速率
size=(int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)))#读取视频大小
wrt=cv2.VideoWriter('test2.avi',cv2.VideoWriter_fourcc('X','V','I','D'),fps,size)#保存文件名,视频解码器格式,帧速率,大小
ret, frame = cap.read()#从视频中读取一帧数据,frame存储了此一帧数据
while ret:
    wrt.write(frame)#将帧写入文件
    cv2.imshow('img',frame)
    ret, frame = cap.read()#从视频中读取一帧数据,frame存储了此一帧数据
    key = cv2.waitKey(1000//24)#延时时间:1000ms显示24张图片,最多等待1000//24毫秒
    if key == ord('q'):#按q退出
        break
cap.release()#关闭视频(释放摄像头)

3. Manipulate grayscale and color images, image channel operations, and calculations

The grayscale image has 256 levels (0-255), 0 represents black, 255 represents white , one byte stores one pixel value, and the image is represented by a single-channel two-dimensional array. The pixel value will not be greater than 255, if it is greater than 256 will be taken modulo.

#代码示例:首先闯将240x320的黑色图像,并输出当前灰度值。每过1秒,灰度值增加20,直到按下esc退出
img=numpy.zeros((240,320),dtype=numpy.uint8)
n=0
while true:
    cv2.imshow('Grayimg',img)
    n+=20
    img[:,:]=n#将所有像素点值修改为n
    print(img[1,1])
    key=cv2.waitKey(1000)
    if key==27:
        break

Color image RGB has three channels, and the value range of each channel is 0-255. The combination of three channel pixels represents a color image, but the default image format in opencv is BGR , and a three-dimensional array is used to represent a color image.

#代码示例:三条颜色动态变化效果
img=numpy.zeros((240,320,3),dtype=numpy.uint8)#240是宽,320是长
r0=0
r1=1
r2=2
while true:
    img[:80,:,r0]=255      #通道r0,将上部1/3颜色值设置为255
    img[80:160,:,r1]=255   #通道r1,将中部部1/3颜色值设置为255
    img[160:,:,r2]=255     #通道r2,将下部1/3颜色值设置为255
    cv2.imshow('Colorimg',img)
    key=cv2.waitKey(1000)
    img[:,:,:]=0   #清零变为黑色图像
    t=r0  #轮换通道
    r0=r1
    r1=r2
    r2=t
    if key==27:
        break

The channel operation is to split and merge the color channels. The image in BGR format in opencv is a three-dimensional array, and the channel can be split by the array index operation. split separation, merge merger .

b,g,r = cv2.split(img3)#分离cv2的BGR颜色通道索引为0,1,2,matplotlib为RBG
#print(b,g,r)#一串矩阵
#print(r.shape)
img4=cv2.merge((b,g,r))#合并B、G、R单通道为三通道彩色图像
#print(img4.shape)
cur_img1=img3.copy()
cur_img1[:,:,1]=0
cur_img1[:,:,2]=0
cv_show('B',cur_img1)#只保留B通道
cur_img2=img3.copy()
cur_img2[:,:,0]=0
cur_img2[:,:,2]=0
cv_show('G',cur_img2)#只保留G通道
cur_img3=img3.copy()
cur_img3[:,:,0] = 0
cur_img3[:,:,1] = 0
cv_show('R',cur_img3)#只保留R通道

Operations are divided into addition operations, weighted addition operations, and bit operations . There are two types of addition operations: "+" and cv2.add() functions. After "+" is executed, if the result is greater than 256, it will be modulo 256; after cv2.add() is executed, if the result is greater than 256, it will be 255. Weighted addition uses the function cv2.addWeighted(image 1, weight 1, image 2, weight 2, additional value). There are four types of bitwise operations: bitwise and cv2.bitwise_and (Figure 1, Figure 2, mask), bitwise or bitwise_or, bitwise inversion bitwise_not, and bitwise exclusive OR bitwise_xor. Bitwise AND is often used to create mask images.

Summarize:

As a beginner, there may be incomplete summaries in it, and the article will be improved after further in-depth study! Next time learn GUI !

Guess you like

Origin blog.csdn.net/weixin_51658186/article/details/130336220