python灰度图像为什么显示成彩色的_python opencv image 怎么变成伪彩色
匿名用戶
1級
2017-05-16 回答
OpenCV 生成 偽彩色圖像
opencv中沒有易用的偽彩色圖像生成函數,這里提供一個改造過的函數,利用自定義colorbar 將灰度圖像轉換成為偽彩色圖像,優點在于提供了對于顏色的直觀可操控性,轉換方便。
函數代碼如下:
[cpp] view plain copy 在CODE上查看代碼片派生到我的代碼片
//function : Pseudo color - enhanced
//author : Xin Yang, Shenzhen Univ., School of medicine
//email : xinyang@szu.edu.cn
//date : 2015.01.23
void C_Assistant::Gray2PseudoColor(IplImage* src ,IplImage *&dst)
{
if(dst != NULL)
{
cvReleaseImage(&dst);
dst = NULL;
}
dst = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 3);
IplImage *R, *G,*B;
//Load referred color bar
std::string HeatMapPath = "..\\ColorBar.png";
IplImage* heatmap = cvLoadImage(HeatMapPath.c_str());
//we split the heatmap along the 3 channels
R = cvCreateImage(cvGetSize(heatmap), heatmap->depth,1);
G = cvCloneImage(R);
B = cvCloneImage(R);
cvSplit(heatmap,B,G,R,NULL);
for(int x=0; xwidth; x++)
{
for(int y=0;yheight; y++)
{
//memory access to the destination color image (faster than splitting the 3 channels...)
unsigned char *data = &((unsigned char*)(dst->imageData + dst->widthStep*y ))[x*3];
//read the intensity value in the grayscale image
unsigned char gray = src->imageData[src->widthStep*y + x*src->nChannels];
//remember, OpenCV store images as BGR internally !
//So access [2] for Red, [1] for Green et [3] for Blue
float ColorIndex = gray/255.0*heatmap->height;
if(ColorIndex >= heatmap->height) ColorIndex = heatmap->height - 1;
data[2] = cvGet2D(R, ColorIndex, 1).val[0]; //Red channel
data[1] = cvGet2D(G, ColorIndex, 1).val[0]; //Green channel
data[0] = cvGet2D(B, ColorIndex, 1).val[0]; //Blue channel
}
}
//Clear
if(heatmap != NULL)
{
cvReleaseImage(&heatmap);
heatmap = NULL;
}
}
參考可用的colorbar如下,也可以自己生成來替換。
總結
以上是生活随笔為你收集整理的python灰度图像为什么显示成彩色的_python opencv image 怎么变成伪彩色的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于hadoop的商品推荐系统_【论文笔
- 下一篇: 空间金字塔池化_回顾语义分割—Dense