Compare commits

...

2 Commits
main ... Simon

3 changed files with 114 additions and 8 deletions

View File

@ -11,12 +11,63 @@
// 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; FILE *file = fopen(path, "rb");
if (!file) return NULL;
fseek(file, strlen("__info2_image_file_format__"), SEEK_SET);
unsigned short anzahlBilder, width, height;
fread(&anzahlBilder, sizeof(unsigned short), 1, file);
fread(&width, sizeof(unsigned short), 1, file);
fread(&height, sizeof(unsigned short), 1, file);
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
if (!series) {
fclose(file);
return 0;
}
series->count = anzahlBilder;
series->images = malloc(sizeof(GrayScaleImage) * anzahlBilder);
series->labels = malloc(sizeof(unsigned char) * anzahlBilder);
int pixelCount = width * height;
for (unsigned int i = 0; i < anzahlBilder; i++)
{
series->images[i].width = width;
series->images[i].height = height;
series->images[i].buffer = malloc(pixelCount);
fread(series->images[i].buffer, 1, pixelCount, file);
fread(&series->labels[i], 1, 1, file);
}
fclose(file);
return series; return series;
} }
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt // TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series) void clearSeries(GrayScaleImageSeries *series)
{ {
} if (series == NULL)
return 0;
for (unsigned int i = 0; i < series->count; i++)
{
free(series->images[i].buffer);
series->images[i].buffer = NULL;
}
free(series->images);
free(series->labels);
series->images = NULL;
series->labels = NULL;
free(series);
}

View File

@ -4,32 +4,81 @@
// 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 matrix;
matrix.rows = rows;
matrix.cols = cols;
if(rows == 0 || cols == 0){
matrix.werte = NULL;
return matrix;
}
matrix.werte = malloc(rows * cols * sizeof(MatrixType));
return matrix;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
//speicher freimachen:
free(matrix->werte);
} }
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.werte[rowIdx * matrix.cols + colIdx] = value;
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
return matrix.werte[rowIdx * matrix.cols + colIdx];
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols){
//nicht sicher
}
Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix1.cols);
for(int reihe = 0; reihe < matrix1.rows; reihe++){
for(int spalte = 0; spalte < matrix1.cols; spalte++){
MatrixType wertM1 = getMatrixAt(matrix1, reihe, spalte);
MatrixType wertM2 = getMatrixAt(matrix2, reihe, spalte);
setMatrixAt(wertM1 + wertM2, ergebnisMatrix, reihe, spalte);
}
}
return ergebnisMatrix;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix2.cols);
for(int i = 0; i < matrix1.rows; i++){
for(int j = 0; j < matrix2.cols; j++){
MatrixType summe = 0;
for(int l = 0; l < matrix1.cols; l++){
summe += getMatrixAt( matrix1, i, l) * getMatrixAt(matrix2, l, j);
}
setMatrixAt(summe, ergebnisMatrix, i, j);
}
}
return ergebnisMatrix;
} }

View File

@ -6,8 +6,14 @@
typedef float MatrixType; typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct {
unsigned int rows;
unsigned int cols;
float *werte;
} 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);