forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
1
Commits
matrix
..
71b4017581
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71b4017581 |
+4
-94
@@ -6,107 +6,17 @@
|
|||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
//Datei öffnen, Header, Anzahl, Höhe und Breite lesen, geöffnete Datei zurückgeben
|
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||||
static FILE* openAndReadShort (const char *path, unsigned short *count, unsigned short *width, unsigned short *height) {
|
|
||||||
FILE *file = fopen(path, "rb");
|
|
||||||
if (!file) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
size_t headerLength = strlen(FILE_HEADER_STRING);
|
|
||||||
char *header = malloc (headerLength + 1);
|
|
||||||
if(!header) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fread(header, sizeof(char), headerLength, file) != headerLength) {
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||||
free (header);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
header[headerLength] = '\0';
|
|
||||||
|
|
||||||
if (strcmp (header, FILE_HEADER_STRING) != 0) {
|
|
||||||
free(header);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
free (header);
|
|
||||||
|
|
||||||
fread(count, sizeof(unsigned short), 1, file);
|
|
||||||
fread(width, sizeof(unsigned short), 1, file);
|
|
||||||
fread(height, sizeof(unsigned short), 1, file);
|
|
||||||
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Speicher anlegen und Pixel eines Bildes einlesen
|
|
||||||
static GrayScaleImage* readPixles (FILE *file, unsigned short *width, unsigned short *height) {
|
|
||||||
GrayScaleImage *image = malloc (sizeof(GrayScaleImage));
|
|
||||||
image->width = *width;
|
|
||||||
image->height = *height;
|
|
||||||
image->buffer = malloc ((*width) * (*height) * sizeof(GrayScalePixelType));
|
|
||||||
if (!image->buffer) {
|
|
||||||
free(image);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < (*width) * (*height); i++) {
|
|
||||||
unsigned char pixel;
|
|
||||||
if (fread(&pixel, sizeof(unsigned char), 1, file) != 1) {
|
|
||||||
free(image->buffer);
|
|
||||||
free(image);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
image->buffer[i] = pixel;
|
|
||||||
}
|
|
||||||
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Ausführen von openAndReadShort, Anlegen des Speichers für Bilderserie, readPixles wird für jedes Bild ausgeführt
|
|
||||||
//Nach jedem Bild wird das zugehörige Label gelesen, bei sämtlichen Fehlern wird NULL zurückgegeben und Speicher durch clearSeries bereinigt
|
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
unsigned short count = 0, width = 0, height = 0;
|
GrayScaleImageSeries *series = NULL;
|
||||||
|
|
||||||
FILE *file = openAndReadShort(path, &count, &width, &height);
|
|
||||||
if (file == 0) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
|
||||||
if (!series) {
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
series->count = count;
|
|
||||||
|
|
||||||
series->images = malloc(count * sizeof(GrayScaleImage));
|
|
||||||
series->labels = malloc(count* sizeof(unsigned char));
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < series->count; i++) {
|
|
||||||
GrayScaleImage *image = readPixles(file, &width, &height);
|
|
||||||
series->images[i] = *image;
|
|
||||||
free(image);
|
|
||||||
|
|
||||||
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1) {
|
|
||||||
clearSeries(series);
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
return series;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Bereinigt den Speicher
|
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < series->count; i++) {
|
|
||||||
free(series->images[i].buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(series->images);
|
|
||||||
free(series->labels);
|
|
||||||
free(series);
|
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
@@ -21,7 +20,6 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
if (mat.buffer == NULL)
|
if (mat.buffer == NULL)
|
||||||
{
|
{
|
||||||
clearMatrix(&mat);
|
clearMatrix(&mat);
|
||||||
perror("could not allocate memory");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mat;
|
return mat;
|
||||||
@@ -62,11 +60,6 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
{
|
{
|
||||||
Matrix resMat = (matrix1.cols > matrix2.cols) ? createMatrix(matrix1.rows, matrix1.cols) : createMatrix(matrix2.rows, matrix2.cols);
|
Matrix resMat = (matrix1.cols > matrix2.cols) ? createMatrix(matrix1.rows, matrix1.cols) : createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
|
||||||
if (resMat.buffer == NULL)
|
|
||||||
{
|
|
||||||
return createMatrix(0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matrix1.cols != matrix2.cols)
|
if (matrix1.cols != matrix2.cols)
|
||||||
{
|
{
|
||||||
if (matrix1.rows != matrix2.rows)
|
if (matrix1.rows != matrix2.rows)
|
||||||
@@ -117,6 +110,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
return resMat;
|
return resMat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO implement
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
if (matrix1.cols != matrix2.rows || matrix1.buffer == NULL || matrix2.buffer == NULL)
|
if (matrix1.cols != matrix2.rows || matrix1.buffer == NULL || matrix2.buffer == NULL)
|
||||||
@@ -127,11 +121,6 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
int rows = matrix1.rows, cols = matrix2.cols;
|
int rows = matrix1.rows, cols = matrix2.cols;
|
||||||
Matrix resMat = createMatrix(rows, cols);
|
Matrix resMat = createMatrix(rows, cols);
|
||||||
|
|
||||||
if (resMat.buffer == NULL)
|
|
||||||
{
|
|
||||||
return createMatrix(0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t rowIdx = 0; rowIdx < rows; rowIdx++)
|
for (size_t rowIdx = 0; rowIdx < rows; rowIdx++)
|
||||||
{
|
{
|
||||||
for (size_t colIdx = 0; colIdx < cols; colIdx++)
|
for (size_t colIdx = 0; colIdx < cols; colIdx++)
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ typedef float MatrixType;
|
|||||||
// Matrixtyp
|
// Matrixtyp
|
||||||
typedef struct Matrix
|
typedef struct Matrix
|
||||||
{
|
{
|
||||||
MatrixType *buffer;
|
|
||||||
size_t rows;
|
size_t rows;
|
||||||
size_t cols;
|
size_t cols;
|
||||||
|
MatrixType *buffer;
|
||||||
|
|
||||||
} Matrix;
|
} Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
some_tag
|
|
||||||
Reference in New Issue
Block a user