您的位置:首页 > 其它

将图像转换为1位位图 (1像素对应1bit)

2016-11-29 09:00 183 查看
文章参考网址:http://www.voidcn.com/blog/suifeng50/article/p-4907477.html

#include "opencv2\highgui\highgui.hpp"

#include "opencv2\imgproc\imgproc.hpp"

#include "opencv2\core\core.hpp"

#include <Windows.h>

 #include<fstream>

#include<iostream>

#include <ctime>

using namespace cv;

using namespace std;

int  mat_to_binary( const  cv::Mat img,  int  line_byte,  char * data)

{
int  width = img.cols;
int  height = img.rows;
size_t  line_size = line_byte * 8;
size_t  bit_size = line_size * height;

char  *p = data;  int  offset, v; unsigned  char  temp;
for ( int  row=height-1; row>=0; row-- ) {       
for  ( int  col=0; col<width; col++ ) {  
offset = col % 8;
v = img.at<uchar>(row, col);
temp = 1;
temp = temp << (8 - offset -1);
if (v == 255 ) {                
*(p + col/8) |= temp;
}  else  {
temp = ~temp;
*(p + col/8) &= temp;
}
}
for ( int  j = width/8 ; j < line_byte; j++)
p[j] = 0;
p = p + line_byte;
}
return  0;

}

int  save_bmp_image( const  cv::Mat img, std::string dst) 

{
int  width = img.cols;
int  height = img.rows;
const  int  biBitCount = 1;

//颜色表大小,以字节为单位,灰度图像颜色表为256*4字节,彩色图像颜色表大小为0,二值图为2*4
int  color_type_num = 2;
int  colorTablesize = color_type_num *  sizeof (RGBQUAD);
RGBQUAD *pColorTable =  new  RGBQUAD[color_type_num];
for ( int  i = 0; i < color_type_num; i++) {
pColorTable[i].rgbBlue = i*255;
pColorTable[i].rgbRed  = i*255;
pColorTable[i].rgbGreen= i*255;
pColorTable[i].rgbReserved = 0;
}

//待存储图像数据每行字节数为4的倍数
int  line_byte = (width * biBitCount/8+3)/4*4;
char * p_data = ( char *) malloc (line_byte*height);
mat_to_binary(img, line_byte, p_data);

std::ofstream fp(dst.c_str(), std::ios::binary | std::ios::out);

if (!fp.is_open()) {
// cout <<  "open "  << dst << " failed!"  << endl;
return  -1;
}

//申请位图文件头结构变量,填写文件头信息
BITMAPFILEHEADER fileHead;
fileHead.bfType = 0x4D42;   //bmp类型

fileHead.bfSize=  sizeof (BITMAPFILEHEADER) +  sizeof (BITMAPINFOHEADER)\
+ colorTablesize + line_byte*height;               //bfSize是图像文件4个组成部分之和
fileHead.bfReserved1 = 0;
fileHead.bfReserved2 = 0;    
fileHead.bfOffBits = 54+colorTablesize;               //bfOffBits是图像文件前3个部分所需空间之和    

fp.write(( char *)&fileHead,  sizeof (BITMAPFILEHEADER)); //写文件头进文件

//申请位图信息头结构变量,填写信息头信息
BITMAPINFOHEADER head; 
head.biBitCount = biBitCount;
head.biClrImportant = 0;
head.biClrUsed = 0;
head.biCompression = 0;
head.biHeight = height;
head.biPlanes = 1;
head.biSize = 40;
head.biSizeImage = line_byte*height;
head.biWidth = width;
head.biXPelsPerMeter = 0;
head.biYPelsPerMeter = 0;

//写位图信息头进内存  
fp.write(( char *)&head,  sizeof (BITMAPINFOHEADER));

//颜色表,写入文件 
fp.write(( char *)pColorTable,  sizeof (RGBQUAD)*color_type_num);  

//写位图数据进文件pBmpBuf
fp.write(( char *)p_data, height*line_byte);
fp.close();

delete  []pColorTable;
delete  []p_data;
return  0;

}

int main()

{

clock_t start,finish;  
start=clock();  
//cout << "HW .... " << endl;  

 
Mat src=imread("E:\\Image\\0_1.bmp",0);//加载图像

   
string dst="E:\\Image\\2.bmp";//存放生成1位位图的路径

int a;
 

a=save_bmp_image( src, dst) ;
finish=clock(); 
cout << finish-start   << "/" << CLOCKS_PER_SEC  << " (s) "<< endl;  

cout <<a<<endl;
cout <<dst<<endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐