Ask Your Question
0

Converting GpuMat 32FC1 > 8UC1

asked 2016-07-01 01:39:07 -0600

Ammy gravatar image

updated 2016-07-01 04:09:03 -0600

pklab gravatar image

Hello together,

i've got a problem with converting an GpuMat (the same problem i have, when i'm doing it with a cv::Mat). I want to delete some areas from a picture (gm_temp) where the threshold of the depth, stored in another matrix (gm_depth), is under a certain value (sts.d_treshold). Because cuda::treshold converts the gm_mask to 32FC1, i need to convert it back to 8UC1 to be able to multiply it with gm_temp. But after the conversion gm_mask only have zeros in its matrix, although it had the proper values before.

          cv::cuda::GpuMat gm_mask (gm_temp.rows, gm_temp.cols, CV_8UC1, 0.0);
          gm_depth.upload(m_depth);
          cuda::resize(gm_depth, gm_depth, Size(gm_temp.cols, gm_temp.rows));
          cuda::threshold(gm_depth, gm_mask, sts.d_treshold, 1.0, THRESH_BINARY); // Until here it's working fine.
          gm_mask.convertTo(gm_mask, CV_8UC1, 255.0/1.0, 0);
          cuda::multiply(gm_temp, gm_mask, gm_temp);
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2016-07-01 04:08:27 -0600

pklab gravatar image

convertTo should works fine. Your issue might comes from values. In your case the proper values before should be greater than 0.5/255

From the convertTo doc

dstMat(x,y) = saturate_cast<dstType>(alpha*srcMat(x,y) + beta)

you should know that

saturate_cast<char>(0.4999) = 0
saturate_cast<char>(0.5)    = 1
saturate_cast<char>(0.7)    = 1
...
saturate_cast<char>(260.x)  = 255

in general, when dstType is fixed type if srcValues < 0.5/alpha than dstValue = 0

Is this your case ?

edit flag offensive delete link more

Comments

as it is defined in

cuda::threshold(gm_depth, gm_mask, sts.d_treshold, 1.0, THRESH_BINARY)

i have got only "1" and "0" entrys in gm_mask, so that shouldn't be the problem i think.

Ammy gravatar imageAmmy ( 2016-07-01 05:22:34 -0600 )edit
1

you are right, if your gm_mask contains only "0" or "1" , after convertTo you should have only "0" or "255". I don't have cuda for a test but with common Mat all is working fine here. Please inspect values of gm_mask before convertTo and let us know.

pklab gravatar imagepklab ( 2016-07-01 06:06:01 -0600 )edit

I've now solved the problem. One has to use an additional matrix to copy the converted matrix into. so it works as follows:

cv::cuda::GpuMat gm_temp;
gm_mask.convertTo(gm_temp, CV_8UC1, 255.0/1.0, 0);
gm_mask = gm_temp;
Ammy gravatar imageAmmy ( 2016-07-01 08:55:20 -0600 )edit

mmm It seems that GpuMat::convertTo won't convert itself on flight. This should be investigated.

BTW You said the same problem i have, when i'm doing it with a cv::Mat... is this really true ? In my test the case with Mat img; img.convertTo(img,... works fine

In case Mat::convertTo works and GpuMat::convertTo doesn't You could open an issue here https://github.com/Itseez/opencv/issues.

pklab gravatar imagepklab ( 2016-07-01 10:32:42 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-07-01 01:39:07 -0600

Seen: 4,103 times

Last updated: Jul 01 '16