OpenCV C++中的Mat格式灰度图转Python中的Image
import numpy as np
#height、width为图片高、宽
#mat_data为二进制数据
#dtype与原Mat格式对应
#转为灰度图
image = np.frombuffer(mat_data, dtype=np.int16).reshape(height, width)
灰度图转RGB图
import cv2
import numpy as np
image_rgb = np.zeros(gray_image.shape, dtype = np.uint8)
#灰度图为uint16, 转成uint8的图片
for index in range(len(gray_image)):
image_rgb[index] = gray_image[index] >> 2
image_rgb = cv2.cvtColor(image_rgb, cv2.COLOR_GRAY2BGR)
RGBA图片Byte数据转Python中的RGBA图
import numpy as np
image = np.frombuffer(image_file, dtype=np.uint8).reshape(image_height, image_width, 4)
RGBA图转RGB图
import cv2
image_rgb = cv2.cvtColor(image_rgba, cv2.COLOR_RGBA2BGR)