imageInput.c matrix.c matrix.h angepasst, läuft jetzt
This commit is contained in:
parent
6d657e2e6d
commit
46183d084c
109
imageInput.c
109
imageInput.c
@ -6,17 +6,124 @@
|
||||
#define BUFFER_SIZE 100
|
||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||
|
||||
static void strip_newline(char *s)
|
||||
{
|
||||
if (!s) return;
|
||||
size_t len = strlen(s);
|
||||
while (len > 0 && (s[len-1] == '\n' || s[len-1] == '\r')) {
|
||||
s[len-1] = '\0';
|
||||
len--;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
GrayScaleImageSeries *series = NULL;
|
||||
if (!path) return NULL;
|
||||
|
||||
FILE *fp = fopen(path, "r");
|
||||
if (!fp) return NULL;
|
||||
|
||||
char header[BUFFER_SIZE];
|
||||
if (!fgets(header, sizeof(header), fp)) {
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
strip_newline(header);
|
||||
if (strcmp(header, FILE_HEADER_STRING) != 0) {
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned int count = 0;
|
||||
if (fscanf(fp, "%u", &count) != 1) {
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||
if (!series) {
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
series->count = count;
|
||||
series->images = NULL;
|
||||
series->labels = NULL;
|
||||
|
||||
if (count == 0) {
|
||||
series->images = NULL;
|
||||
series->labels = NULL;
|
||||
fclose(fp);
|
||||
return series;
|
||||
}
|
||||
|
||||
series->images = calloc(count, sizeof(GrayScaleImage));
|
||||
series->labels = calloc(count, sizeof(unsigned char));
|
||||
if (!series->images || !series->labels) {
|
||||
clearSeries(series);
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i) {
|
||||
unsigned int label = 0;
|
||||
unsigned int width = 0, height = 0;
|
||||
if (fscanf(fp, "%u %u %u", &label, &width, &height) != 3) {
|
||||
clearSeries(series);
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
series->labels[i] = (unsigned char)(label & 0xFF);
|
||||
series->images[i].width = width;
|
||||
series->images[i].height = height;
|
||||
|
||||
size_t pixels = (size_t)width * (size_t)height;
|
||||
if (pixels == 0) {
|
||||
series->images[i].buffer = NULL;
|
||||
continue;
|
||||
}
|
||||
|
||||
series->images[i].buffer = malloc(pixels * sizeof(GrayScalePixelType));
|
||||
if (!series->images[i].buffer) {
|
||||
clearSeries(series);
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (size_t p = 0; p < pixels; ++p) {
|
||||
int pix = 0;
|
||||
if (fscanf(fp, "%d", &pix) != 1) {
|
||||
clearSeries(series);
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
if (pix < 0) pix = 0;
|
||||
if (pix > 255) pix = 255;
|
||||
series->images[i].buffer[p] = (GrayScalePixelType)pix;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return series;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
void clearSeries(GrayScaleImageSeries *series)
|
||||
{
|
||||
if (!series) return;
|
||||
if (series->images) {
|
||||
for (unsigned int i = 0; i < series->count; ++i) {
|
||||
free(series->images[i].buffer);
|
||||
series->images[i].buffer = NULL;
|
||||
}
|
||||
free(series->images);
|
||||
series->images = NULL;
|
||||
}
|
||||
if (series->labels) {
|
||||
free(series->labels);
|
||||
series->labels = NULL;
|
||||
}
|
||||
free(series);
|
||||
}
|
||||
68
matrix.c
68
matrix.c
@ -6,30 +6,92 @@
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix m;
|
||||
// Default: leere Matrix bei Fehler/Null-Dimensionen
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
m.buffer = NULL;
|
||||
|
||||
if (rows == 0 || cols == 0) {
|
||||
return m;
|
||||
}
|
||||
|
||||
size_t count = (size_t)rows * (size_t)cols;
|
||||
m.buffer = (MatrixType *)calloc(count, sizeof(MatrixType));
|
||||
if (!m.buffer) {
|
||||
// Allocation failed -> return empty matrix
|
||||
return m;
|
||||
}
|
||||
|
||||
// Nur bei erfolgreicher Allokation Dimensionen setzen
|
||||
m.rows = rows;
|
||||
m.cols = cols;
|
||||
return m;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
if (!matrix) return;
|
||||
free(matrix->buffer);
|
||||
matrix->buffer = NULL;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if (matrix.buffer == NULL) return;
|
||||
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return;
|
||||
size_t idx = (size_t)rowIdx * matrix.cols + colIdx;
|
||||
matrix.buffer[idx] = value;
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if (matrix.buffer == NULL) return (MatrixType)0;
|
||||
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return (MatrixType)0;
|
||||
size_t idx = (size_t)rowIdx * matrix.cols + colIdx;
|
||||
return matrix.buffer[idx];
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
Matrix result;
|
||||
result.rows = result.cols = 0;
|
||||
result.buffer = NULL;
|
||||
|
||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) return result;
|
||||
|
||||
result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
if (!result.buffer) return result;
|
||||
|
||||
size_t count = (size_t)matrix1.rows * matrix1.cols;
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
Matrix result;
|
||||
result.rows = result.cols = 0;
|
||||
result.buffer = NULL;
|
||||
|
||||
if (matrix1.cols != matrix2.rows) return result;
|
||||
if (matrix1.rows == 0 || matrix1.cols == 0 || matrix2.cols == 0) return result;
|
||||
|
||||
result = createMatrix(matrix1.rows, matrix2.cols);
|
||||
if (!result.buffer) return result;
|
||||
|
||||
for (unsigned int i = 0; i < matrix1.rows; ++i) {
|
||||
for (unsigned int j = 0; j < matrix2.cols; ++j) {
|
||||
MatrixType sum = (MatrixType)0;
|
||||
for (unsigned int k = 0; k < matrix1.cols; ++k) {
|
||||
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||
}
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
8
matrix.h
8
matrix.h
@ -5,6 +5,13 @@
|
||||
|
||||
typedef float MatrixType;
|
||||
|
||||
// Definition der Matrix-Struktur hinzufügen
|
||||
typedef struct {
|
||||
MatrixType *buffer;
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
} Matrix;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
|
||||
@ -15,5 +22,4 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2);
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user