This commit is contained in:
Nick Haller 2025-11-11 14:09:06 +01:00
parent 67c5110e5c
commit d4375c31e8
4 changed files with 247 additions and 246 deletions

View File

@ -1,95 +1,96 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "imageInput.h" #include "imageInput.h"
#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 Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen // TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path) GrayScaleImageSeries *readImages(const char *path)
{ {
// GrayScaleImageSeries *series = NULL; // GrayScaleImageSeries *series = NULL;
// return series; // return series;
FILE *file = fopen(path, "rb"); FILE *file = fopen(path, "rb");
if (file == NULL) { if (file == NULL) {
return NULL; // Datei existiert nicht oder konnte nicht geöffnet werden return NULL; // Datei existiert nicht oder konnte nicht geöffnet werden
} }
// Überprüfe den Dateitag // Überprüfe den Dateitag
char fileTag[25]; char fileTag[25];
fread(fileTag, sizeof(fileTag[0]), 24, file); fread(fileTag, sizeof(fileTag[0]), 24, file);
fileTag[24] = '\0'; // Null-Terminierung des Strings fileTag[24] = '\0'; // Null-Terminierung des Strings
if (strcmp(fileTag, "__info2_image_file_format__") != 0) { if (strcmp(fileTag, "__info2_image_file_format__") != 0) {
fclose(file); fclose(file);
return NULL; // Ungültiges Dateiformat return NULL; // Ungültiges Dateiformat
} }
// Lese die Metadaten: Anzahl der Bilder, Breite und Höhe // Lese die Metadaten: Anzahl der Bilder, Breite und Höhe
unsigned short numberOfImages, width, height; unsigned short numberOfImages, width, height;
fread(&numberOfImages, sizeof(numberOfImages), 1, file); fread(&numberOfImages, sizeof(numberOfImages), 1, file);
fread(&width, sizeof(width), 1, file); fread(&width, sizeof(width), 1, file);
fread(&height, sizeof(height), 1, file); fread(&height, sizeof(height), 1, file);
// Speicher für die Bildserie und die Bilddaten allozieren // Speicher für die Bildserie und die Bilddaten allozieren
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries)); GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
if (series == NULL) { if (series == NULL) {
fclose(file); fclose(file);
return NULL; // Speicher konnte nicht allokiert werden return NULL; // Speicher konnte nicht allokiert werden
} }
series->count = numberOfImages; series->count = numberOfImages;
series->images = (GrayScaleImage *)malloc(numberOfImages * sizeof(GrayScaleImage)); series->images = (GrayScaleImage *)malloc(numberOfImages * sizeof(GrayScaleImage));
series->labels = (unsigned char *)malloc(numberOfImages * sizeof(unsigned char)); series->labels = (unsigned char *)malloc(numberOfImages * sizeof(unsigned char));
if (series->images == NULL || series->labels == NULL) { if (series->images == NULL || series->labels == NULL) {
free(series); free(series);
fclose(file); fclose(file);
return NULL; // Speicher konnte nicht allokiert werden return NULL; // Speicher konnte nicht allokiert werden
} }
// Lese die Bilddaten und die Labels // Lese die Bilddaten und die Labels
for (int i = 0; i < numberOfImages; i++) { for (int i = 0; i < numberOfImages; i++) {
series->images[i].width = width; series->images[i].width = width;
series->images[i].height = height; series->images[i].height = height;
series->images[i].buffer = (GrayScalePixelType *)malloc(width * height * sizeof(GrayScalePixelType)); series->images[i].buffer = (GrayScalePixelType *)malloc(width * height * sizeof(GrayScalePixelType));
if (series->images[i].buffer == NULL) { if (series->images[i].buffer == NULL) {
// Fehlerbehandlung: Speicher freigeben, wenn Allocation fehlschlägt // Fehlerbehandlung: Speicher freigeben, wenn Allocation fehlschlägt
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++)
free(series->images[j].buffer); {
} free(series->images[j].buffer);
free(series->images); }
free(series->labels); free(series->images);
free(series); free(series->labels);
fclose(file); free(series);
return NULL; fclose(file);
} return NULL;
}
// Lese die Pixel-Daten und das Label
fread(series->images[i].buffer, sizeof(GrayScalePixelType), width * height, file); // Lese die Pixel-Daten und das Label
fread(&series->labels[i], sizeof(unsigned char), 1, file); fread(series->images[i].buffer, sizeof(GrayScalePixelType), width * height, file);
} fread(&series->labels[i], sizeof(unsigned char), 1, file);
}
fclose(file);
return series; fclose(file);
} return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series) // TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
{ void clearSeries(GrayScaleImageSeries *series)
if (series == NULL) return; {
if (series == NULL) return;
for (int i = 0; i < series->count; i++) {
free(series->images[i].buffer); // Speicher für das Bild freigeben for (int i = 0; i < series->count; i++) {
} free(series->images[i].buffer); // Speicher für das Bild freigeben
}
free(series->images); // Speicher für die Bild-Array freigeben
free(series->labels); // Speicher für die Labels freigeben free(series->images); // Speicher für die Bild-Array freigeben
free(series); free(series->labels); // Speicher für die Labels freigeben
free(series);
} }

252
matrix.c
View File

@ -1,127 +1,127 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "matrix.h" #include "matrix.h"
// TODO Matrix-Funktionen implementieren // TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix m = {0, 0, NULL}; Matrix m = {0, 0, NULL};
if (rows > 0 && cols > 0) if (rows > 0 && cols > 0)
{ {
m.rows = rows; m.rows = rows;
m.cols = cols; m.cols = cols;
m.buffer = malloc(rows * cols * sizeof(int)); m.buffer = malloc(rows * cols * sizeof(int));
} }
return m; return m;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix == NULL) if (matrix == NULL)
{ {
return; return;
} }
// Speicher freigeben, falls vorhanden // Speicher freigeben, falls vorhanden
free(matrix->buffer); free(matrix->buffer);
matrix->buffer = NULL; matrix->buffer = NULL;
// Metadaten zurücksetzen // Metadaten zurücksetzen
matrix->rows = 0; matrix->rows = 0;
matrix->cols = 0; matrix->cols = 0;
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // setzte Matrix auf den Wert value am Punkt (row col) matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // setzte Matrix auf den Wert value am Punkt (row col)
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
MatrixType value = 0; MatrixType value = 0;
if (rowIdx < matrix.rows && colIdx < matrix.cols) if (rowIdx < matrix.rows && colIdx < matrix.cols)
{ {
value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col) value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col)
} }
return value; return value;
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = {0}; Matrix result = {0};
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{ {
return result; return result;
} }
result.rows = matrix1.rows; result.rows = matrix1.rows;
result.cols = matrix1.cols; result.cols = matrix1.cols;
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
// wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen // wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
if (result.buffer == NULL) if (result.buffer == NULL)
{ {
result.rows = result.cols = 0; result.rows = result.cols = 0;
return result; return result;
} }
// Matritzenaddition // Matritzenaddition
for (unsigned int i = 0; i < result.rows; i++) for (unsigned int i = 0; i < result.rows; i++)
{ {
for (unsigned int j = 0; j < result.cols; j++) for (unsigned int j = 0; j < result.cols; j++)
{ {
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j]; result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
} }
} }
return result; return result;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = {0}; Matrix result = {0};
if (matrix1.cols != matrix2.rows) if (matrix1.cols != matrix2.rows)
{ {
return result; return result;
} }
result.rows = matrix1.rows; result.rows = matrix1.rows;
result.cols = matrix2.cols; result.cols = matrix2.cols;
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
// wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen // wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
if (result.buffer == NULL) if (result.buffer == NULL)
{ {
result.rows = result.cols = 0; result.rows = result.cols = 0;
return result; return result;
} }
// Matritzenmultiplikation // Matritzenmultiplikation
for (int r = 0; r < result.rows; r++) // Zeile in Ergebnis for (int r = 0; r < result.rows; r++) // Zeile in Ergebnis
{ {
for (int m = 0; m < result.cols; m++) // Spalte in Ergebnis for (int m = 0; m < result.cols; m++) // Spalte in Ergebnis
{ {
MatrixType sum = 0; MatrixType sum = 0;
for (int n = 0; n < matrix1.cols; n++) for (int n = 0; n < matrix1.cols; n++)
{ {
sum += matrix1.buffer[r * matrix1.cols + n] * sum += matrix1.buffer[r * matrix1.cols + n] *
matrix2.buffer[n * matrix2.cols + m]; matrix2.buffer[n * matrix2.cols + m];
} }
result.buffer[r * result.cols + m] = sum; result.buffer[r * result.cols + m] = sum;
} }
} }
return result; return result;
} }

View File

@ -1,26 +1,26 @@
#ifndef MATRIX_H #ifndef MATRIX_H
#define MATRIX_H #define MATRIX_H
#define UNDEFINED_MATRIX_VALUE 0 #define UNDEFINED_MATRIX_VALUE 0
typedef float MatrixType; typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct typedef struct
{ {
unsigned int rows; // Anzahl der Zeilen unsigned int rows; // Anzahl der Zeilen
unsigned int cols; // Anzahl der Spalten unsigned int cols; // Anzahl der Spalten
MatrixType *buffer; // Zeiger auf die Matrixdaten MatrixType *buffer; // Zeiger auf die Matrixdaten
} Matrix; } Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix); void clearMatrix(Matrix *matrix);
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx); void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx); MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
Matrix add(const Matrix matrix1, const Matrix matrix2); Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2); Matrix multiply(const Matrix matrix1, const Matrix matrix2);
#endif #endif

BIN
runMatrixTests Executable file

Binary file not shown.