python-3.x - 将 .mat 文件转换为 cv 图像
问题描述
我有一个.mat
文件,想将其转换为 CV 图像格式,以便可以将其用于 CNN 模型。
我正在尝试获取 RGB/其他彩色图像而不是灰色。
我尝试执行以下操作(如下),但得到了灰度图像,但是当我使用 matplotlib 绘制实际的 mat 文件时,它不是灰度的。此外,该.mat
文件还有一个px_spacing
与图像数组不同的数组。我不确定这有什么帮助。
def mat_to_image(mat_image):
f = loadmat(mat_image,appendmat=True)
image = np.array(f.get('I')).astype(np.float32)
mean = image.mean()
std= image.std()
print(mean, std)
hi = np.max(image)
lo = np.min(image)
image = (((image - lo)/(hi-lo))*255).astype(np.uint8)
im = Image.fromarray(image,mode='RGB')
return im
images=mat_to_image(dir/filename)
cv_img = cv2.cvtColor(np.array(images), cv2.COLOR_GRAY2RGB)
通常绘制.mat
文件会获取非灰度(RGB 图像)
imgplot= plt.imshow(loadmat(img,appendmat=True).get('I'))
plt.show()
这是 mat 文件的外观print(loadmat('filename'))
{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Mon Sep 9 11:32:54 2019',
'__version__': '1.0',
'__globals__': [],
'I': array([
[ 81, 75, 74, 75, -11, 14, 49, 37, 29, -24, -183, -349, -581, -740],
[ 51, 33, 67, 36, 1, 42, 30, 49, 47, 42, 14, -85, -465, -727],
[ 23, 31, 36, 20, 54, 70, 44, 56, 56, 79, 62, 19, -204, -595],
[ 7, 12, 36, 47, 59, 68, 74, 56, 59, 100, 74, 34, -3, -353],
[ 23, 19, 51, 87, 86, 79, 91, 76, 96, 95, 52, 51, 74, -141],
[ 18, 51, 54, 97, 93, 94, 98, 83, 119, 71, 36, 69, 50, -16],
[ -10, 5, 53, 92, 69, 87, 103, 114, 118, 77, 51, 68, 30, 0],
[ -24, 11, 74, 80, 49, 68, 106, 129, 107, 63, 57, 70, 39, -1],
[ -45, 43, 83, 69, 43, 64, 98, 108, 90, 35, 27, 55, 31, -13],
[ -9, 32, 83, 78, 66, 106, 89, 85, 58, 43, 31, 39, 28, 7],
[ 45, 35, 76, 45, 51, 84, 55, 66, 49, 41, 39, 28, 13, -7],
[ 85, 67, 61, 45, 69, 53, 23, 32, 31, -12, -34, -182, -376, -425],
[ 136, 93, 71, 54, 30, 39, 17, -21, -29, -43, -101, -514, -792, -816]
], dtype=int16),
'px_spacing': array([[0.78125]])}
解决方案
推荐阅读
- json - 如何从 json 中提取这些值
- html - 我已尽我所能,但我的网站无法在 iPhone 上运行或响应
- sql - 通过 ODBC 而不是 ADODB 访问 Excel VBA 数据库
- java - 使用 codelabs 构建我的第一个应用程序遇到了一些问题
- view - 如何在swiftui中访问其他视图结构中的视图结构
- python - 分析数据时如何避免重复表达?
- android - 将自定义类保存到 Firestore 时如何正确排除 Document Id 属性?
- python - 如何在 python dash 应用程序中绘制来自传感器的串行信号?
- python - 使用带有关键字列表的 Scrapy
- reactjs - 如何将键分配给地图功能Javascript