Ask Your Question
1

how to create a heat map using opencv instead of one color degrees

asked 2015-02-01 01:20:05 -0600

RSA gravatar image

I have a function that generates a data from 0 to 1 .. I am drawing the data but it gives me the degrees of white or black but I want to have a the heat map scale. The output is shown in the image image description

I want to have a dark red in the middle and blue in the outside boundries as I said the heat map colors..

The code I used is the following:

 cv::Mat img(numberOfPixels,numberOfPixels, CV_8UC3, cv::Scalar(0,0,0));
 cv::Mat image = img;
 for(int x=0;x<img.cols;x++)
{
    for(int y=0;y<img.rows;y++)
    {
        obstX = (x - img.cols/2.0)*8;
        obstY = (img.rows/2.0 - y)*8;

        f = this->getF(param1,param2 );
        double F = sqrt(f(0)*f(0) + f(1)*f(1) + f(2)*f(2));
        Vec3b color = image.at<cv::Vec3b>(cv::Point(x,y));
        color.val[0] = uchar(F * 255.0);
        color.val[1] = uchar(F * 255.0);
        color.val[2] =  uchar(F * 255.0);

        image.at<cv::Vec3b>(cv::Point(x,y)) = color;
    }
}

imwrite(img);
}
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
4

answered 2015-02-01 01:34:31 -0600

Simply use the matlab copied function called applyColorMap.

edit flag offensive delete link more
2

answered 2015-02-01 02:41:43 -0600

Haris gravatar image

updated 2015-02-01 02:42:20 -0600

As StevenPuttemans suggest you can use applyColorMap,

Or in another way use HSV as color space , and use hue value for color range,

See an example,

 cv::Mat hsv(100,180, CV_8UC3, cv::Scalar(0,0,0));
 for(int x=0;x<hsv.cols;x++)
 {
     int h=x;
     int s=255;
     int v=255;

     Scalar color;
     color.val[0] = uchar(h);
     color.val[1] = uchar(s);
     color.val[2] = uchar(v);
     hsv.col(x) = color;

}

 Mat bgr;
 cvtColor(hsv,bgr,CV_HSV2BGR);
 imwrite("hsv.png",bgr);

image description

edit flag offensive delete link more

Comments

This approach has the disadvantage that red appears twice so that your mapping is not invertible and a user can't extract the value of a red pixel.

FooBar gravatar imageFooBar ( 2015-02-01 03:43:08 -0600 )edit

You are right, but by adding an offset to hue(here 30-40 ) the entire red range can be used, also the user can select any hue range(between 0-180) then do mapping.

Haris gravatar imageHaris ( 2015-02-01 09:25:04 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-02-01 01:20:05 -0600

Seen: 9,216 times

Last updated: Feb 01 '15