简体   繁体   中英

How to read JPG images as CV_16UC1 and print on screen?

I want to read a JPG images as a CV_16UC1 on OpenCV (in C++ ).

I can also convert from CV_8UC3 to CV_16UC1 .

This code below works, but here it's using only CV_8UC3 format, which is not what I need:

using namespace cv;
int main()
{
    Mat im1 = imread("MyPic.JPG");
    Mat im2 = imread("MyPic.JPG");
    Size sz1 = im1.size();
    Size sz2 = im2.size();

    Mat final(sz1.height, sz1.width + sz2.width, CV_8UC3);
    Mat left(final, Rect(0, 0, sz1.width, sz1.height));
    im1.copyTo(left);

    Mat right(final, Rect(sz1.width, 0, sz2.width, sz2.height));
    im2.copyTo(right);

    imshow("final", final);
    waitKey(0);
    return 0;
 }

But I need to use CV_16UC1 (This is really necessary !). Therefore, I've converted like this:

    cv::Mat im2 = cv::imread("MyPic.jpg");
    cv::Size sz2 = im2.size();

    cv::Mat im3; // I want im3 to be the CV_16UC1 of im2
    im2.convertTo(im3, CV_16UC1);
    cv::Size sz3 = im3.size();

    cv::Mat im1 = cv::imread("MyPic.jpg");
    cv::Size sz1 = im1.size();

    cv::Mat im4;
    im4.convertTo(im1, CV_16UC1);
    cv::Size sz4 = im4.size();

    // Two images by each other side - this must be CV_16UC1
    cv::Mat final(sz1.height, sz1.width + sz2.width, CV_16UC1);

    // The left image
    cv::Mat left(final, cv::Rect(0, 0, sz1.width, sz1.height));
    im1.copyTo(left);

    // The right image
    cv::Mat right(final, cv::Rect(sz1.width, 0, sz2.width, sz2.height));
    im4.copyTo(right);

    cv::imshow("final", final);

    cv::waitKey(0);

It seems that I'm doing something wrong on the convertions or on the way I'm reading, because no image is appearing on the screen.

How can I convert correctly from CV_8UC3 to CV_16UC1 ?

A 8-Bit-Unsigned Mat's pixel values range between 0 and 255, so, when you load the images from file and display them, each pixel will be brighter the closer it is to 255, the maximum value.

When you convert those images to 16Bit-Unsigned, the pixels will keep the same absolute value (something between 0 and 255), but now the maximum value of your data structure is 65535. So, it is no surprise at all that the images are dark. Even the higher value (255) will still be so low in the new resolution that it will look like it is not there.

"How to read JPG images as CV_16UC1 and print on screen?"

You can't. The .jpg images just aren't CV_16UC1. If you generated those images and thought they would hold 16Bit data, they don't. Probably you didn't save it correctly. Once they are .jpg, they lose most the information they had and there is no magic way to recover it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM