将图像转换为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;
}
#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;
}
相关文章推荐
- 相机针孔模型----从世界坐标系,到相机坐标系,再到图像物理坐标系,最后到图像像素坐标系的转换过程解析
- Kinect V2.0得到RGB图像各个像素点对应的三维坐标(相机坐标系)
- OpenGL学习二十四:绘制像素,位图,图像
- 图像的深度转换,图像提取,像素级访问及时间测量
- 点阵图、矢量图、像素图、位图图像、位元块传输
- HBITMAP位图句柄转换图像
- 计算机视觉:相机成像原理:世界坐标系、相机坐标系、图像坐标系、像素坐标系之间的转换
- 实现1位,4位,8位,24位BMP位图的互相转换的方法,32位转24位
- [原]BMP位图 转换 透明 TGA图像 - 增加 alpha通道 -TGA文件格式初解
- GDI+ 在Delphi程序的应用 -- GDI+图像与GDI位图的相互转换
- 图像 学习之 绝对尺寸与像素点数转换
- MFC数字图像处理24位图转8位图 等四种图像色彩转换方式
- IplImage与位图的相互转换,Windows系统中一行像素所占的字节数为4的倍数
- GDI+ 在Delphi程序的应用 -- GDI+图像与GDI位图的相互转换
- SWT 从图像上取像素点 转换为color值
- OpenGL 编程指南笔记——第8章 OpenGL 绘制像素、位图、字体和图像
- 计算机视觉:相机成像原理:世界坐标系、相机坐标系、图像坐标系、像素坐标系之间的转换
- SWT 从图像上取像素点 转换为color值
- 将图像像素转换到较低
- 各种图像格式转换为位图BMP格式程序代码