First time here? Check out the FAQ!

Ask Your Question
0

Bitmap to hex array conversion in c++ using opencv

asked Feb 28 '13

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");
if(image.empty())
return 0;
cv::imshow("Image", image);
cv::waitKey();
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

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Feb 28 '13

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 ;)
     return;

// 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<image.total(); i++ )
{
    cout << image.at<uchar>(i) << " "; // this prints characters ( a for 64, etc)
    // cout << int(image.at<uchar>(i)) << " "; // this prints numbers ( 64 for 64, etc)
    // cout << cv::format("0x%x ", image.at<uchar>(i)); // this prints hex numbers ( 0x40 for 64, etc)
}

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

Preview: (hide)

Comments

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 (Mar 5 '13)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 (Mar 5 '13)edit

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

berak gravatar imageberak (Mar 5 '13)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 (Mar 5 '13)edit

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

KOPII gravatar imageKOPII (Mar 5 '13)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 (Mar 5 '13)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 (Mar 6 '13)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 (Mar 6 '13)edit

Unfortunately I need to do this... :(

KOPII gravatar imageKOPII (Mar 8 '13)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 (Mar 11 '13)edit

Question Tools

Stats

Asked: Feb 28 '13

Seen: 3,565 times

Last updated: Aug 20 '16