cvColorMap only supports source images of type CV_8UC1 or CV_8UC3
上伪彩色的时候报错cv::ColorMap only supports source images of type CV_8UC1 or CV_8UC3
上伪彩色
# opencv 对灰度图上伪彩色:
roiMask = cv.applyColorMap(roiMask, cv.COLORMAP_JET)
报错:
cv::ColorMap only supports source images of type CV_8UC1 or CV_8UC3
解决方案,
1、把矩阵的数据类型,修改成np.uint8
2、改成1个通道,或者3通道的RGB数据;
# 计算每个soma的changeIndex
# 计算方法参考paper: Dynamics of a disinhibitory prefrontal microcircuit in controlling social competition
import tifffile
import numpy as np
import os
import cv2 as cv
import json
if __name__ == '__main__':
dataRootPath = r'D:/Deo'
curImgRootPath = dataRootPath + '/Avg.tif'
trace = dataRootPath + '/trace.mat'
roisetJsonPath = dataRootPath + '/RoiSetJson.json'
if os.path.exists(roisetJsonPath):
with open(roisetJsonPath) as f:
rois = json.load(f)
curImgData = tifffile.imread(curImgRootPath)
imgsize = curImgData.shape # (595, 853)
# roiMask = np.ones((imgsize[1], imgsize[0], 4)) * (255, 255, 255, 0)
roiMask = np.zeros((imgsize[1], imgsize[0]))
roiMask = roiMask.astype(np.uint8)
roiMask01 = roiMask.copy()
for key, value in rois.items():
# 把胞体的text给打出来
pos = value['pos']
points = value['polygon']
line = np.array([points], dtype=np.int32)
color = int(np.random.random()*255) # 这个要修改成change Index
cv.fillConvexPoly(roiMask01, line, 1) # 胞体内是1
# 如果胞体可见的话,就去渲染roiMask
if value['visible']:
cv.fillConvexPoly(roiMask, line, color) # 填充颜色
# opencv 对灰度图上伪彩色:
roiMask = cv.applyColorMap(roiMask, cv.COLORMAP_JET)
channelA = (1 - roiMask01) * 0 + roiMask01 * 255 #前边的是背景,要透明的。后边是胞体,要显示change index
channelA = channelA[:,:,np.newaxis]
roiMask = np.concatenate((roiMask, channelA), axis=2)
file_ = dataRootPath + '/somaChangeIndex.png'
cv.imencode('.png', roiMask)[1].tofile(file_) # 解决了中文乱码问题;
效果如下(有了透明效果和伪彩色效果):