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

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

本文介绍了直接像素访问的Opencv颜色映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个灰度图像,我想通过用颜色调色板(如Matlab中的色彩映射)映射灰度值来显示颜色。



通过使用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);
}
}
}

更改if



正确的结果是使用cvSet2D:

=http://i.stack.imgur.com/RxZCe.jpgalt =在此处输入代码>



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



>



感谢您的帮助

解决方案

我发布这封信



答案是:



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



谢谢


I have a gray scale image that I want to display in color by mapping the gray scale values with a color palette (like colormap in Matlab).

I managed to do it by using OpenCV cvSet2D function, but I would like to access to the pixels directly for performance reasons.

But when I do that the image has strange colors. I tried to set the colors in different orders (RGB, BGR,…) but can’t seem to get around it.

There is my code:

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

Change the if (true) to if (false) for different pixel access.

The correct result is with cvSet2D:

The wrong result with the direct memory access:

Thank you for your help

解决方案

I'm posting this to close the question like asked in the comments of my question.

The answer was:

I found out my error... In fact it's RGB but that wasn't the problem, I had color values at 256 instead of 255... Really sorry... I guess asking the question helped me found the answer

Thank you

这篇关于直接像素访问的Opencv颜色映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 19:19