最終更新: mikk_ni3_92 2007年12月06日(木) 15:56:10履歴
現在地 >> メニュー >> サンプルコード::OpenCV >> OpenCV::読み込みテスト
#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;
/****[画像データセット関数]**************/
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 *1==NULL)
{
printf("File Not Found : %s\n",filename);
return 0;
}
fseek(file, 18, SEEK_CUR);
if *2 != 1) {
printf("Error reading width from %s.\n", filename);
return 0;
}
if *3 != 1) {
printf("Error reading height from %s.\n", filename);
return 0;
}
size = image->sizeX * image->sizeY * 3;
if *4 != 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;
}
if *5 != 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 *6 != 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;
}
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");
std::exit(0);
}
if (!ImageLoad("test2.bmp", image1)) { //ここで、「画像ファイル」を指定する
std::exit(1);
}
/*OpenCVにデータをわたす*/
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;
std::exit(0);
}
cvNamedWindow("OpenCV FUNC",CV_WINDOW_AUTOSIZE);
cvShowImage("OpenCV FUNC",imgB);
cvWaitKey(0);
free(imgA->imageData);
cvReleaseImage( & imgA);
cvReleaseImage( & imgB);
cvDestroyAllWindows();
return 0;
}
#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;
/****[画像データセット関数]**************/
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 *1==NULL)
{
printf("File Not Found : %s\n",filename);
return 0;
}
fseek(file, 18, SEEK_CUR);
if *2 != 1) {
printf("Error reading width from %s.\n", filename);
return 0;
}
if *3 != 1) {
printf("Error reading height from %s.\n", filename);
return 0;
}
size = image->sizeX * image->sizeY * 3;
if *4 != 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;
}
if *5 != 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 *6 != 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;
}
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");
std::exit(0);
}
if (!ImageLoad("test2.bmp", image1)) { //ここで、「画像ファイル」を指定する
std::exit(1);
}
/*OpenCVにデータをわたす*/
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;
std::exit(0);
}
cvNamedWindow("OpenCV FUNC",CV_WINDOW_AUTOSIZE);
cvShowImage("OpenCV FUNC",imgB);
cvWaitKey(0);
free(imgA->imageData);
cvReleaseImage( & imgA);
cvReleaseImage( & imgB);
cvDestroyAllWindows();
return 0;
}