博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【OpenCV学习】图像装载函数的模拟加载24BPP的BMP文件
阅读量:6375 次
发布时间:2019-06-23

本文共 3467 字,大约阅读时间需要 11 分钟。

作者:

出处:

/*文中出现的注释若为三项注释,则依次为:Offset      Size     Purpose */#include 
#include
#include
#include
#include
using namespace std;/*****[自己画图用的构造体]*****/struct Image { unsigned long sizeX; unsigned long sizeY; char *data;};typedef struct Image Image;/****[BMP图像导入]**************/int ImageLoad(char *filename, Image *image){ FILE *file; unsigned long size; unsigned long i; unsigned short int planes; unsigned short int bpp; char temp; if ((file = fopen(filename, "rb"))==NULL)//读入文件 { printf("File Not Found : %s/n",filename); return 0; } fseek(file, 18, SEEK_CUR);//直接跳到宽度信息读取部分 /*读取宽度信息:18 4 the bitmap width in pixels (signed integer).*/ if ((i = (unsigned long)fread(&image->sizeX, 4, 1, file)) != 1) { printf("Error reading width from %s./n", filename); return 0; } /*读取高度信息:22 4 the bitmap height in pixels (signed integer).*/ if ((i = (unsigned long)fread(&image->sizeY, 4, 1, file)) != 1) { printf("Error reading height from %s./n", filename); return 0; } size = image->sizeX * image->sizeY * 3;//三通道 /*读取色彩平面个数(必须为1) 26 2 the number of color planes being used. Must be set to 1.*/ if ((fread(&planes, 2, 1, file)) != 1) { printf("Error reading planes from %s./n", filename); return 0; } if (planes != 1) { printf("Planes from %s is not 1: %u/n", filename, planes); return 0; } /*读取像素深度 28 2 the number of bits per pixel, which is the color depth of the image. Typical values are 1, 4, 8, 16, 24 and 32.*/ if ((i = (unsigned long)fread(&bpp, 2, 1, file)) != 1) { printf("Error reading bpp from %s./n", filename); return 0; } if (bpp != 24) { printf("Bpp from %s is not 24: %u/n", filename, bpp); return 0; } fseek(file, 24, SEEK_CUR); image->data = (char *) malloc(size); if (image->data == NULL) { printf("Error allocating memory for color-corrected image data"); return 0; } if ((i = (unsigned long)fread(image->data, size, 1, file)) != 1) { printf("Error reading image data from %s./n", filename); return 0; } for (i=0;i
data[i]; image->data[i] = image->data[i+2]; image->data[i+2] = temp; }//RGB<->BGR return 1;}/*****[准备装载图像]******************/IplImage *imgA;CvSize window_size;void LoadImage(){ Image *image1; image1 = (Image *) malloc(sizeof(Image)); if (image1 == NULL) { printf("Error allocating space for image"); return ; } if (!ImageLoad("test2.bmp", image1)) { return ; } window_size.width = image1->sizeX; window_size.height= image1->sizeY; imgA = cvCreateImage(window_size,IPL_DEPTH_8U,3); imgA->imageData = image1->data; free(image1); cvCvtColor(imgA, imgA, CV_RGB2BGR); cvFlip(imgA,NULL,0); }int main( int argc, char **argv){ LoadImage(); cvNamedWindow("MY FUNCTION",CV_WINDOW_AUTOSIZE); cvShowImage("MY FUNCTION",imgA); /*OpenCV*/ IplImage *imgB = cvLoadImage( "test2.bmp", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); if(imgB ==NULL) { cout<<"Can't Load Image ." << endl; exit(0); } cvNamedWindow("OpenCV FUNC",CV_WINDOW_AUTOSIZE); cvShowImage("OpenCV FUNC",imgB); cvWaitKey(0); free(imgA->imageData); cvReleaseImage( & imgA); cvReleaseImage( & imgB); cvDestroyAllWindows(); return 0;}

 

作者:

出处:

转载地址:http://pgcqa.baihongyu.com/

你可能感兴趣的文章
Docker学习之路(一)
查看>>
Java工程师知识图谱
查看>>
[Spring实战系列](2)Maven创建Spring-HelloWorld项目
查看>>
CentOS安装运行NodeJS框架Express
查看>>
白话版 动态规划法
查看>>
(十六) 整合spring cloud云架构 -使用spring cloud Bus刷新配置
查看>>
全面总结国内BATH等大厂开源的安卓有关的库(持续更新中...)
查看>>
NutUI 2.1.0 发布,移动端轻量级 Vue 组件库
查看>>
缓存系列文章--热点key问题
查看>>
CSS动画小结
查看>>
初学乍练:一键部署redis集群
查看>>
从一个骗局谈生活中的基础算法
查看>>
JdbcTemplate使用别名绑定变量
查看>>
详解Apache Kylin安装和踩坑
查看>>
MVC架构中,用户的请求简单梳理
查看>>
RPA机器人为何会在财务共享领域刮起旋风?
查看>>
NGINX的奇淫技巧 —— 2. IF AND 和 OR
查看>>
排列组合技术
查看>>
哈工大发明“电子体毛”,让机器人学会“敏感”
查看>>
上传一张照片,让算法告诉你是否患有抑郁症
查看>>