2012-11-18 69 views
4

我正在分割algorithme用于医学图像上的色彩和轮廓的过程中,必须显示在原始图像上不断变化的轮廓。
我与灰度JPEG工作。为了显示轮廓,我使用drawContours函数,但是我无法设置绘制颜色轮廓。我想在灰度图像上绘制红色轮廓,但只显示黑色或白色。
下面是代码的部分:的OpenCV - 绘制灰度图像

Mat_<uchar> matrix = imread(path,0); 
int row = matrix.rows; 
int col = matrix.cols; 
Mat_<uchar> maskInter(row,col); 
for (int i=0; i<row; i++) 
    for (int j=0; j<col; j++) 
    { 
     if ((double)matrix(i,j) <0) 
     {maskInter(i,j)=255;} 
     else 
     {maskInter(i,j)=0;} 
    }; 

vector<vector<Point> > contours; 
vector<Vec4i> hierarchy; 
Mat mat_out = maskInter.clone(); 
findContours(mat_out, contours, hierarchy, CV_RETR_TREE , CHAIN_APPROX_SIMPLE); 

drawContours(img, contours, -1, RGB(255,0,0),1.5,8,hierarchy,2,Point()); 
namedWindow(title); 
moveWindow(title, 100, 100); 
imshow(title, img); 
waitKey(0); 

是否有可能显示灰度图像上的颜色轮廓?
感谢

回答

9

你以绘制和显示颜色需要一个3通道(RGB)图像。您的Mat_<uchar> matrix只是一个频道。您可以将灰度图像转换成彩色图像,如下所示:

// read image 
    cv::Mat img_gray = imread(path,0); 

    // create 8bit color image. IMPORTANT: initialize image otherwise it will result in 32F 
    cv::Mat img_rgb(img_gray.size(), CV_8UC3); 

    // convert grayscale to color image 
    cv::cvtColor(img_gray, img_rgb, CV_GRAY2RGB); 
+0

谢谢,这工作得很好。 – alvinleetya

+0

欢迎! 最低选民可以给我的答案一个理由或改善? – cfo

+0

你的最终陈述是错误的。你的答案与我的答案没有显着差异。 – ArtemStorozhuk

0

是否有可能显示灰度图像上的颜色轮廓?

如果它真的是灰度(每个像素1个字节)比没有,你不能。要绘制彩色轮廓,你应该转换为使用cvtColor图像RGB(每像素3个字节),这场平局色彩轮廓之后。

+0

+1是的,确实如此。 – karlphillip