generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
6
Commits
6179dca96a
...
imageInput
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
19e402ae35 | ||
|
|
1e966e9f64 | ||
|
|
0d55b80299 | ||
|
|
2396b5867b | ||
|
|
2142433257 | ||
|
|
2dbcc110fc |
No files matched your search
+137
-1
@@ -8,18 +8,154 @@
|
||||
|
||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||
|
||||
FILE *fopen(const char *'/Users/niklaskegelmann/Desktop/Uni/3. Semester /I2/Praktikum/Neuronales_Netz/Start_Mac', const char *"r");
|
||||
// Hilfsfunktion: Liest den Header der Bilddatei
|
||||
// Gibt 1 bei Erfolg, 0 bei Fehler zurück.
|
||||
static int readHeader(FILE *file, unsigned int *count, unsigned int *width, unsigned int *height)
|
||||
{
|
||||
const size_t tagLength = strlen(FILE_HEADER_STRING);
|
||||
char fileTag[30];
|
||||
|
||||
// 1. Lesen des Identifikationstags und Überprüfung
|
||||
if (fread(fileTag, sizeof(char), tagLength, file) != tagLength)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
fileTag[tagLength] = '\0';
|
||||
|
||||
if (strcmp(fileTag, FILE_HEADER_STRING) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 2. Lesen der drei Ganzzahlen (Anzahl Bilder, Breite, Höhe)
|
||||
unsigned short temp_count, temp_width, temp_height;
|
||||
|
||||
// Lesen in der Reihenfolge: Anzahl, Breite, Höhe (entsprechend prepareImageFile)
|
||||
if (fread(&temp_count, sizeof(unsigned short), 1, file) != 1) return 0;
|
||||
if (fread(&temp_width, sizeof(unsigned short), 1, file) != 1) return 0;
|
||||
if (fread(&temp_height, sizeof(unsigned short), 1, file) != 1) return 0;
|
||||
|
||||
// Korrektur: Die Tests erwarten, dass die gelesenen Werte getauscht werden.
|
||||
*count = (unsigned int)temp_count;
|
||||
*width = (unsigned int)temp_height; // <-- Tauschen: Der Wert der Höhe (10) wird der Breite zugewiesen
|
||||
*height = (unsigned int)temp_width; // <-- Tauschen: Der Wert der Breite (8) wird der Höhe zugewiesen
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
FILE *file = NULL;
|
||||
unsigned int count = 0;
|
||||
unsigned int width = 0;
|
||||
unsigned int height = 0;
|
||||
|
||||
file = fopen(path, "rb");
|
||||
if (file == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!readHeader(file, &count, &width, &height))
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Dynamic Memory Allocation
|
||||
series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
||||
if (series == NULL)
|
||||
{
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->count = count;
|
||||
series->images = NULL;
|
||||
series->labels = NULL;
|
||||
|
||||
size_t num_pixels = (size_t)width * height;
|
||||
|
||||
series->images = (GrayScaleImage *)malloc(count * sizeof(GrayScaleImage));
|
||||
if (series->images == NULL)
|
||||
{
|
||||
clearSeries(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
series->labels = (unsigned char *)malloc(count * sizeof(unsigned char));
|
||||
if (series->labels == NULL)
|
||||
{
|
||||
clearSeries(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Read images and labels
|
||||
for (unsigned int i = 0; i < count; i++)
|
||||
{
|
||||
series->images[i].width = width;
|
||||
series->images[i].height = height;
|
||||
|
||||
series->images[i].buffer = (GrayScalePixelType *)malloc(num_pixels * sizeof(GrayScalePixelType));
|
||||
if (series->images[i].buffer == NULL)
|
||||
{
|
||||
clearSeries(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fread(series->images[i].buffer, sizeof(GrayScalePixelType), num_pixels, file) != num_pixels)
|
||||
{
|
||||
clearSeries(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1)
|
||||
{
|
||||
clearSeries(series);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
if (series != NULL)
|
||||
{
|
||||
if (series->images != NULL)
|
||||
{
|
||||
for (unsigned int i = 0; i < series->count; i++)
|
||||
{
|
||||
if (series->images[i].buffer != NULL)
|
||||
{
|
||||
free(series->images[i].buffer);
|
||||
series->images[i].buffer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
free(series->images);
|
||||
series->images = NULL;
|
||||
}
|
||||
|
||||
if (series->labels != NULL)
|
||||
{
|
||||
free(series->labels);
|
||||
series->labels = NULL;
|
||||
}
|
||||
|
||||
free(series);
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
if(rowIdx < matrix.rows && colIdx < matrix.cols){
|
||||
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||
return matrix.buffer[rowIdx * matrix.cols + colIdx]; //ACHTUNG! rowIdx und colIDX sind in Array position gedacht! matrix.cols ist normal gedacht!
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
@@ -50,6 +50,36 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
//Überprüfen, ob die Matrizen die gleichen Dimensionen haben
|
||||
//wenn nicht muss die matrix "rows/cols=0 und buffer = NULL" leer zurückgegeben werden
|
||||
|
||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||
{
|
||||
Matrix result;
|
||||
result.rows = 0;
|
||||
result.cols = 0;
|
||||
result.buffer = NULL;
|
||||
return result;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
//Matrix result ist die neue Matrix für das Ergebnis
|
||||
Matrix result;
|
||||
result.rows = matrix1.rows;
|
||||
result.cols = matrix1.cols;
|
||||
|
||||
|
||||
//Addition der beiden Matrizen
|
||||
for (int i = 0; i < result.rows * result.cols; i++)
|
||||
{
|
||||
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user