Yongxing notes -OpenCV-1 Basic Operation

What OpenCV that?
OpenCV is based on a BSD license (open source) issue of cross-platform computer vision library that can run on Linux, Windows, Android and Mac OS operating systems. It is lightweight and efficient - consists of a series of C functions and a small amount of C ++ classes, while providing an interface Python, Ruby, MATLAB language, etc., many common algorithms implemented image processing and computer vision.
This article column will learn OpenCV-Python with you

#建议在pip中安装OpenCV-python
pip install opencv-python
#也可中anconda环境中安装
conda install opencv

Click on the picture to open recommended reading:
Here Insert Picture Description

1, the image reading:
cv2.imread (filename, the flags = None)

  • filename: Path picture
  • flags: reading mode
flags description
cv2.IMREAD_COLOR Loaded three-channel color image, ignoring transparency
cv2.IMREAD_GRAYSCALE Loading image grayscale
cv2.IMREAD_UNCHANGED Loading an alpha channel image, the display of transparency and translucency (Channel 4) image

Alpha Channel: is transparency and translucency of a picture. A 32-bit image stored in each 8 bits red, green and blue, and alpha channel. In this case, light may not represent a transparent or opaque, may also represent the alpha channel 256 of translucency.
2, the display image:
cv2.imshow (winName, MAT)

  • winname: name of the image display window
  • mat: takes a picture display of object names

3, wait for any key:
cv2.waitKey (Delay = None)

  • delay: Delay time
    when dalay = 0: indicates forever never exit
    Dalay units of: ms
    ord (c)
  • c:字符
    转换为 Unicode 值
    Here Insert Picture Description
    ESC键的 ASCLL 值为 27
    #图像等待 delay>0: 延时 单位为毫秒 delay<=0: 无限等待键盘输入

4、关闭窗口:
关闭所有窗口:
cv2.destroyAllWindows()
关闭指定窗口:
destroyWindows(wname)

5、图像的保存:
cv2.imwrite(filename,img,params=None)

  • filename : 文件夹的路径
  • params:此参数针对特定的图片格式
    对于JPEG,其表示的是图像的质量,用0-100的整数表示,默认为95。 注意,cv2.IMWRITE_JPEG_QUALITY类型为Long,必须转换成int
    PNG,第三个参数表示的是压缩级别。cv2.IMWRITE_PNG_COMPRESSION,从0到9,压缩级别越高,图像尺寸越小。默认级别为3:
import cv2
img1 = cv2.imread("first.jpg")
cv2.imshow("windowsimg1",img1)
print(chr(27))
k = cv2.waitKey()
if k == 27 :
    print("1")
    cv2.destroyAllWindows()
elif k == ord('s'):
    cv2.imwrite("second.jpg",img1)
    cv2.destroyAllWindows()

6、图像通道的分离与合并:
一般情况下图片通道的排序为:RGB 。 但是在OpenCV中默认的排序为 BGR
分离通道:
cv2.split(m,mv=None)

  • m:需要分离通道的图像对象
import cv2
img = cv2.imread("first.jpg",cv2.IMREAD_UNCHANGED)
b,g,r = cv2.split(img) #彩色图像三通道,灰度图像单通道
cv2.imshow("b",b)
cv2.imshow("g",g)
cv2.imshow("r",r)
cv2.waitKey()
cv2.destroyAllWindows()

7、获取图像属性:
image.shape
(行数,列数,通道数)
image.size
输出图像像素总和

import cv2
img = cv2.imread("first.jpg",cv2.IMREAD_UNCHANGED)
print(img.shape)
print(img.size)

生成全0数组:
np.zeros(shape,dtype=float)

  • shape:数组的属性
  • dtype: Type

8, the combined channel
cv2.merge (mv, dst = None)

  • mv: The combined channel
import cv2
import numpy as np
img = cv2.imread("first.jpg",cv2.IMREAD_UNCHANGED)
zreo = np.zeros(img.shape[:2],dtype="uint8") #大小与img图像相同的生成二阶全0数组
#格式为uint8否则imshow不能显示
b,g,r = cv2.split(img)
cv2.imshow("blue",cv2.merge([b,zreo,zreo]))
cv2.imshow("green",cv2.merge([zreo,g,zreo]))
cv2.imshow("red",cv2.merge([zreo,zreo,r]))
cv2.waitKey()
cv2.destroyAllWindows()

5, ROI region of interest:
is simply a slice image array to obtain a region of interest

import cv2
img = cv2.imread("first.jpg")
imgROI = img[123:412,234:879] #设置感兴趣区域
cv2.imshow("ROI",imgROI)
cv2.waitKey()
cv2.destroyAllWindows()

Exercises:
were read in a color mode and a model image grayscale, RGB channels separated color image to obtain three image, and then sequentially shows four images displayed by the image s save the image, the image name for the image color, final color image synthesized channel display, press "q" key off, and then the display areas of interest RIO, press ESC to close. The final properties of the image output.

Comment out your answer

Published 45 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/m0_43505377/article/details/103746264