2011-07-13 53 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