具有直接像素访问的Opencv颜色映射

dav*_*vid 7 c++ opencv

我有一个灰度图像,我希望通过使用调色板(如Matlab中的colormap)映射灰度值来显示颜色.

我设法通过使用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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于不同的像素访问,将if(true)更改为if(false).

正确的结果是使用cvSet2D:

在此处输入代码

直接内存访问的错误结果:

在此输入图像描述

谢谢您的帮助

mas*_*sad 5

我已经为Microsoft Kinect Sensor着色深度贴图做了类似的工作.我用于将灰度深度图转换为彩色图像的代码将适用于您要执行的操作.您可能需要稍作修改,因为我的情况下深度值在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);     

        }
        }
}
Run Code Online (Sandbox Code Playgroud)

对于如下所示的输入图像:

在此输入图像描述

此代码的输出是:

在此输入图像描述


dav*_*vid 0

我发布此内容是为了结束问题,就像我的问题评论中所提出的那样。

答案是:

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

谢谢