Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

for anyone doing simmilar, to display normalised first image from 16bit 256x256 raw binary with 256 bytes header, there is also endian conversion which may not be correct.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <fstream>          

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
        //file read
        ifstream fs;
        fs.open(argv[1], ios::in | ios::binary);
        unsigned short *tmp = new unsigned short [256*256];
        fs.seekg(256);
        fs.read((char*)tmp, 256*256*2 );
        fs.close();

    // read into Mat
    Mat imAcq=Mat(256,256,CV_16UC1,tmp);

    //endian conversion
    Mat mat1;
    mat1 = imAcq/256;
    imAcq = ( imAcq - ( 256 * mat1 ) ) * 256 + mat1;

    normalize(imAcq, imAcq, 0, 65536, NORM_MINMAX, -1, Mat() );

    //convert to 8bit for imshow
    Mat img8bit;
    imAcq.convertTo(img8bit,CV_8UC1,1);
    namedWindow( "8bit normalised", WINDOW_AUTOSIZE );
    imshow( "8bit normalised", img8bit );                   // Show image

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

for anyone doing simmilar, to display normalised first image from 16bit 256x256 raw binary with 256 bytes header, there is also endian conversion which may not be correct.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <fstream>          

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
        //file read
        ifstream fs;
        fs.open(argv[1], ios::in | ios::binary);
        unsigned short *tmp = new unsigned short [256*256];
        fs.seekg(256);
        fs.read((char*)tmp, 256*256*2 );
        fs.close();

    // read into Mat
    Mat imAcq=Mat(256,256,CV_16UC1,tmp);

    //endian conversion
    Mat mat1;
    mat1 = imAcq/256;
    imAcq = ( imAcq - ( 256 * mat1 ) ) * 256 + mat1;

    normalize(imAcq, imAcq, 0, 65536, NORM_MINMAX, -1, Mat() );

    //convert to 8bit for imshow
    Mat img8bit;
    imAcq.convertTo(img8bit,CV_8UC1,1);
imAcq.convertTo(img8bit,CV_8UC1,1/256.0);
    namedWindow( "8bit normalised", WINDOW_AUTOSIZE );
    imshow( "8bit normalised", img8bit );                   // Show image

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

for anyone doing simmilar, to display normalised first image from 16bit 256x256 raw binary with 256 bytes header, there is also endian conversion which may not be correct. (many thanks to L.Berger)

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <fstream>          

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
        //file read
        ifstream fs;
        fs.open(argv[1], ios::in | ios::binary);
        unsigned short *tmp = new unsigned short [256*256];
        fs.seekg(256);
        fs.read((char*)tmp, 256*256*2 );
        fs.close();

    // read into Mat
    Mat imAcq=Mat(256,256,CV_16UC1,tmp);

    //endian conversion
    unsigned char*c = (unsigned char*)tmp;
    for (int i = 0; i < 65536*2; i += 2)
    swap(c[i], c[i + 1]);
    Mat mat1;
    mat1 = imAcq/256;
    imAcq = ( imAcq - ( Mat(256, 256, CV_16UC1,tmp);// , tmp, 256 * mat1 ) ) 256 * 256 + mat1;
sizeof(unsigned short));

    normalize(imAcq, imAcq, 0, 65536, NORM_MINMAX, -1, Mat() );

    //convert to 8bit for imshow
    Mat img8bit;
    imAcq.convertTo(img8bit,CV_8UC1,1/256.0);
    namedWindow( "8bit normalised", WINDOW_AUTOSIZE );
    imshow( "8bit normalised", img8bit );                   // Show image

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

for anyone doing simmilar, to display normalised first image from 16bit 256x256 raw binary with 256 bytes header, there is also endian conversion (many thanks to L.Berger)

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <fstream>          

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
        //file read
        ifstream fs;
        fs.open(argv[1], ios::in | ios::binary);
        unsigned short *tmp = new unsigned short [256*256];
        fs.seekg(256);
        fs.read((char*)tmp, 256*256*2 );
        fs.close();

    // read into Mat
    Mat imAcq=Mat(256,256,CV_16UC1,tmp);

    //endian conversion
    unsigned char*c = (unsigned char*)tmp;
    for (int i = 0; i < 65536*2; i += 2)
    swap(c[i], c[i + 1]);
    Mat imAcq = Mat(256, 256, CV_16UC1,tmp);// , tmp, 256 * 256 * sizeof(unsigned short));

    normalize(imAcq, imAcq, 0, 65536, NORM_MINMAX, -1, Mat() );

    //convert to 8bit for imshow
    Mat img8bit;
    imAcq.convertTo(img8bit,CV_8UC1,1/256.0);
    namedWindow( "8bit normalised", WINDOW_AUTOSIZE );
    imshow( "8bit normalised", img8bit );                   // Show image

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

for anyone doing simmilar, to display normalised first image from 16bit 256x256 raw binary file with 256 bytes header, there is also endian conversion (many thanks to L.Berger)

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <fstream>          

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
        //file read
        ifstream fs;
        fs.open(argv[1], ios::in | ios::binary);
        unsigned short *tmp = new unsigned short [256*256];
        fs.seekg(256);
        fs.read((char*)tmp, 256*256*2 );
        fs.close();

    //endian conversion
    unsigned char*c = (unsigned char*)tmp;
    for (int i = 0; i < 65536*2; i += 2)
    swap(c[i], c[i + 1]);
    Mat imAcq = Mat(256, 256, CV_16UC1,tmp);// , tmp, 256 * 256 * sizeof(unsigned short));

    normalize(imAcq, imAcq, 0, 65536, NORM_MINMAX, -1, Mat() );

    //convert to 8bit for imshow
    Mat img8bit;
    imAcq.convertTo(img8bit,CV_8UC1,1/256.0);
    namedWindow( "8bit normalised", WINDOW_AUTOSIZE );
    imshow( "8bit normalised", img8bit );                   // Show image

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}