OpenCV 03 (data structure--Mat)

1. Introduction to Mat

Mat is a data structure used by OpenCV to represent image data in the C++ language. It is converted into a numpy ndarray in python. Mat consists of header and data. The header records the dimension, size, data type and other data of the image.

1.1 Mat copy

- Mat shared data

 In python, Mat data corresponds to numpy's ndarray, and the copy of Mat can be realized using the deep and shallow copy methods provided by numpy.

import cv2
import numpy as np

img = cv2.imread('./cat.jpeg')

#浅拷贝
img2 = img.view()

#深拷贝
img3 = img.copy()

img[10:100, 10:100] = [0, 0, 255]

cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.imshow('img3', img3)

cv2.waitKey(0)
cv2.destroyAllWindows()

# cv2.imshow('img', img)
# cv2.imshow('img2', img2)
# cv2.imshow('img3', img3)

Replace with:
cv2.imshow('img', np.hstack((img, img2, img3))) #hstack is stacked horizontally

cv2.imshow('img', np.vstack((img, img2, img3))) #vstack is stacked vertically

1.2 Access the properties of the image (Mat)

Mat in OpenCV has been converted into ndarray in python, and the properties of the Mat image can be accessed through the properties of ndarray.

import cv2
import numpy as np

img = cv2.imread('cat.jpeg')

#shape属性中包括了三个信息
#高度,长度 和 通道数
print(img.shape)

#图像占用多大空间
#高度 * 长度 * 通道数
print(img.size)

#图像中每个元素的位深
print(img.dtype)

Guess you like

Origin blog.csdn.net/peng_258/article/details/132766631