Opencv study notes - basic image operations


foreword

Let me talk about some basic knowledge of images first:

(1) An image is composed of pixels, and a pixel is a point on the image.

(2) Common images include 1-channel (grayscale image), 3-channel (color image), and 4-channel (transparent image). The pixel value range of each channel is [0, 255]. Color images are our most common , which means that each pixel is composed of three values ​​of B, G, and R (that is, the three primary colors), such as (255, 255, 255) for white.insert image description here

1. Data reading-image

1. Read the image

opencv reads the picture of the specified path and displays it

import cv2 #opencv读取的格式是BGR

img=cv2.imread('D:\kaibai.jpg',cv2.IMREAD_COLOR)#读取彩色图片
cv2.imshow('kaibai',img)#显示图像的名字,显示图片
cv2.waitKey(0)#等待时间,毫秒级,0表示任意键终止
cv2.destroyAllWindows()#触发关闭后,关闭图像

Effect:
insert image description here
convert the image into a grayscale image and display it

import cv2 #opencv读取的格式是BGR

img=cv2.imread('D:\kaibai.jpg',cv2.IMREAD_GRAYSCALE)#读取图片转成灰度图像
cv2.imshow('kaibai',img)#显示图像的名字,显示图片
cv2.waitKey(0)#等待时间,毫秒级,0表示任意键终止
cv2.destroyAllWindows()#触发关闭后,关闭图像

Effect:
insert image description here
The converted image can be saved by the following code:

cv2.imwrite('path',img)

path: the path to save the picture, img: the picture to be saved

There are also some basic operations:

img.shape#img.shape[0]:图像的垂直尺寸(高度);img.shape[1]:图像的水平尺寸(宽度);img.shape[2]:图像的通道数
type(img)#查看图片格式
img.size#查看图片像素点个数
img.dtype#查看数据类型

2. Read part of the image

Read part of the image rather cropped, specify the length and width of the image, and then read.

import cv2 #opencv读取的格式是BGR

img=cv2.imread('D:\kaibai.jpg',cv2.IMREAD_COLOR)#读取图片
bord=img[0:200,0:200]
cv2.imshow('kaibai',bord)#显示图像的名字,显示图片
cv2.waitKey(0)#等待时间,毫秒级,0表示任意键终止
cv2.destroyAllWindows()#触发关闭后,关闭图像

Effect:
insert image description here

2. Data reading - video

Principle: OpenCV reads video including reading from video files and cameras. Reading video is actually reading each frame, which is equivalent to reading and displaying each frame as an image. cv2.VideoCapture can capture the camera and use numbers to control different devices, such as 0,1. If it is a video file, just specify the path directly.

import cv2

video = cv2.VideoCapture('D:\Video\大肥猫.mp4')#读取视频文件
# 检查是否打开正确
if video.isOpened():
    oepn,frame = video.read()
else:
    open = False
#如果打开正确
while open:
    ret, frame = video.read()
    if frame is None:
        break
    if ret == True:
        gray = cv2.cvtColor(frame,  cv2.IMREAD_COLOR)
        cv2.imshow('result', gray)
        if cv2.waitKey(100) & 0xFF == 27:#按esc退出
            break
video.release()
cv2.destroyAllWindows()#关闭窗口

cv2.waitKey() is the waiting time, in milliseconds, 0 means that any key is terminated, and the next frame will be switched when the time is up. The slower the time, the fuller the video effect we see, and the video may not be seen if it is too fast. Equivalent to seconds off.
Effect:

opencv video read

3. Color channel extraction

When using opencv for channel extraction, pay attention to the order of extraction, because the image format read by opencv is BGR instead of RGB. One color in BGR can be reserved separately to see the effect.

import cv2 #opencv读取的格式是BGR
def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
img=cv2.imread('D:\kaibai.jpg',cv2.IMREAD_COLOR)#读取图片
b,g,r=cv2.split(img)
# 只保留R
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
cv_show('R',cur_img)
# 只保留G
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,2] = 0
cv_show('G',cur_img)
# 只保留B
cur_img = img.copy()
cur_img[:,:,1] = 0
cur_img[:,:,2] = 0
cv_show('B',cur_img)

Keep only the effect of R:
insert image description here

4. Boundary Filling

Boundary filling is equivalent to expanding the image by a circle based on the original image size.

import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt

img=cv2.imread('D:\kaibai.jpg',cv2.IMREAD_COLOR)#读取图片
top_size,bottom_size,left_size,right_size = (50,50,50,50)#上下左右分别填充的大小
#进行复制的补零操作, 只对边缘的点进行复制,然后该列上的点都是这些
replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
#进行翻转的补零操作,举例只对当前对应的边缘   gfedcba|abcdefgh|hgfedcb
reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_REFLECT)
#进行翻转的补零操作, gfedcb|abcdefgh|gfedcb
reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT_101)
#进行上下边缘调换的外包复制操作     bcdegh|abcdefgh|abcdefg
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_WRAP)
#默认下是填充黑色
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_CONSTANT, value=0)

plt.subplot(231), plt.imshow(img, 'gray'), plt.title('ORIGINAL')
plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('REPLICATE')
plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('REFLECT')
plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('REFLECT_101')
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')

plt.show()

BORDER_REPLICATE : Replication method, that is, copying the most edge pixels.

BORDER_REFLECT : reflection method, the pixels in the image of interest are copied on both sides, for example: gfedcba|abcdefgh|hgfedcb.

BORDER_REFLECT_101 : Reflection method, that is, taking the most edge pixel as the axis, symmetrical, gfedcb|abcdefgh|gfedcba.

BORDER_WRAP : Outer wrapping method cdefgh|abcdefgh|abcdefg.

BORDER_CONSTANT : Constant method, filled with constant value.

5. Numerical calculation

Only images with the same dimension can be added. For example, if a dimension is (414,500,3), a dimension is (429,499,3) and cannot be added.

import cv2 #opencv读取的格式是BGR


img_cat=cv2.imread('D:\cat.jpg',cv2.IMREAD_COLOR)#读取图片
img_dog=cv2.imread('D:\dog.jpg',cv2.IMREAD_COLOR)
 img_cat2= img_cat +10 #每个像素点都加10
 img_cat[:5,:,0]#只打印前5行
相当于% 256
(img_cat + img_cat2)[:5,:,0]
最大就是255,超过255后取255
cv2.add(img_cat,img_cat2)[:5,:,0]

6. Image Fusion

Image fusion is equivalent to superimposing two pictures together, and image fusion can only be fused with images of the same dimension.

import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt

img_cat=cv2.imread('D:\cat.jpg',cv2.IMREAD_COLOR)#读取图片
img_dog=cv2.imread('D:\dog.jpg',cv2.IMREAD_COLOR)
print(img_cat.shape)#(414, 500, 3)
print(img_dog.shape)#(429, 499, 3)
img_dog = cv2.resize(img_dog, (500, 414))#将狗的图像维度修改和猫的一样
#res = cv2.resize(img, (0, 0), fx=4, fy=4)#不指定图像的宽高,而是和原来的成比例
res = cv2.addWeighted(img_cat, 0.4, img_dog, 0.6, 0)
plt.imshow(res)
plt.show()

cv2.addWeighted(img_cat, 0.4, img_dog, 0.6, 0) where 0.4 is the weight of img_cat in the fusion image, 0.6 is the weight of img_dog in the fusion image, and 0 is the bias item.
Effect:
insert image description here

Guess you like

Origin blog.csdn.net/Thousand_drive/article/details/124461573