📄 imageio.c
字号:
/*****************************************************************************
* File: ImageIO.c
* Desc: function of Image IO
* Author: Xuhui Zhou @ Carnegie Mellon University
* Date: 11/14/2003
*****************************************************************************/
//#define _MT //enable multithread
#include "ImageIO.h"
#define MAXPATH 128
#define MAXDIR 66
#define MAXEXT 5
static char szReadFilePath[MAXPATH];
static char szSaveFilePath[MAXPATH];
static char szReadFileTitle[MAXPATH];
static char szReadImageTitle[MAXPATH];
static char szSaveFileTitle[MAXPATH];
static char szReadFileDir[MAXDIR];
static char szSaveFileDir[MAXDIR];
static char szReadExt[MAXEXT];
static char szSaveExt[MAXEXT];
static char szBuffer[MAXPATH];
static OPENFILENAME rfn, sfn;
static char szFilterSel[] = "*.trk;*.tks;*.bmp;*.dib;*.jpg;*.jpe;*.png;*.pbm;*.pgm;*.ppm;*.ras;*.tif\0*.trk;*.tks;*.bmp;*.dib;*.jpg;*.jpe;*.png;*.pbm;*.pgm;*.ppm;*.ras;*.tif\0";
static int gFrameNo=0;
static int gFrameDigit=1;
IplImage* gInputImage=NULL;
IplImage* gMaskImage=NULL;
static int gImageWidth=720;
static int gImageHeight=480;
extern POINT gaClickPoint[4]; //input track region points
static char gTrkFilePath[MAX_PATH];
char gBtkFileArray[100][MAX_PATH];
int gBtkStepNum[100];
int gBtkFileCount;
int gRptVerstion=1;
///////////////////////////////////////////////////////////////////////////////
void iio_Init(HWND hwnd)
//define OPENFILENAME rfn
{
rfn.lpstrTitle = "Pick a sequence or open a logged sequence";
rfn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
rfn.lStructSize = sizeof(OPENFILENAME);
rfn.hwndOwner = hwnd;
rfn.lpstrFilter = szFilterSel;
rfn.nMaxCustFilter = 0;
rfn.nFilterIndex = 2l;
rfn.nMaxFile = MAXPATH;
rfn.nMaxFileTitle = MAXPATH;
rfn.lpstrFile = szReadFilePath;
rfn.lpstrInitialDir = szReadFileDir;
rfn.lpstrFileTitle = szReadFileTitle;
rfn.lpstrDefExt = szReadExt;
// sfn.lpstrTitle = "Save Image in File";
// sfn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
// sfn.lStructSize = sizeof(OPENFILENAME);
// sfn.hwndOwner = hwnd;
// sfn.lpstrFilter = szFilterSel;
// sfn.nMaxCustFilter = 0;
// sfn.nFilterIndex = 2l;
// sfn.nMaxFile = MAXPATH;
// sfn.nMaxFileTitle = MAXPATH;
// sfn.lpstrFile = szSaveFileName;
// sfn.lpstrInitialDir = szSaveFileDir;
// sfn.lpstrFileTitle = szSaveFileTitle;
// sfn.lpstrDefExt = szSaveExt;
}
///////////////////////////////////////////////////////////////////////////////
int iio_OpenFileByPrompt()
//return 0=False; 1=image sequence; 2=trk file.
{
int rstOpenFile;
char suffix[5];
strcpy(szBuffer, szReadFilePath); //backup current path in case of cancel dialog
strcpy(szReadFilePath, "");
if(GetOpenFileName(&rfn) == FALSE){//cancel file dialog
//restore path
strcpy(szReadFilePath, szBuffer);
rstOpenFile = 0;
}
else{
//get suffix
strcpy(suffix, szReadFilePath+strlen(szReadFilePath)-3);
iio_RealeaseMaskImage();
if (strcmp(suffix, "trk") == 0){
//open track file
rstOpenFile = iio_ReadTrackFile(szReadFilePath);
}
else if (strcmp(suffix, "tks") == 0){
//open batch track file
rstOpenFile = iio_ReadBatchTrackFile(szReadFilePath);
}
else{//default is a image frame
//open image file
rstOpenFile = iio_ReadFileByName(szReadFilePath);
}
}
return rstOpenFile;
}
///////////////////////////////////////////////////////////////////////////////
char* iio_GetFileTitle()
{
return szReadFileTitle;
}
///////////////////////////////////////////////////////////////////////////////
char* iio_GetImageTitle()
{
return szReadImageTitle;
}
///////////////////////////////////////////////////////////////////////////////
char* iio_GetFilePath()
{
return szReadFilePath;
}
///////////////////////////////////////////////////////////////////////////////
int iio_GetFrameNo()
{
return gFrameNo;
}
///////////////////////////////////////////////////////////////////////////////
IplImage* iio_GetInputImage()
{
return gInputImage;
}
///////////////////////////////////////////////////////////////////////////////
IplImage* iio_GetMaskImage()
{
return gMaskImage;
}
///////////////////////////////////////////////////////////////////////////////
void iio_RealeaseMaskImage()
{
cvReleaseImage(&gMaskImage);
}
///////////////////////////////////////////////////////////////////////////////
int iio_ImageMaxX()
{
return gImageWidth;
}
///////////////////////////////////////////////////////////////////////////////
int iio_ImageMaxY()
{
return gImageHeight;
}
///////////////////////////////////////////////////////////////////////////////
int iio_ReadFileByName(char* filepath)
{
IplImage* inputImage;
//if empty string, return
if (strlen(filepath)==0) return 0;
inputImage = cvLoadImage(filepath, 1);
if (inputImage==NULL){
return 0;
}
cvFlip(inputImage,NULL,0);
iio_CalcFrameNo(filepath, &gFrameNo, &gFrameDigit);
//return to global variable
strcpy(szReadFilePath, filepath);
iio_CalcFrameTitle(filepath, szReadFileTitle);
iio_CalcFrameTitle(filepath, szReadImageTitle);
cvReleaseImage(&gInputImage);
gInputImage = inputImage;
gImageWidth = inputImage->width;
gImageHeight = inputImage->height;
return 1;
}
///////////////////////////////////////////////////////////////////////////////
int iio_ReadTrackFile(char* filepath)
{
FILE *filein;
char imgfile[MAXPATH];
int i;
int rst;
int readstatus;
char firststring[MAXPATH];
int trkVersion;
int row, col;
int minrow, mincol, maxrow, maxcol;
int maskval;
char maskchar[2];
strcpy(gTrkFilePath, filepath);
filein = fopen(filepath, "r");
if (filein==NULL) return 0;
strcpy(gTrkFilePath, filepath);
//read in the first string
readstatus = fscanf(filein, "%s", &firststring);
if (readstatus ==EOF) return 0;
//judge the version of trk file
trkVersion = 1; //CMU version 1.0, (bottom, left) is origin
gRptVerstion = 1;
if (strcmp(firststring, "VIVID_CID_ICD_V0.9")==0){
trkVersion = 2;
gRptVerstion = 2;
}
if (strcmp(firststring, "VIVID_CID_ICD_V1.0")==0){
trkVersion = 3;
gRptVerstion = 3;
}
if (strcmp(firststring, "V20")==0){ //(top, left) is origin
trkVersion = 4;
gRptVerstion = 1;
}
if (strcmp(firststring, "V21")==0){ //with object mask
trkVersion = 5;
gRptVerstion = 1;
}
//get image file name for different version
switch(trkVersion) {
case 1:
//verstion CMU 1.0
strcpy(imgfile, firststring);
break;
case 2:
//verstion CID 0.9
readstatus = fscanf(filein, "%s", &imgfile);
if (readstatus ==EOF) return 0;
break;
case 3:
//verstion CID 1.0
readstatus = fscanf(filein, "%s", &imgfile);
if (readstatus ==EOF) return 0;
break;
case 4:
//verstion CMU 2.0
readstatus = fscanf(filein, "%s", &imgfile);
if (readstatus ==EOF) return 0;
break;
case 5:
//verstion CMU 2.1
readstatus = fscanf(filein, "%s", &imgfile);
if (readstatus ==EOF) return 0;
break;
}
//get object blob
for(i=0;i<4;i++){
fscanf(filein, "%d", &gaClickPoint[i].x);
if (readstatus ==EOF) return 0;
fscanf(filein, "%d", &gaClickPoint[i].y);
if (readstatus ==EOF) return 0;
}
//get image
rst = iio_ReadFileByName(imgfile);
if (rst==0) return 0;
//get object mask for version 2.1
if (trkVersion==5)
{
gMaskImage = cvCreateImage(cvSize(gImageWidth, gImageHeight), 8, 1);
cvSetZero(gMaskImage);
minrow = min(gaClickPoint[0].y, gaClickPoint[2].y);
maxrow = max(gaClickPoint[0].y, gaClickPoint[2].y);
mincol = min(gaClickPoint[0].x, gaClickPoint[2].x);
maxcol = max(gaClickPoint[0].x, gaClickPoint[2].x);
for (row=minrow; row<=maxrow; row++){
for(col=mincol;col<=maxcol;col++){
//read mask
fscanf(filein, "%1s", maskchar);
if (readstatus ==EOF) return 0;
//set mask image
maskval = atoi(maskchar);
cvSetReal2D(gMaskImage, row, col, maskval*255);
}
}
cvFlip(gMaskImage,NULL,0);
//cvSaveImage("c:\\CTracker\\gMaskImage.bmp", gMaskImage);
}
fclose(filein);
//flip blob coordinate for cmu version 2.0
if (trkVersion==4 || trkVersion==5){
for(i=0;i<4;i++){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -