【问题标题】:openCV - draw color contours on greyscale imageopenCV - 在灰度图像上绘制颜色轮廓
【发布时间】:2012-11-06 16:06:00
【问题描述】:

我正在研究医学图像的分割算法,在此过程中,它必须在原始图像上显示不断变化的轮廓。
我正在使用灰度 JPEG。为了显示轮廓,我使用了 drawContours 函数,但我无法绘制颜色轮廓。我想在灰度图像上绘制一个红色轮廓,但它只显示为黑色或白色。
以下是部分代码:

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);

是否可以在灰度图像上显示颜色轮廓?
谢谢

【问题讨论】:

    标签: image opencv colors contour grayscale


    【解决方案1】:

    是否可以在灰度图像上显示颜色轮廓?

    如果它真的是灰度(每像素 1 个字节),那么你不能。要绘制颜色轮廓,您应该使用cvtColor 并在此绘制颜色轮廓之后将图像转换为 RGB(每像素 3 个字节)。

    【讨论】:

      【解决方案2】:

      您需要一个 3 通道 (RGB) 图像才能绘制和显示颜色。您的Mat_&lt;uchar&gt; 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);
      

      【讨论】:

      • 欢迎您!投反对票的人可以给出理由或改进我的答案吗?
      • 你最后的陈述是错误的。而且你的回答和我的回答没有太大区别。
      • @Astor 我发现首席财务官的回答更有帮助。只有一件事我不明白:当我尝试在图像 img_rgb 上使用 cvDrawContour 或 cv::Line 绘制线条或轮廓时,我只能使用红色、绿色或蓝色。如果我使用 CV_RGB(255,0,0) 它将是红色的,CV_RGB(0,255,0) 它将是绿色的......但是如果我想使用橙色(例如 CV_RGB(253,152,0))它会画成白色(或黑色,取决于 R、G、B 的值)。
      猜你喜欢
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 2013-04-23
      • 2021-02-01
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多