OpenCV学习——图像装载函数的模拟加载24BPP的BMP文件

这个程序很基本的阐释了怎样利用OpenCV中的数据结构写自己的程序。
测试图像:

/*
文中出现的注释若为三项注释,则依次为:
Offset Size Purpose
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>

#include <cv.h>
#include <highgui.h>

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<size;i+=3) {
temp = image->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;
}

你可能感兴趣的:(数据结构)