Compare commits
30
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c772e34cfd | ||
|
|
e75d2956a9 | ||
|
|
0397890940 | ||
|
|
d3a79ff7bc | ||
|
|
86360718ea | ||
|
|
d73f9ec750 | ||
|
|
caaf6b402f | ||
|
|
9b4f9c0e70 | ||
|
|
689ae7bd56 | ||
|
|
6bcf7fa1fb | ||
|
|
b411ffa6c1 | ||
|
|
c1086b6be5 | ||
|
|
542d94eac8 | ||
|
|
d5466ae1fd | ||
|
|
b7de923066 | ||
|
|
09ada4e4d0 | ||
|
|
4aee809756 | ||
|
|
b661866a16 | ||
|
|
5b32e1475c | ||
|
|
c96311347e | ||
|
|
f3b964f17e | ||
|
|
782bcdc350 | ||
|
|
a1c582fcff | ||
|
|
bf27920a61 | ||
|
|
6885e104eb | ||
|
|
d15f3d1b9c | ||
|
|
84fb7219fb | ||
|
|
e2dbb0952f | ||
|
|
3a72794684 | ||
|
|
f0bc0ce06a |
+206
@@ -5,14 +5,220 @@
|
|||||||
|
|
||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
#define HEADER_LEN (sizeof(FILE_HEADER_STRING) - 1) //-1, um \0 Character rauszufiltern
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
|
static int checkDimensions(unsigned int width, unsigned int height){
|
||||||
|
|
||||||
|
int ok = 0;
|
||||||
|
|
||||||
|
if(width > 0 && height > 0)
|
||||||
|
return ok = 1;
|
||||||
|
|
||||||
|
else
|
||||||
|
return ok = 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int checkHeader(const GrayScalePixelType *buffer){
|
||||||
|
|
||||||
|
int ok = 0;
|
||||||
|
char header[HEADER_LEN + 1]; //Array mit Platz für Header Länge + \0 Character -> String
|
||||||
|
|
||||||
|
// Jeder Byte in String kopieren
|
||||||
|
for(int i = 0; i < HEADER_LEN; i++){
|
||||||
|
|
||||||
|
header[i] = buffer[i];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
header[HEADER_LEN] = '\0'; // Nullterminator am Ende anhängen
|
||||||
|
|
||||||
|
if(strcmp(header, FILE_HEADER_STRING) == 0) // wenn beide identische sind gibt cmp 0 zurück
|
||||||
|
return ok = 1;
|
||||||
|
|
||||||
|
else
|
||||||
|
return ok = 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static FILE *openImageFile(const char *path)
|
||||||
|
{
|
||||||
|
return fopen(path, "rb");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ReadCheckHeader(FILE *fp, GrayScalePixelType *header)
|
||||||
|
{
|
||||||
|
if(fread(header, sizeof(GrayScalePixelType), HEADER_LEN, fp) != HEADER_LEN)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int readMetaData(FILE *fp, unsigned int *numImages, unsigned int *width, unsigned int *height)
|
||||||
|
{
|
||||||
|
if(fread(numImages, sizeof(unsigned int), 1, fp) != 1 || fread(width, sizeof(unsigned int), 1, fp) != 1 || fread(height, sizeof(unsigned int), 1, fp)!= 1)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static GrayScaleImageSeries *allocateSeriesStruct(void)
|
||||||
|
{
|
||||||
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||||
|
return series;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int AllocateCheckImageAndLabels(GrayScaleImageSeries *series, unsigned int numImages)
|
||||||
|
{
|
||||||
|
series->count = numImages;
|
||||||
|
//Speicher für die Pointer an sich belegen
|
||||||
|
series->images = malloc(numImages * sizeof(GrayScaleImage)); //Speicher für alle Bild-Strukturen, nicht aber für die Pixel selbst (denn image ist nochmal ein Array selbst)
|
||||||
|
series->labels = malloc(numImages * sizeof(unsigned char)); //Speicher für alle Labels
|
||||||
|
|
||||||
|
//Prüfen, ob Speicher für die Arrays reserviert werden konnte, wenn nicht -> Freigabe
|
||||||
|
if(series->images == NULL || series->labels == NULL){
|
||||||
|
|
||||||
|
free(series->images); //wenn trotzdem was reingeschrieben wurde, dann freigeben
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int AllocateCheckPixelData(GrayScaleImageSeries *series, unsigned int numImages, unsigned int width, unsigned int height)
|
||||||
|
{
|
||||||
|
for(unsigned int i = 0; i < numImages; i++){
|
||||||
|
|
||||||
|
series -> images[i].buffer = malloc(width * height); //Speicher für Pixeldaten selbst
|
||||||
|
|
||||||
|
if(series->images[i].buffer == NULL){
|
||||||
|
|
||||||
|
for(unsigned int j = 0; j < i; j++){
|
||||||
|
free(series->images[j].buffer); //wenn Speicher nicht reserviert werden konnte, dann zuerst das Array mit Pixeldaten freigeben
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
free(series->images); //anschließend auch die Speicher für Bildstrukturen und Label-Arrays freigeben, weil zuvor für diese schon Speicher reserviert wurde
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ReadImagesAndLabels(FILE *fp, GrayScaleImageSeries *series, unsigned int numImages, unsigned int width, unsigned int height)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < numImages; i++)
|
||||||
|
{
|
||||||
|
//Breite und Höhe werden gesetzt (GrayScaleImage)
|
||||||
|
series -> images[i].width = width;
|
||||||
|
series -> images[i].height = height;
|
||||||
|
|
||||||
|
|
||||||
|
//Pixeldaten einlesen und Prüfen ob alle essentiellen Pixel eingelesen wurden
|
||||||
|
if(fread(series -> images[i].buffer, 1, width * height, fp) != (width * height))
|
||||||
|
{
|
||||||
|
clearSeries(series);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Jedes Bild hat ein Label, was direkt danach eingelesen wird und auf Größe geprüft wird
|
||||||
|
if(fread(&series -> labels[i], 1, 1, fp) != 1){
|
||||||
|
clearSeries(series);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 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;
|
||||||
|
|
||||||
|
//Datei im Binärmodus öffnen
|
||||||
|
FILE *fp = openImageFile(path);
|
||||||
|
if(!fp)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
//Buffer zum Einlesen des Headers
|
||||||
|
GrayScalePixelType header[HEADER_LEN];
|
||||||
|
|
||||||
|
//Prüfen, ob genau die HEADER_LEN Bytes eingelesen werden
|
||||||
|
if(!ReadCheckHeader(fp, header)){
|
||||||
|
fclose(fp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Prüfen, ob es sich um den korrekten Header handelt mit Hilfsfunktion
|
||||||
|
if(!checkHeader(header)){
|
||||||
|
fclose(fp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Anzahl der Bilder, Breite und Höhe einlesen
|
||||||
|
unsigned int numImages, width, height; //muss man das als Zeiger initialisieren?????????
|
||||||
|
if(!readMetaData(fp, &numImages, &width, &height)){
|
||||||
|
fclose(fp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Prüfen, ob gültige Länge oder Breite
|
||||||
|
if(!checkDimensions(width, height)){
|
||||||
|
fclose(fp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Speicher für die gesamte Bildserie reservieren (aber nur für die Struktur)
|
||||||
|
series = allocateSeriesStruct();
|
||||||
|
if(!series) {
|
||||||
|
fclose(fp);
|
||||||
|
return NULL; //Fehler bei Speicherreservierung
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!AllocateCheckImageAndLabels(series, numImages)){
|
||||||
|
fclose(fp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Speicher für die Pixeldaten selbst reserviern und Prüfen, ob er reserviert werden konnte
|
||||||
|
if(!AllocateCheckPixelData(series, numImages, width, height)){
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(!ReadImagesAndLabels(fp, series, numImages, width, height)){
|
||||||
|
fclose(fp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +1,265 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
|
enum addModes{SAMEDIMENSIONS, COLVEC, ROWVEC};
|
||||||
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
Matrix m;
|
||||||
|
m.rows = rows;
|
||||||
|
m.cols = cols;
|
||||||
|
m.buffer = NULL;
|
||||||
|
|
||||||
|
if(rows == 0 || cols == 0)
|
||||||
|
{
|
||||||
|
m.rows = 0;
|
||||||
|
m.cols = 0;
|
||||||
|
m.buffer = NULL;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
m.buffer = calloc(rows * cols, sizeof(MatrixType));
|
||||||
|
|
||||||
|
if (m.buffer == NULL)
|
||||||
|
{
|
||||||
|
m.rows = 0;
|
||||||
|
m.cols = 0;
|
||||||
|
m.buffer = NULL;
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if (matrix->buffer == NULL)
|
||||||
|
{
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
matrix->buffer = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
free(matrix->buffer);
|
||||||
|
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
matrix->buffer = NULL;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
matrix.data[(rowIdx * matrix.cols) + colIdx] = value; //wir setzen den data-Wert an der Stelle (rowIdx*Spalten + colIdx) auf den Wert von value
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
matrix.buffer[(rowIdx * matrix.cols) + colIdx] = value; //wir setzen den data-Wert an der Stelle (rowIdx*Spalten + colIdx) auf den Wert von value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
|
{
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix.buffer[(rowIdx * matrix.cols) + colIdx];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int get_add_mode(Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
|
int get_add_mode = -1;
|
||||||
|
|
||||||
|
if(matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows)
|
||||||
|
{
|
||||||
|
get_add_mode = SAMEDIMENSIONS;
|
||||||
|
}
|
||||||
|
else if(matrix1.cols == 1 && matrix1.rows == matrix2.rows)
|
||||||
|
{
|
||||||
|
get_add_mode = COLVEC;
|
||||||
|
}
|
||||||
|
else if(matrix2.cols == 1 && matrix1.rows == matrix2.rows)
|
||||||
|
{
|
||||||
|
get_add_mode = COLVEC;
|
||||||
|
}
|
||||||
|
else if(matrix1.rows == 1 && matrix1.cols == matrix2.cols)
|
||||||
|
{
|
||||||
|
get_add_mode = ROWVEC;
|
||||||
|
}
|
||||||
|
else if(matrix2.rows == 1 && matrix1.cols == matrix2.cols)
|
||||||
|
{
|
||||||
|
get_add_mode = ROWVEC;
|
||||||
|
}
|
||||||
|
|
||||||
|
return get_add_mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Matrix addSameDim(Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
|
Matrix matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < (matrix1.rows * matrix1.cols); i++)
|
||||||
|
{
|
||||||
|
matrix_erg.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix_erg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Matrix addColVec(Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
|
Matrix matrix_erg;
|
||||||
|
|
||||||
|
if(matrix1.cols == 1)
|
||||||
|
{
|
||||||
|
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < matrix2.rows; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix2.cols + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(matrix2.cols == 1)
|
||||||
|
{
|
||||||
|
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < matrix1.cols; j++)
|
||||||
|
{
|
||||||
|
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix_erg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Matrix addRowVec(Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
|
Matrix matrix_erg;
|
||||||
|
|
||||||
|
if(matrix1.rows == 1)
|
||||||
|
{
|
||||||
|
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < matrix2.rows; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[j] + matrix2.buffer[i * matrix2.cols + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(matrix2.rows == 1)
|
||||||
|
{
|
||||||
|
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < matrix1.cols; j++)
|
||||||
|
{
|
||||||
|
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix_erg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
int ok = get_add_mode(matrix1,matrix2);
|
||||||
|
Matrix matrix_erg = createMatrix(0, 0);
|
||||||
|
|
||||||
|
switch(ok)
|
||||||
|
{
|
||||||
|
case SAMEDIMENSIONS:
|
||||||
|
matrix_erg = addSameDim(matrix1, matrix2);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case COLVEC:
|
||||||
|
matrix_erg = addColVec(matrix1, matrix2);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ROWVEC:
|
||||||
|
matrix_erg = addRowVec(matrix1, matrix2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix_erg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int can_multiply (Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
|
int can_multiply = 0;
|
||||||
|
|
||||||
|
if(matrix1.cols == matrix2.rows)
|
||||||
|
{
|
||||||
|
can_multiply = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return can_multiply;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
int ok = can_multiply(matrix1,matrix2);
|
||||||
|
unsigned int erg_rows = matrix1.rows;
|
||||||
|
unsigned int erg_cols = matrix2.cols;
|
||||||
|
Matrix matrix_erg = createMatrix(0, 0);
|
||||||
|
|
||||||
|
if (ok == 1)
|
||||||
|
{
|
||||||
|
matrix_erg = createMatrix(erg_rows, erg_cols);
|
||||||
|
|
||||||
|
for (int i = 0; i < erg_rows; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < erg_cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType sum = 0;
|
||||||
|
|
||||||
|
for (int k = 0; k < matrix1.cols; k++)
|
||||||
|
{
|
||||||
|
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
setMatrixAt(sum, matrix_erg, i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix_erg;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ typedef struct
|
|||||||
{
|
{
|
||||||
unsigned int rows;
|
unsigned int rows;
|
||||||
unsigned int cols;
|
unsigned int cols;
|
||||||
MatrixType *data;
|
MatrixType *buffer;
|
||||||
}Matrix;
|
}Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
|
|||||||
Reference in New Issue
Block a user