Gilad
Gilad

Reputation: 6575

imshow opencv doens't show image

this is my code, when I try to use imshow the image is black

    cv::Mat src(height, width, CV_16U);
    for (int i = 0; i < width* height; ++i)
    {
        src.at<unsigned short>(i) = input_image[i]/1023;
    }
    cv::Mat temp;
    cv::resize(src, temp, cv::Size(), 0.3, 0.3, cv::INTER_LINEAR);
    cv::imshow("image1", temp);
    cv::waitKey(0);

    cv::Mat imageData;
    cv::cvtColor(src, imageData, cv::COLOR_BayerGR2RGB);

    cv::resize(imageData, temp, cv::Size(), 0.3, 0.3, cv::INTER_LINEAR);
    cv::imshow("image1", temp);
    cv::waitKey(0);

if I change my code to

cv::Mat src(height, width, CV_32F);
for (int i = 0; i < width* height; ++i)
{
    src.at<float>(i) = input_image[i]/1023;
}

enter image description here

can you explain how to display the image?

Upvotes: 0

Views: 463

Answers (1)

beaker
beaker

Reputation: 16791

Since your input_image is in the range 0..1023, by dividing by 1023 you're getting a value between 0 and 1. When you cast that to an unsigned integer type, your image is all zeros. (It might round some of them to 1, but that's not really going to be visible anyway.)

To get the number to use the full range of unsigned short, you need to multiply by 65535:

src.at<unsigned short>(i) = input_image[i]/1024*65536;

or

src.at<unsigned short>(i) = input_image[i]*64;

Upvotes: 1

Related Questions