Read, display, and write images in open-cv

Determine whether your pytorch virtual environment has cv2 installed and can be usedpip list

1

.Read the image: imread()
The first parameter of the imread() method is passed in the path of the image file . If the read is successful, the return value is an array of numpy() type.
Failure to obtain the image will not throw an exception, but will get a None

import cv2
img = cv2.imread('图像的路径')
print('type(img)',img)
#返回值numpy数组

The second parameter has 3 modes

cv2.IMREAD_COLOR	缺省方式,读取图像为BGR 8-bit 格式.  这里需要注意opencv默认通道为BGR模式
cv2.IMREAD_UNCHANGED	图像格式不做任何改变,可用在读取带alpha通道的图片
cv2.IMREAD_GRAYSCALE	读取图像为转换为灰度图
import cv2
img = cv2.imread('图像路径',cv2.IMREAD_UNCHANGED)
print(img.shape)

5125123

Note: The width of the image is width = cols = img.shape[1]; the height of the image is height = rows = img.shape[0]

2

Display image imshow()
imshow()is used to display pictures. The first parameter is the name of the display window. The second parameter is the imread()image instance generated by the wait method.
waitKey()If the parameter passed in is 0, it will wait indefinitely until any key is pressed, or other values ​​are passed in. The parameter indicates the waiting time, unit is ms. After the time is over, the image display window will be closed.

import sys
import cv2
img = cv2.imread('图片路径',cv2.IMREAD_UNCHANGED)
	if img is None:
		print('没有读入图像')
		sys.exit(-1)#异常终止,退出程序
cv2.imshow('名称',img)
cv2.waitKey(1000)

Resize window
can be used nameWindow(), resizeWindow()
nameWindow(窗口名称,cv2.WINDOW_NORMAL)named window
resizeWindow(窗口名称,窗口宽度,窗口高度)zoom window

rows,cols = img.shape[0],img.shape[1]

cv2.namedWindow('lena-down', cv2.WINDOW_NORMAL)
cv2.resizeWindow('lena-down',(int(cols*0.5),int(rows*0.5)))
cv2.imshow('lena-down',img)

cv2.namedWindow('lena-up', cv2.WINDOW_NORMAL) 
cv2.resizeWindow('lena-up',(int(cols*1.3),int(rows*1.3)))
cv2.imshow('lena-up',img)

destroy window

cv2.waitKey(0)
cv2.destroyWindow('窗口名称') #关闭单个窗口
cv2.waitKey(0)
cv2.destroyAllWindows()  #关闭所有显示窗口

A simple display image package

dbg_is_show = False
def show_img(win_name,img,wait_time=0,img_ratio=0.15,is_show=True):
    if is_show is not True:
        return 
    rows = img.shape[0]
    cols = img.shape[1]
    cv2.namedWindow(win_name, cv2.WINDOW_NORMAL )#cv2.WINDOW_AUTOSIZE)
    cv2.resizeWindow(win_name,(int(cols*img_ratio),int(rows*img_ratio)))
    #这里先是图像的宽度,宽度是cols = img.shape[1]
    cv2.imshow(win_name,img)
    cv2.waitKey(wait_time)

3.

imwrite()
imwrite()The first parameter when writing a picture
is the name of the file to be written. The suffix ** of the file name determines the format of the picture file. The second parameter is the image instance.

cv2.imwrite('lena.bmp',img)    
cv2.imwrite('lena.png',img) 
#将imread()读出的图像实例分别写入为bmp和jpg格式的文件

4.

The data read by cv2 is directly displayed with matplotlib, which will cause color confusion. The reason is that the data read by cv2 is organized in BGR format, and the image displayed by matplotlib needs to be organized in RGB format, so the read numpy data needs to be converted:

import numpy as np
import matplotlib.pyplot as plt
import cv2

img = cv2.imread('lena.jpg')  #opencv读取文件图片 
print('type(img):',type(img))
print(img.shape)
img2=img.copy()
img2[:,:,2]=img[:,:,0]
img2[:,:,0]=img[:,:,2]
plt.imshow(img2) #imshow
plt.show()

Guess you like

Origin blog.csdn.net/aqiangdeba/article/details/129748902