garak
garak

Reputation: 4813

Convert type of Matrix in OpenCV

I have the following code to convert the "type" of the matrix using cv::Mat.convertTo() function in OpenCV API.

But when I show it using an imshow(), the images are completely different.

The converted image is completely grey.

However, the values in those images(cv::Mat) are the same.

I think I'm missing something very trivial here.

Mat x = imread("/home/jason/Desktop/1.png",0);
Mat y = Mat(x.rows,x.cols,CV_16S,Scalar(0));

x.convertTo(y,CV_16SC1, 1,0);

imshow("x",x);
imshow("y",y);
waitKey(0);

cout<<x.type()<<endl;
cout<<y.type()<<endl;

//verifying the values in the image.

cout<<"rows 10"<<x.row(10)<<endl;
cout<<"rows 10"<<y.row(10)<<endl;

Upvotes: 0

Views: 4312

Answers (1)

Andrey Kamaev
Andrey Kamaev

Reputation: 30152

When you are passing CV_16S image to the imshow (or its C equivalent) the pixel values are implicitly recalculated before the visualization by the formula vdisplayed = 128 + vimage/256

So you probably need to multiply the image by some constant (256?) or convert it to CV_8U before the imshow call.

Upvotes: 1

Related Questions