convert IPLImage to unsigned short [closed]

asked 2020-06-26 01:58:32 -0500

snehal gravatar image

updated 2020-06-26 03:22:47 -0500

supra56 gravatar image


I have converted an 8bit image from type IplImage to unsigned char as below :

unsigned char* pImageBuffer = (unsigned char*)malloc(sht*swd);

    for (int j = 0; j < height; j++)
        for (int i = 0; i < width; i++)
            pImageBuffer[j*swd + i] = (unsigned char)(pGrayImage-mageData[j*pGrayImage->widthStep + i]);

Mat inputImg = Mat(height, width, CV_8UC1, pImageBuffer);

imwrite("E:\\Mat_inputImg_8bit.bmp", inputImg);

I got the 'inputImg' image same as the original one.

But when I tried same for 16bit image I am not getting 'inputImg' image the same as the original one.

unsigned short* pImageBuffer = (unsigned short*)malloc(sht*swd*sizeof(unsigned short));

    for (int j = 0; j < sht; j++)
        for (int i = 0; i < swd; i++)
            pImageBuffer[j*swd + i] = (unsigned short)(pGrayImage->imageData[j*pGrayImage->widthStep + i]);

    Mat inputImg = Mat(sht, swd, CV_16UC1, pImageBuffer);
    imwrite("E:\\Mat_inputImg_16bit.png", inputImg);

please give me any suggestions.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by berak
close date 2020-06-26 02:14:41.432133


opencv's c-api is deprecated, and will get removed entirely in the next major release, so please


you have to use cv::Mat and the c++ api, like:

Mat img = imread("some.png", IMREAD_GRAYSCALE);
Mat im16; img.convertTo(im16, CV_16U);
berak gravatar imageberak ( 2020-06-26 02:20:24 -0500 )edit

Thank you.

snehal gravatar imagesnehal ( 2020-06-26 02:32:38 -0500 )edit