2011-07-13 56 views
6

我有一个灰度图像,我想通过使用调色板(如Matlab中的颜色映射)映射灰度值来显示颜色。使用直接像素访问的Opencv颜色映射

我设法通过使用OpenCV cvSet2D功能,但我想直接出于性能原因访问像素。

但是,当我这样做的图像有奇怪的颜色。我试图设置不同的顺序(RGB,BGR,...)的颜色,但似乎无法绕过它。

有我的代码:

IplImage* temp = cvCreateImage(cvSize(img->width/scale,img->height/scale), IPL_DEPTH_8U, 3); 
for (int y=0; y<temp->height; y++) 
{ 
    uchar* ptr1 = (uchar*) (temp->imageData + y * temp->widthStep); 
    uchar* ptr2 = (uchar*) (img->imageData + y * img->widthStep); 

    for (int x=0; x<temp->width; x++) 
    { 
     CvScalar v1; 

     int intensity = (int)ptr2[x]; 
     int b=0, g=0, r=0; 
     r = colormap[intensity][0]; 
     g = colormap[intensity][1]; 
     b = colormap[intensity][2]; 

     if (true) 
     { 
      ptr1[3*x] = b; 
      ptr1[3*x+1] = g; 
      ptr1[3*x+2] = r; 
     } 
     else 
     { 
      v1.val[0] = r; 
      v1.val[1] = g; 
      v1.val[2] = b; 
      cvSet2D(temp, y, x, v1); 
     } 
    } 
} 

更改,如果(真),以如果(假)针对不同像素的访问。

正确的结果是cvSet2D:

enter code here

错误的结果与直接存储器存取:

enter image description here

谢谢您的帮助

+0

现在看不到任何错误。 'imageData'中的正确字节顺序应该是BGR。如果您更改字节顺序,图像如何改变?它应该改变,否则你做错了什么。尝试仅渲染一种颜色(将其他通道保留为0 /黑色)。看看它们是否合适。对齐似乎是正确的,但我对围绕角落的强烈对比感到有点困惑。 – Mario

+2

我发现了我的错误...其实它是RGB但这不是问题,我的颜色值为256而不是255 ...真的很抱歉...我想问这个问题帮助我找到了答案。 – david

+0

啊......好吧。这解释了强烈的对比(由于溢出等原因)。:) – Mario

回答

0

我正在发布这个关闭在我的问题的评论中问的问题。

答案是:

我发现我的错误。其实这是RGB但这不是问题,我不得不颜色值256,而不是255 ......真的对不起......我猜问这个问题帮我找到了答案

谢谢

4

我已经做了从Microsoft Kinect传感器着色深度图的东西类似。我用于将灰度深度图转换为彩色图像的代码适用于您正在尝试执行的操作。您可能需要稍微修改,因为在我的情况下,深度值在500到2000之间,我不得不重新调整它们。

用于着色的灰度图像转换成彩色图像的功能是:

void colorizeDepth(const Mat& gray, Mat& rgb) 
{ 
     double maxDisp= 255; 
     float S=1.f; 
     float V=1.f ; 

     rgb.create(gray.size(), CV_8UC3); 
     rgb = Scalar::all(0); 

    if(maxDisp < 1) 
      return; 

    for(int y = 0; y < gray.rows; y++) 
     { 
      for(int x = 0; x < gray.cols; x++) 
      { 
       uchar d = gray.at<uchar>(y,x); 
       unsigned int H = 255 - ((uchar)maxDisp - d) * 280/ (uchar)maxDisp;  
      unsigned int hi = (H/60) % 6; 

      float f = H/60.f - H/60; 
       float p = V * (1 - S); 
       float q = V * (1 - f * S); 
       float t = V * (1 - (1 - f) * S); 

      Point3f res; 

       if(hi == 0) //R = V, G = t, B = p 
        res = Point3f(p, t, V); 
       if(hi == 1) // R = q, G = V, B = p 
        res = Point3f(p, V, q); 
       if(hi == 2) // R = p, G = V, B = t 
        res = Point3f(t, V, p); 
       if(hi == 3) // R = p, G = q, B = V 
        res = Point3f(V, q, p); 
       if(hi == 4) // R = t, G = p, B = V 
        res = Point3f(V, p, t); 
       if(hi == 5) // R = V, G = p, B = q 
        res = Point3f(q, p, V); 

       uchar b = (uchar)(std::max(0.f, std::min (res.x, 1.f)) * 255.f); 
       uchar g = (uchar)(std::max(0.f, std::min (res.y, 1.f)) * 255.f); 
       uchar r = (uchar)(std::max(0.f, std::min (res.z, 1.f)) * 255.f); 

       rgb.at<Point3_<uchar> >(y,x) = Point3_<uchar>(b, g, r);  

     } 
     } 
} 

对于它看起来像这样的输入图像:该代码的

enter image description here

输出是:

enter image description here