Abhishek Thakur
Abhishek Thakur

Reputation: 17005

converting float to unsigned char in OpenCV

I have designed a filter in the form of a horizontal 1D vector using OpenCV and C++. The vector consists of float data. The original uchar data of the grayscale image is multiplied with this float vector as a 1 dimensional window to obtain the result. However, I am not getting proper results.

When the vector elements are multiplied with the image pixel values, the exceed the range 0-255 and I think this is causing problems.

Is there any way to typecast this float data into uchar to get proper results?

I'm using Img.at<uchar> = (uchar)(floatVector) right now.

Thanks

Upvotes: 1

Views: 5022

Answers (2)

Boyko Perfanov
Boyko Perfanov

Reputation: 3047

You want to carry out the multiplication in the float type, and only at the end, convert back to unsigned char. Don't forget to also have your float vector normalized (all values add up to 1)

So basically you want

Data.at(coordinates) = (unsigned char) (floatVector(0)*(Data.at(coord0) + ... + FloatVector(last)*Data.at(coordLast))

Upvotes: 0

rotating_image
rotating_image

Reputation: 3076

I will suggest you to type cast after you have multiplied...so convert your uchar image matrix to CV_32FC1 (since you say its grayscale image so channel = 1)....do the convolution of the image with your filter then type cast the values to ucharfor displaying may be..

Upvotes: 1

Related Questions