generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5171475646 | ||
|
|
03d707108a | ||
|
|
6d9aa3709a |
No files matched your search
+124
-2
@@ -6,17 +6,139 @@
|
|||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||||
|
static int checkFileHeader(FILE *file)
|
||||||
|
{
|
||||||
|
char buffer[BUFFER_SIZE] = {0};
|
||||||
|
|
||||||
|
size_t length = strlen(FILE_HEADER_STRING);
|
||||||
|
|
||||||
|
if (length >= BUFFER_SIZE)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fread(buffer, 1, length, file) != length)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return strcmp(buffer, FILE_HEADER_STRING) == 0;
|
||||||
|
}
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
GrayScaleImageSeries *series = NULL;
|
|
||||||
|
|
||||||
return series;
|
FILE *file = fopen(path, "rb");
|
||||||
|
|
||||||
|
if (file == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (!checkFileHeader(file))
|
||||||
|
{
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
unsigned short numberOfImages;
|
||||||
|
unsigned short width;
|
||||||
|
unsigned short height;
|
||||||
|
if (fread(&numberOfImages, sizeof(unsigned short), 1, file) != 1 ||
|
||||||
|
fread(&width, sizeof(unsigned short), 1, file) != 1 ||
|
||||||
|
fread(&height, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
{
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (numberOfImages == 0 || width == 0 || height == 0)
|
||||||
|
{
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
GrayScaleImageSeries *series =
|
||||||
|
calloc(1, sizeof(GrayScaleImageSeries));
|
||||||
|
|
||||||
|
|
||||||
|
if (series == NULL)
|
||||||
|
{
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
series->count = numberOfImages;
|
||||||
|
series->images =
|
||||||
|
calloc(numberOfImages, sizeof(GrayScaleImage));
|
||||||
|
|
||||||
|
series->labels =
|
||||||
|
calloc(numberOfImages, sizeof(unsigned char));
|
||||||
|
if (series->images == NULL || series->labels == NULL)
|
||||||
|
{
|
||||||
|
clearSeries(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < numberOfImages; i++)
|
||||||
|
{
|
||||||
|
GrayScaleImage *image = &series->images[i];
|
||||||
|
|
||||||
|
image->width = width;
|
||||||
|
image->height = height;
|
||||||
|
|
||||||
|
image->buffer = malloc(
|
||||||
|
(size_t)width * height * sizeof(GrayScalePixelType)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (image->buffer == NULL)
|
||||||
|
{
|
||||||
|
clearSeries(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fread(image->buffer,
|
||||||
|
sizeof(GrayScalePixelType),
|
||||||
|
(size_t)width * height,
|
||||||
|
file) != (size_t)width * height)
|
||||||
|
{
|
||||||
|
clearSeries(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fread(&series->labels[i],
|
||||||
|
sizeof(unsigned char),
|
||||||
|
1,
|
||||||
|
file) != 1)
|
||||||
|
{
|
||||||
|
clearSeries(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(file);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (series->images != NULL)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < series->count; i++)
|
||||||
|
{
|
||||||
|
free(series->images[i].buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
}
|
}
|
||||||
@@ -44,24 +44,92 @@ void clearMatrix(Matrix *matrix)
|
|||||||
matrix->cols = 0;
|
matrix->cols = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
matrix.buffer[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)
|
||||||
{
|
{
|
||||||
|
if (matrix.buffer == NULL ||
|
||||||
|
rowIdx >= matrix.rows ||
|
||||||
|
colIdx >= matrix.cols) {
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix.buffer[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 &&
|
||||||
|
matrix1.cols != 1 &&
|
||||||
|
matrix2.cols != 1)) {
|
||||||
|
return createMatrix(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ungültige Eingabematrizen abfangen
|
||||||
|
if (matrix1.buffer == NULL || matrix2.buffer == NULL) {
|
||||||
|
return createMatrix(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Größere Spaltenanzahl bestimmen
|
||||||
|
unsigned int cols = matrix1.cols > matrix2.cols
|
||||||
|
? matrix1.cols : matrix2.cols;
|
||||||
|
|
||||||
|
Matrix result = createMatrix(matrix1.rows, cols);
|
||||||
|
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < result.rows; i++) {
|
||||||
|
for (unsigned int j = 0; j < result.cols; j++) {
|
||||||
|
|
||||||
|
// Bei einer Spalte immer Spalte 0 verwenden
|
||||||
|
unsigned int j1 = matrix1.cols == 1 ? 0 : j;
|
||||||
|
unsigned int j2 = matrix2.cols == 1 ? 0 : j;
|
||||||
|
|
||||||
|
result.buffer[i * result.cols + j] =
|
||||||
|
matrix1.buffer[i * matrix1.cols + j1] +
|
||||||
|
matrix2.buffer[i * matrix2.cols + j2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
if (matrix1.cols != matrix2.rows) {
|
||||||
|
return createMatrix(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ergebnismatrix erstellen
|
||||||
|
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
|
||||||
|
// Matrizen multiplizieren
|
||||||
|
for (unsigned int rowIdx = 0; rowIdx < matrix1.rows; rowIdx++) {
|
||||||
|
for (unsigned int colIdx = 0; colIdx < matrix2.cols; colIdx++) {
|
||||||
|
for (unsigned int k = 0; k < matrix1.cols; k++) {
|
||||||
|
result.buffer[rowIdx * result.cols + colIdx] +=
|
||||||
|
matrix1.buffer[rowIdx * matrix1.cols + k] *
|
||||||
|
matrix2.buffer[k * matrix2.cols + colIdx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user