summaryrefslogtreecommitdiff
path: root/OASIS/c/src/ekd_cv.c
diff options
context:
space:
mode:
Diffstat (limited to 'OASIS/c/src/ekd_cv.c')
-rw-r--r--OASIS/c/src/ekd_cv.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/OASIS/c/src/ekd_cv.c b/OASIS/c/src/ekd_cv.c
new file mode 100644
index 0000000..1f9afad
--- /dev/null
+++ b/OASIS/c/src/ekd_cv.c
@@ -0,0 +1,90 @@
+#include "ekd_cv.h"
+
+void read_bmp(char* filename, uint8_t** data) {
+ bmp_header_t header;
+ bmp_info_t info;
+
+ FILE* file = fopen(filename, "rb");
+ if (file == NULL) {
+ printf("Unable to open file.\n");
+ return;
+ }
+
+ fread(&header.signature, sizeof(uint16_t), 1, file);
+ fread(&header.file_size, sizeof(uint32_t), 1, file);
+ fread(&header.reserved, sizeof(uint32_t), 1, file);
+ fread(&header.data_offset, sizeof(uint32_t), 1, file);
+
+ fread(&info.size, sizeof(uint32_t), 1, file);
+ fread(&info.width, sizeof(uint32_t), 1, file);
+ fread(&info.height, sizeof(uint32_t), 1, file);
+ fread(&info.planes, sizeof(uint16_t), 1, file);
+ fread(&info.bit_depth, sizeof(uint16_t), 1, file);
+ fread(&info.compression, sizeof(uint32_t), 1, file);
+ fread(&info.image_size, sizeof(uint32_t), 1, file);
+ fread(&info.x_pixels_per_meter, sizeof(uint32_t), 1, file);
+ fread(&info.y_pixels_per_meter, sizeof(uint32_t), 1, file);
+ fread(&info.colours_used, sizeof(uint32_t), 1, file);
+ fread(&info.important_colours, sizeof(uint32_t), 1, file);
+
+ uint8_t* raw_data = (uint8_t*) malloc(info.width * info.height * sizeof(uint8_t));
+
+ fseek(file, header.data_offset, SEEK_SET);
+ fread(raw_data, sizeof(uint8_t), info.width * info.height, file);
+
+ fclose(file);
+
+ for (uint8_t i=0; i<info.height; i++) {
+ for (uint8_t j=0; j < info.width; j++) data[i][j] = raw_data[(i * info.width) + j];
+ }
+
+ free(raw_data);
+}
+
+void read_dir(char* dneg_path, char* dpos_path, char** file_name_arr) {
+ DIR* dneg_dir = opendir(dneg_path);
+ DIR* dpos_dir = opendir(dpos_path);
+ struct dirent *entry;
+ uint8_t item_id = 0;
+
+ while ((entry = readdir(dneg_dir)) != NULL) {
+ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
+ continue;
+ }
+ strcat(file_name_arr[item_id], entry->d_name);
+ item_id++;
+ }
+
+ while ((entry = readdir(dpos_dir)) != NULL) {
+ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
+ continue;
+ }
+ strcat(file_name_arr[item_id], entry->d_name);
+ item_id++;
+ }
+
+ closedir(dneg_dir);
+ closedir(dpos_dir);
+}
+
+void flatten_image(uint8_t** image, float* image_flattened) {
+ for (uint8_t i=0; i<IMAGE_HEIGHT; i++) {
+ for (uint8_t j=0; j<IMAGE_WIDTH; j++) image_flattened[(i*IMAGE_WIDTH)+j] = (float)image[i][j];
+ }
+}
+
+void shuffle_data_labels(float** data, int32_t* labels, uint8_t num_images) {
+ srand(time(NULL)); // initialize random seed
+ for (int i = num_images - 1; i > 0; i--) {
+ // Generate a random index between 0 and i (inclusive)
+ int j = rand() % (i + 1);
+
+ // Swap the rows of data_flatten and labels at indices i and j
+ float* temp_data = data[i];
+ data[i] = data[j];
+ data[j] = temp_data;
+ int32_t temp_label = labels[i];
+ labels[i] = labels[j];
+ labels[j] = temp_label;
+ }
+} \ No newline at end of file