Kommentare

This commit is contained in:
Bannach 2025-11-17 17:29:25 +01:00
parent 2b3c8663fe
commit 6a32226f07
2 changed files with 53 additions and 22 deletions

View File

@ -23,7 +23,7 @@ GrayScaleImageSeries *readImages(const char *path)
{ {
FILE* file = fopen(path,"rb"); FILE* file = fopen(path,"rb");
unsigned int width=0,height=0; unsigned int width=0,height=0;
GrayScaleImageSeries *imageSeries = (GrayScaleImageSeries*)malloc(sizeof(GrayScaleImageSeries)); GrayScaleImageSeries *imageSeries = malloc(sizeof(GrayScaleImageSeries));
if (imageSeries ==NULL) { if (imageSeries ==NULL) {
printf("Error allocating memory for GrayScaleImageSeries\n"); printf("Error allocating memory for GrayScaleImageSeries\n");
return NULL; return NULL;
@ -33,33 +33,34 @@ GrayScaleImageSeries *readImages(const char *path)
fclose(file); fclose(file);
return NULL; return NULL;
} }
fread(&imageSeries->count,sizeof(unsigned short),1,file); fread(&imageSeries->count,sizeof(unsigned short),1,file);//Einlesen der Anzahl an Bildern
imageSeries->images = (GrayScaleImage*)malloc(imageSeries->count * sizeof(GrayScaleImage)); imageSeries->images = (GrayScaleImage*)malloc(imageSeries->count * sizeof(GrayScaleImage));
fread(&width,sizeof(unsigned short),1,file); fread(&width,sizeof(unsigned short),1,file);//Einlesen der Breite
fread(&height,sizeof(unsigned short),1,file); fread(&height,sizeof(unsigned short),1,file);//Einlesen der Höhe
imageSeries->labels = malloc(imageSeries->count * sizeof(unsigned char)); imageSeries->labels = malloc(imageSeries->count * sizeof(unsigned char));
if(imageSeries->labels == NULL) { if(imageSeries->labels == NULL) { //Wenn Speicher nicht reservierbar
free(imageSeries); free(imageSeries);
fclose(file); fclose(file);
return NULL; return NULL;
} }
for(int i = 0; i < imageSeries->count; i++) { for(int i = 0; i < imageSeries->count; i++) {
imageSeries->images[i].width = width; imageSeries->images[i].width = width; //Bilder Einlesen //Breite und Höhe immer gleich
imageSeries->images[i].height = height; imageSeries->images[i].height = height;
const unsigned int imageSize = height*width; const unsigned int imageSize = height*width; //Anzahl Pixel insgesamt
imageSeries->images[i].buffer = (GrayScalePixelType*) malloc(imageSize*sizeof(GrayScalePixelType)); imageSeries->images[i].buffer = (GrayScalePixelType*) malloc(imageSize*sizeof(GrayScalePixelType));
if(imageSeries->images[i].buffer == NULL) { if(imageSeries->images[i].buffer == NULL) {
free(imageSeries->images); free(imageSeries->images); //Falls speicher nicht reservierbar
free(imageSeries); free(imageSeries);
fclose(file); fclose(file);
return NULL; return NULL;
} }
for (int j=0;j<imageSize;j++) for (int j=0;j<imageSize;j++)
{ {
//Einlesen der Pixel
fread(&imageSeries->images[i].buffer[j],sizeof(GrayScalePixelType),1,file); fread(&imageSeries->images[i].buffer[j],sizeof(GrayScalePixelType),1,file);
} }
fread(&imageSeries->labels[i],sizeof(unsigned char),1,file); fread(&imageSeries->labels[i],sizeof(unsigned char),1,file);//Einlesen des Labels
} }
fclose(file); fclose(file);

View File

@ -19,6 +19,12 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
matrix.rows = rows; matrix.rows = rows;
matrix.cols = cols; matrix.cols = cols;
matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * rows * cols); matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * rows * cols);
if (matrix.buffer == NULL) {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
} }
return matrix; return matrix;
@ -72,18 +78,17 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
Matrix add(const Matrix matrix1, const Matrix matrix2) { Matrix add(const Matrix matrix1, const Matrix matrix2) {
Matrix matrix; Matrix matrix;
if(matrix1.rows != matrix2.rows )
{
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
if (matrix1.cols < matrix2.cols) { if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) { //Broadcasting für matrix1
matrix.cols = matrix2.cols; matrix.cols = matrix2.cols;
matrix.rows = matrix2.rows; matrix.rows = matrix2.rows;
matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols); matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols);
if (matrix.buffer == NULL) {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
for (int i = 0; i < matrix.rows; i++) { for (int i = 0; i < matrix.rows; i++) {
for (int j = 0; j < matrix.cols; j++){ for (int j = 0; j < matrix.cols; j++){
matrix.buffer[i * matrix.cols + j] = matrix2.buffer[i * matrix.cols + j]+matrix1.buffer[i*matrix1.cols]; matrix.buffer[i * matrix.cols + j] = matrix2.buffer[i * matrix.cols + j]+matrix1.buffer[i*matrix1.cols];
@ -94,11 +99,17 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
return matrix; return matrix;
} }
if (matrix1.cols > matrix2.cols) else if (matrix2.cols == 1 && matrix1.rows == matrix2.rows)
{ { //Broadcasting für matrix2
matrix.cols = matrix1.cols; matrix.cols = matrix1.cols;
matrix.rows = matrix1.rows; matrix.rows = matrix1.rows;
matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols); matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols);
if (matrix.buffer == NULL) {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
for (int i = 0; i < matrix.rows; i++) { for (int i = 0; i < matrix.rows; i++) {
for (int j = 0; j < matrix.cols; j++){ for (int j = 0; j < matrix.cols; j++){
matrix.buffer[i * matrix.cols + j] = matrix1.buffer[i * matrix.cols + j]+matrix2.buffer[i*matrix2.cols]; matrix.buffer[i * matrix.cols + j] = matrix1.buffer[i * matrix.cols + j]+matrix2.buffer[i*matrix2.cols];
@ -108,12 +119,18 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
return matrix; return matrix;
} }
else else if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows)
{ {
//Falls beide Matrizen die gleichen Dimensionen haben
matrix.cols = matrix1.cols; matrix.cols = matrix1.cols;
matrix.rows = matrix1.rows; matrix.rows = matrix1.rows;
matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType));
if (matrix.buffer == NULL) {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
for (int i = 0; i < matrix1.rows; i++) { for (int i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix1.cols; j++) { for (int j = 0; j < matrix1.cols; j++) {
(matrix.buffer[i*matrix.cols+j])= (matrix1.buffer[i*matrix.cols+j]) + (matrix2.buffer[i*matrix2.cols+j]); (matrix.buffer[i*matrix.cols+j])= (matrix1.buffer[i*matrix.cols+j]) + (matrix2.buffer[i*matrix2.cols+j]);
@ -121,6 +138,12 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
} }
return matrix; return matrix;
} }
else {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
} }
@ -133,9 +156,15 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
matrix.cols = matrix2.cols; matrix.cols = matrix2.cols;
matrix.rows = matrix1.rows; matrix.rows = matrix1.rows;
matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType));
if (matrix.buffer == NULL) {
matrix.buffer=NULL;
matrix.cols = 0;
matrix.rows = 0;
return matrix;
}
for (int i = 0; i<matrix.rows; i++) { for (int i = 0; i<matrix.rows; i++) {
for (int j = 0; j<matrix.cols; j++) { for (int j = 0; j<matrix.cols; j++) {
matrix.buffer[i*matrix.cols+j] = 0; matrix.buffer[i*matrix.cols+j] = 0; //Füllen des Arrays mit Nullen, da im nächsten Schritt mit += gearbeitet wird.
} }
} }
for(int i = 0; i < matrix.rows; i++) for(int i = 0; i < matrix.rows; i++)
@ -144,6 +173,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
{ {
for(int k = 0; k < matrix1.cols; k++) for(int k = 0; k < matrix1.cols; k++)
{ {
//Multiplikation der Matrizen
matrix.buffer[i*matrix.cols+j] += matrix1.buffer[i*matrix1.cols+k] * matrix2.buffer[k*matrix2.cols+j]; matrix.buffer[i*matrix.cols+j] += matrix1.buffer[i*matrix1.cols+k] * matrix2.buffer[k*matrix2.cols+j];
} }
} }