aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian C <cc@localhost>2025-03-04 23:23:26 -0800
committerChristian C <cc@localhost>2025-03-04 23:23:26 -0800
commit2ce30c1873467859305c3ab4f88cc0d3ea7cb082 (patch)
tree11982d79e25be3678ce3fa9185b2caf0d55bf984
parent076651de2ada69bd31cc246ffe899afdc8e4cb13 (diff)
Dilate masks
-rw-r--r--src/main.c81
1 files changed, 66 insertions, 15 deletions
diff --git a/src/main.c b/src/main.c
index 804f58b..9075dcb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,10 +14,9 @@
#include <lib/color.h>
#include <lib/algo/flood_fill.h>
-#define TAG 50839
#define OFFSET 16
-#define ONE_ROW 1
+// Get full path
char* full_path(char* dir, char* file)
{
char* fpath = NULL;
@@ -30,6 +29,7 @@ char* full_path(char* dir, char* file)
return fpath;
}
+// Determines if file name has tif file extension
bool_t is_tif_ext(char* file_name)
{
size_t file_len = strlen(file_name);
@@ -39,7 +39,61 @@ bool_t is_tif_ext(char* file_name)
return FALSE;
}
-uint16_t* combine_masks(uint16_t *destination, uint16_t *extra_labels, size_t width, size_t height)
+// Convert x,y coords to linear coordinate
+size_t xy_to_coord(size_t x, size_t y, uint32_t width, uint32_t height)
+{
+ return x + y*width;
+}
+
+// Dilate masks by one 4-connected pixel
+uint16_t* dilate(uint16_t* mask, uint32_t width, uint32_t height)
+{
+ uint16_t *new_mask = (uint16_t*)calloc(width*height,sizeof(uint16_t));
+ for (size_t y = 0; y < height; y++) {
+ for (size_t x = 0; x < width; x++) {
+ size_t current_position = xy_to_coord(x, y, width, height);
+ if (mask[current_position] != 0) {
+ new_mask[current_position] = mask[current_position];
+ continue;
+ }
+ size_t proposed_position;
+ if (x != 0) {
+ proposed_position = xy_to_coord(x-1, y, width, height);
+ if (mask[proposed_position] != 0) {
+ new_mask[current_position] = mask[proposed_position];
+ continue;
+ }
+ }
+ if ((x+1) != width) {
+ proposed_position = xy_to_coord(x+1, y, width, height);
+ if (mask[proposed_position] != 0) {
+ new_mask[current_position] = mask[proposed_position];
+ continue;
+ }
+ }
+ if (y != 0) {
+ proposed_position = xy_to_coord(x, y-1, width, height);
+ if (mask[proposed_position] != 0) {
+ new_mask[current_position] = mask[proposed_position];
+ continue;
+ }
+ }
+ if ((y+1) != height) {
+ proposed_position = xy_to_coord(x, y+1, width, height);
+ if (mask[proposed_position] != 0) {
+ new_mask[current_position] = mask[proposed_position];
+ continue;
+ }
+ }
+ }
+ }
+ return new_mask;
+}
+
+// Combine Label Masks
+// For all empty spaces in the destination, put the extra label if it exists
+// Allocates an array if destination is unallocated
+uint16_t* combine_masks(uint16_t *destination, uint16_t *extra_labels, uint32_t width, uint32_t height)
{
if (destination == NULL) {
destination = (uint16_t*)calloc(width*height, sizeof(uint16_t));
@@ -55,25 +109,23 @@ uint16_t* combine_masks(uint16_t *destination, uint16_t *extra_labels, size_t wi
return destination;
}
+// Process Tif File to Labels
+// width, height will be overwritten with image dimensions
+// starting_label_p will be incremented for each label found in the image
uint16_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, uint16_t *starting_label_p)
{
- //-----------------------------------------------
//-TIFF-IMAGE-OPEN-------------------------------
- //-----------------------------------------------
TIFF *tif = TIFFOpen(tif_file_name, "r");
if (!tif) {
fprintf(stderr, "Failed to open TIFF file\n");
return NULL;
}
- //-----------------------------------------------
- //-----------------------------------------------
//-TIFF-FIND-DIMENSIONS--------------------------
- //-----------------------------------------------
size_t channels = 1;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, height);
- //-----------------------------------------------
+
tmsize_t STRIP_LENGTH = TIFFStripSize(tif);
tmsize_t STRIP_COUNT = TIFFNumberOfStrips(tif);
if ((*width)*(*height)*3 == STRIP_LENGTH*STRIP_COUNT) {
@@ -81,11 +133,8 @@ uint16_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height,
} else if ((*width)*(*height)*4 == STRIP_LENGTH*STRIP_COUNT) {
channels = 4;
}
- //-----------------------------------------------
- //-----------------------------------------------
//-TIFF-LOAD-DATA--------------------------------
- //-----------------------------------------------
void* buffer = malloc(STRIP_LENGTH*sizeof(uint8_t));
if (buffer == NULL) {
fprintf(stderr, "Memory allocation error\n");
@@ -107,12 +156,9 @@ uint16_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height,
}
}
free(buffer);
- //-----------------------------------------------
- //-----------------------------------------------
//-FLOOD-FILL-SEGMENTATION-----------------------
//-CONTIGUOUS-REGION-FINDING---------------------
- //-----------------------------------------------
uint16_t *labels = NULL;
labels = (uint16_t*)calloc((*width)*(*height),sizeof(uint16_t));
if (labels == NULL) {
@@ -175,6 +221,11 @@ int main(int argc, char** argv)
}
}
printf("%u %u\n", width, height);
+ for (uint16_t count = 0; count < 3; count++) {
+ uint16_t *new_labels = dilate(masks, width, height);
+ free(masks);
+ masks = new_labels;
+ }
//free(masks);
//-----------------------------------------------