forked from freudenreichan/info2Praktikum-NeuronalesNetz
Implement ImageInput
This commit is contained in:
1 parent
8babce0896
commit
c26c5247e3
2 files changed
+84
-10
No files matched your search
+82
-10
@@ -6,17 +6,89 @@
|
|||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
static int controlFile(FILE *file) {
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
if (file == NULL) {
|
||||||
{
|
return 0;
|
||||||
GrayScaleImageSeries *series = NULL;
|
}
|
||||||
|
char buffer[BUFFER_SIZE];
|
||||||
return series;
|
fread(buffer, sizeof(char), strlen(FILE_HEADER_STRING), file);
|
||||||
|
buffer[strlen(FILE_HEADER_STRING)] = '\0';
|
||||||
|
return !strcmp(FILE_HEADER_STRING, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
static unsigned int readNumber(FILE *file) {
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
unsigned int number = 0;
|
||||||
{
|
fread(&number, sizeof(unsigned short), 1, file);
|
||||||
|
return number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// fillBuffer und readLabel muss immer in einer Schleife zusammen stehen
|
||||||
|
static int fillBuffer(unsigned char buffer[], unsigned int bufferSize, FILE *file) {
|
||||||
|
int readcount = fread(buffer, sizeof(GrayScalePixelType), bufferSize, file);
|
||||||
|
if (readcount == 0)
|
||||||
|
return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void readLabels(FILE *file, unsigned char label[], unsigned int count) {
|
||||||
|
fread(label + count, sizeof(char), 1, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
GrayScaleImageSeries *readImages(const char *path) {
|
||||||
|
FILE *file = fopen(path, "rb");
|
||||||
|
|
||||||
|
if (!controlFile(file)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
GrayScaleImageSeries *firstSeries = malloc(sizeof(GrayScaleImageSeries));
|
||||||
|
firstSeries ->count = readNumber(file);
|
||||||
|
unsigned int width = readNumber(file);
|
||||||
|
unsigned int height = readNumber(file);
|
||||||
|
|
||||||
|
firstSeries ->labels = malloc(firstSeries ->count*sizeof(unsigned char));
|
||||||
|
firstSeries ->images = malloc(firstSeries ->count*sizeof(GrayScaleImage));
|
||||||
|
firstSeries ->addrSourceTxt = file;
|
||||||
|
if (firstSeries ->labels == NULL || firstSeries ->images == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
int fault = 1;
|
||||||
|
unsigned int const bufferSize = width * height;
|
||||||
|
for (unsigned int i = 0; i < firstSeries ->count && fault; i++)
|
||||||
|
{
|
||||||
|
GrayScaleImage *grayScaleImage = &firstSeries ->images[i];
|
||||||
|
grayScaleImage ->buffer = malloc(width * height*sizeof(GrayScalePixelType));
|
||||||
|
grayScaleImage ->width = width;
|
||||||
|
grayScaleImage -> height = height;
|
||||||
|
if (width == 0 || height == 0 || firstSeries ->count == 0) { // prüfen ob eingabe richtig ist
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fault = fillBuffer(grayScaleImage ->buffer, bufferSize, file);
|
||||||
|
readLabels(file, firstSeries ->labels, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return firstSeries;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void clearSeries(GrayScaleImageSeries *series) {
|
||||||
|
if (series == NULL) {return;}
|
||||||
|
if (series->images != NULL) {
|
||||||
|
for (unsigned int i = 0; i < series->count; i++) {
|
||||||
|
const GrayScaleImage image = series->images[i];
|
||||||
|
if (image.buffer != NULL) {
|
||||||
|
free(image.buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(series->images);
|
||||||
|
}
|
||||||
|
if (series->labels != NULL) {
|
||||||
|
free(series->labels);
|
||||||
|
}
|
||||||
|
fclose(series->addrSourceTxt);
|
||||||
|
free(series);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -15,9 +15,11 @@ typedef struct
|
|||||||
GrayScaleImage *images;
|
GrayScaleImage *images;
|
||||||
unsigned char *labels;
|
unsigned char *labels;
|
||||||
unsigned int count;
|
unsigned int count;
|
||||||
|
FILE *addrSourceTxt; // um in clearSeries den Text schließen zu können
|
||||||
} GrayScaleImageSeries;
|
} GrayScaleImageSeries;
|
||||||
|
|
||||||
GrayScaleImageSeries *readImages(const char *path);
|
GrayScaleImageSeries *readImages(const char *path);
|
||||||
void clearSeries(GrayScaleImageSeries *series);
|
void clearSeries(GrayScaleImageSeries *series);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in new issue
Block a user