OpenCV - error when splitting channels

时间:2016-07-11 21:52:35

标签: c++ opencv

I'm trying to use the remap function in OpenCV. I have an STMap, so the x values are in the Red channel and y values are in the Green channel.

Here i'm loading my source image which i want to map, and the STMap:

Mat image3= imread("stmap.tif", CV_32FC1);
Mat image4= imread("source.tif", CV_LOAD_IMAGE_UNCHANGED);

Then i split the channels to get the red and green ones from my STMap:

Mat bgr[3];   //destination array
split(image3,bgr);//split source

bgr[0].convertTo(bgr[0],CV_32FC1);
bgr[1].convertTo(bgr[1],CV_32FC1);
bgr[2].convertTo(bgr[2],CV_32FC1);
Mat dst;

And finally, the remapping part:

remap(image4, dst,bgr[2],bgr[1],INTER_LINEAR,BORDER_CONSTANT,Scalar(0,0,0));

However, when i display the dst Mat, it's all black. I tried printing out some values to the console like:

cout << bgr[2].at<float>(1000,300);

but they're all 0's. I displayed bgr[1] and bgr[2] and they're definitely not all black - they are greyscale gradients, like they should be.

So what could be the problem?

Thanks!

EDIT:

I noticed that whenever i would use a .jpeg file and the command:

cout << bgr2[2].at<float>(1000,300);

It would give me something like 0.1828510

But when i run the same command with a 32bit .tif, it gives me:

1.04984e-320

EDIT 2:

How should i use the convertMaps() function?

This is what i tried and didn't work:

Mat image3= imread("STMap.jpg");
Mat image4= imread("image.jpg");

    Mat bgr[3];  
    split(image3,bgr);
    Mat map_x, map_y;
    map_x.create( image.size(), CV_32FC1 );
    map_y.create( image.size(), CV_32FC1 );    
    convertMaps(bgr[2], bgr[1], map_x, map_y, CV_32FC1);

Also, when i used convertTo():

bgr[2].convertTo(red_channel, CV_32FC1, 1/255.0)

bgr[2] displayed correctly (the grayscale image of mapping intensities) and it the red_channel had all of its values between 0.0 and 1.0

1 个答案:

答案 0 :(得分:0)

Your calls to convertTo() make no sense at all. Also have a look at the documentation of remap() and you will find reference to convertMap(), which is the method of choice for changing the matrix type (if you really have to!).

remap() expects the input to be in a certain range, probably also depending on the type of the input matrix. If it works with JPEGs but not with 32 bit TIFF I expect you have a data range issue.