Ask Your Question

Bitmap to hex array conversion in c++ using opencv

asked 2013-02-27 23:06:37 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

I am new to OpenCV. I have to convert a monochrome bitmap to a one dimensional array. What I have done so far is that I have read the image file and shown it in a window.

#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv )
cv::Mat image;
image = imread("imaje.bmp");
return 0;
cv::imshow("Image", image);
return 0; 

The image array is stored in variable named as "image" . I need to get that variable in some text format as I need to send these bytes to a printer. Please guide me how should I proceed ?? Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-02-28 04:28:55 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

first, a word of warning, those cv::Mats only contain the pixels, and stuff like rows & cols. if you want to actually print an IMAGE, you've got to send your original bmp or png file to the printer, not the cv::Mat. but if you just want to print the pixels, not too difficult!:

cv::Mat image;
image = imread("imaje.bmp", 0);  
// the 0 is for : read as grayscale ( if it's not, make it 1 channel gray)

if(image.empty())   // bonus points for checking the results! no, really ;)

// now the most easy thing would be this:
cout << image << endl;  // done, haha!

// if you need more control over formatting, try looping over the pixels:
for ( int i=0; i<; i++ )
    cout <<<uchar>(i) << " "; // this prints characters ( a for 64, etc)
    // cout << int(<uchar>(i)) << " "; // this prints numbers ( 64 for 64, etc)
    // cout << cv::format("0x%x ",<uchar>(i)); // this prints hex numbers ( 0x40 for 64, etc)

If you want to use some predefined style for printing for Mat, see here.

edit flag offensive delete link more


Thanks alot sir. It works fine. One thing I further need to know is that how may I get the rows and columns of that particular image??? I am doing it as :

int roww= image.rows;
int colm=image.cols;
cout&lt;&lt;"Rows ="&lt;&lt;roww&lt;&lt;endl;
cout&lt;&lt;"Columns ="&lt;&lt;colm&lt;&lt;endl;

But its not working ... :(

KOPII gravatar imageKOPII ( 2013-03-05 04:59:10 -0500 )edit

Actually its a Thermal Printer working on POS Commands. It gets the bitmap as array of bytes. Actually the data is sent byte by byte to the printer.

KOPII gravatar imageKOPII ( 2013-03-05 05:10:57 -0500 )edit

oh my, should have mentioned the POS thing earlier .. let's research a bit

berak gravatar imageberak ( 2013-03-05 05:19:34 -0500 )edit

found some spec here , but don't expect me to internalize that in 5 seconds. ;) table 1-23 looks interesting

cout&lt;&lt;"Rows ="&lt;&lt;roww&lt;&lt;endl; (in your post above)

that's supposed to be: <code>cout << "Rows =" << roww << endl; </code> ?
(rrrr, shitty markown syntax, i can't do it)

also, "not working" means what, exactly ?

berak gravatar imageberak ( 2013-03-05 05:26:14 -0500 )edit

Means there is no output at the console for the above code ...

KOPII gravatar imageKOPII ( 2013-03-05 05:52:10 -0500 )edit

I don't have any issue in printing by POS commands. I have successfully done it. I just need to convert my bitmap to array of bytes now and yes I must need the rows and columns because that are used as the parameters for the printer's bitmap printing command... :)

KOPII gravatar imageKOPII ( 2013-03-05 06:04:24 -0500 )edit

Yes I have to send these bytes to a POS printer. It was a mistake by me that I have written network instead of POS printer. And I am sorry for that. I just need to know that how I can get the rows and columns of a spacific image because I am able to print a bitmap whose rows, columns and pixel array is known to me. I have obtained the pixel array successfully as you guided me. Now I am just left with number of rows and columns to be known for a specific image. Thanks

KOPII gravatar imageKOPII ( 2013-03-06 00:09:27 -0500 )edit

I have successfully received number of rows and columns. But here comes a genuine issue i.e. in the pixel array returned , there is one byte for each pixel where as there should be one bit for a pixel which is the requirement of the printer... Please help me fix it.. Thanks

KOPII gravatar imageKOPII ( 2013-03-06 06:53:37 -0500 )edit

Unfortunately I need to do this... :(

KOPII gravatar imageKOPII ( 2013-03-07 23:51:24 -0500 )edit

Finally I used union approach and got the result successfully. Anyway I must say thanks to you and hope you will be guiding me in future... :)

KOPII gravatar imageKOPII ( 2013-03-11 01:18:39 -0500 )edit

Question Tools


Asked: 2013-02-27 23:06:37 -0500

Seen: 2,908 times

Last updated: Aug 20 '16