Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9dec06909 | ||
|
|
4893304ccd |
+54
-3
@@ -11,12 +11,63 @@
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -4,119 +4,81 @@
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix m;
|
||||
Matrix matrix;
|
||||
matrix.rows = rows;
|
||||
matrix.cols = cols;
|
||||
if(rows == 0 || cols == 0){
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
m.buffer = NULL;
|
||||
return m;
|
||||
matrix.werte = NULL;
|
||||
return matrix;
|
||||
|
||||
}
|
||||
m.rows = rows;
|
||||
m.cols = cols;
|
||||
m.buffer = calloc(rows * cols, sizeof(MatrixType)); // matrix flach angelegt a11 a12 a13 a21 a22...
|
||||
if(m.buffer == NULL){ // speicherreservierung fehlgeschlagen
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
}
|
||||
return m;
|
||||
matrix.werte = malloc(rows * cols * sizeof(MatrixType));
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
|
||||
free(matrix->buffer);
|
||||
|
||||
|
||||
matrix->buffer = NULL;
|
||||
//speicher freimachen:
|
||||
free(matrix->werte);
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
// genau einen wert in der matrix setzen
|
||||
int idx = rowIdx * matrix.cols + colIdx; // flache matrix betrachten
|
||||
matrix.buffer[idx] = value;
|
||||
matrix.werte[rowIdx * matrix.cols + colIdx] = value;
|
||||
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
// einen wert in der matrix returnen
|
||||
if(rowIdx >= matrix.rows || colIdx >= matrix.cols || matrix.buffer == NULL){
|
||||
return(MatrixType)0;
|
||||
}
|
||||
int idx = rowIdx * matrix.cols + colIdx;
|
||||
return matrix.buffer[idx];
|
||||
|
||||
return matrix.werte[rowIdx * matrix.cols + colIdx];
|
||||
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
// elementweise addition (beide matrizen selbe größe)
|
||||
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
|
||||
Matrix resultmatrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for(int i = 0; i < (matrix1.rows * matrix1.cols); i++){
|
||||
resultmatrix.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
||||
}
|
||||
return resultmatrix;
|
||||
}
|
||||
// broadcasting
|
||||
// A Matrix B Vektor
|
||||
if(matrix1.rows == matrix2.rows && matrix2.cols == 1){
|
||||
Matrix resultmatrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||
// TODO
|
||||
for(int rows = 0; rows < matrix1.rows; rows++){
|
||||
for(int cols = 0; cols < matrix1.cols; cols++){
|
||||
MatrixType wertm1 = getMatrixAt(matrix1, rows, cols);
|
||||
MatrixType wertm2 = getMatrixAt(matrix2, rows, 0);
|
||||
setMatrixAt(wertm1 + wertm2, resultmatrix, rows, cols);
|
||||
}
|
||||
}
|
||||
return resultmatrix;
|
||||
}
|
||||
if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols){
|
||||
//nicht sicher
|
||||
|
||||
|
||||
// A Vektor B Matrix
|
||||
if(matrix1.rows == matrix2.rows && matrix1.cols == 1){
|
||||
Matrix resultmatrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||
// TODO
|
||||
for(int rows = 0; rows < matrix1.rows; rows++){
|
||||
for(int cols = 0; cols < matrix2.cols; cols++){
|
||||
MatrixType wertm1 = getMatrixAt(matrix1, rows, 0);
|
||||
MatrixType wertm2 = getMatrixAt(matrix2, rows, cols);
|
||||
setMatrixAt(wertm1 + wertm2, resultmatrix, rows, cols);
|
||||
}
|
||||
}
|
||||
return resultmatrix;
|
||||
}
|
||||
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 undef;
|
||||
undef.rows = 0;
|
||||
undef.cols = 0;
|
||||
undef.buffer = NULL;
|
||||
return undef;
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
// bedingung für A*B: A.cols = B.rows
|
||||
if(matrix1.cols == matrix2.rows){
|
||||
Matrix resultmatrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||
for(int i = 0; i < matrix1.rows; i++){
|
||||
for(int j = 0; j < matrix2.cols; j++){
|
||||
MatrixType summe = 0;
|
||||
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);
|
||||
|
||||
|
||||
|
||||
for(int l = 0; l <matrix1.cols; l++){
|
||||
summe += getMatrixAt(matrix1, i, l) * getMatrixAt(matrix2, l, j);
|
||||
}
|
||||
setMatrixAt(summe, resultmatrix, i, j);
|
||||
}
|
||||
setMatrixAt(summe, ergebnisMatrix, i, j);
|
||||
|
||||
|
||||
}
|
||||
return resultmatrix;
|
||||
|
||||
|
||||
}
|
||||
Matrix undef;
|
||||
undef.rows = 0;
|
||||
undef.cols = 0;
|
||||
undef.buffer = NULL;
|
||||
return undef;
|
||||
return ergebnisMatrix;
|
||||
|
||||
}
|
||||
@@ -6,13 +6,14 @@
|
||||
typedef float MatrixType;
|
||||
|
||||
// TODO Matrixtyp definieren
|
||||
typedef struct{
|
||||
typedef struct {
|
||||
unsigned int rows;
|
||||
unsigned int cols;
|
||||
MatrixType *buffer;
|
||||
}Matrix;
|
||||
float *werte;
|
||||
|
||||
|
||||
} Matrix;
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||
void clearMatrix(Matrix *matrix);
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
|
||||
|
||||
Binary file not shown.
@@ -1,20 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>English</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.apple.xcode.dsym.runMatrixTests</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>dSYM</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
</dict>
|
||||
</plist>
|
||||
Binary file not shown.
@@ -1,5 +0,0 @@
|
||||
---
|
||||
triple: 'arm64-apple-darwin'
|
||||
binary-path: runMatrixTests
|
||||
relocations: []
|
||||
...
|
||||
Reference in New Issue
Block a user