generated from freudenreichan/info2Praktikum-NeuronalesNetz
Imageinput bis auf einen Test fertig
This commit is contained in:
parent
cd7d1fc0af
commit
39563aec23
60
imageInput.c
60
imageInput.c
@ -8,23 +8,17 @@
|
||||
|
||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||
|
||||
static void readPictureData(const char *path,const short ptrPicturesCount, const short ptrPictureWidth, const short ptrPictureHeigth)
|
||||
static void readPictureData(FILE *file, unsigned short *ptrPicturesCount, unsigned short *ptrPictureWidth, unsigned short *ptrPictureHeight)
|
||||
{
|
||||
unsigned int header = strlen(FILE_HEADER_STRING);
|
||||
|
||||
FILE *file = fopen(path,"rb");
|
||||
if (file == NULL){
|
||||
perror("failed open file");
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(file, header, SEEK_SET);
|
||||
|
||||
fread(ptrPicturesCount, sizeof(unsigned short), 1, file);
|
||||
fread(ptrPictureWidth, sizeof(unsigned short), 1, file);
|
||||
fread(ptrPictureHeight, sizeof(unsigned short), 1, file);
|
||||
|
||||
fclose(file);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -32,31 +26,47 @@ static void readPictureData(const char *path,const short ptrPicturesCount, const
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
unsigned short picturesCount, pictureWidth, pictureHeight;
|
||||
Matrix labels = creatmatrix(1,picturesCount);
|
||||
Matrix images = creatmatrix(1,picturesCount);
|
||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));;
|
||||
|
||||
FILE *file = fopen(path,"rb");
|
||||
|
||||
if (file == NULL){
|
||||
printf("failed open file\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
readPictureData(file, &picturesCount, &pictureWidth, &pictureHeight);
|
||||
|
||||
series->labels = (unsigned char*)malloc(picturesCount * sizeof(char));
|
||||
series->images = (GrayScaleImage*)malloc(picturesCount * sizeof(GrayScaleImage));
|
||||
series->count = picturesCount;
|
||||
|
||||
for(int i = 0; i < picturesCount; i++){
|
||||
series->images[i].width = pictureWidth;
|
||||
series->images[i].height = pictureHeight;
|
||||
series->images[i].buffer = (unsigned char*)malloc(pictureWidth * pictureHeight*sizeof(unsigned char));
|
||||
|
||||
fread(series->images[i].buffer, sizeof(unsigned char), pictureWidth * pictureHeight, file);
|
||||
|
||||
fread(&series->labels[i], sizeof(unsigned char), 1, file);
|
||||
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
return series;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
for(int i = 0; i < series->count; i++){
|
||||
free(series->images[i].buffer);
|
||||
}
|
||||
|
||||
free(series->images);
|
||||
free(series->labels);
|
||||
}
|
||||
|
||||
MatrixChar createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
if(rows <= 0 || cols <= 0){
|
||||
Matrix matrix = { 0 , 0 , UNDEFINED_MATRIX_VALUE };
|
||||
return matrix;
|
||||
}
|
||||
|
||||
MatrixChar matrix = { rows , cols };
|
||||
|
||||
matrix.buffer = malloc((sizeof(char)*rows*cols));
|
||||
|
||||
return matrix;
|
||||
}
|
||||
@ -17,11 +17,6 @@ typedef struct
|
||||
unsigned int count;
|
||||
} GrayScaleImageSeries;
|
||||
|
||||
typedef struct{
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
MatrixType *buffer;
|
||||
}MatrixChar;
|
||||
|
||||
GrayScaleImageSeries *readImages(const char *path);
|
||||
void clearSeries(GrayScaleImageSeries *series);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user