Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sliu008 committed Nov 20, 2024
1 parent 8d47f5d commit 6069e74
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 31 deletions.
31 changes: 3 additions & 28 deletions podaac/forge_py/strategies/open_cv_footprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,6 @@ def process_multipolygons(contours, hierarchy, width, height):
return None


def process_mask(image, fill_kernel=(20, 20)):
"""
Applies morphological closing to an image mask to fill small gaps, enhancing contiguous regions.
Parameters:
- image (ndarray): The input binary mask image to be processed.
- fill_kernel (array or tuple of int, optional): The size of the structuring element (kernel) used for morphological closing.
Default is (20, 20).
Returns:
- ndarray: The processed binary image with small gaps filled.
Notes:
- Morphological closing is performed using a rectangular structuring element, which fills small gaps and
helps create smoother, more continuous regions in binary masks.
"""
kernel = np.ones(fill_kernel, np.uint8)
return cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)


def convert_to_image_coords(lon, lat, image_width=3600, image_height=1800):
"""
Converts geographic coordinates (longitude, latitude) to pixel coordinates in an image.
Expand Down Expand Up @@ -231,7 +211,7 @@ def convert_to_image_coords(lon, lat, image_width=3600, image_height=1800):
return img_x, img_y


def write_image(filename, lat, lon, image_width=3600, image_height=1800, fill_kernel=(20, 20)):
def write_image(filename, lat, lon, image_width=3600, image_height=1800):
"""
Creates an image from geographic coordinates, processes it to fill gaps, and saves it as a PNG file.
Expand All @@ -241,8 +221,6 @@ def write_image(filename, lat, lon, image_width=3600, image_height=1800, fill_ke
- lon (float or ndarray): Longitude values of points to plot, either as a single value or an array.
- image_width (int, optional): Width of the image in pixels. Default is 3600.
- image_height (int, optional): Height of the image in pixels. Default is 1800.
- fill_kernel (array or tuple of int, optional): The size of the structuring element for morphological operations.
Default is (20, 20).
Returns:
- None
Expand Down Expand Up @@ -278,11 +256,8 @@ def write_image(filename, lat, lon, image_width=3600, image_height=1800, fill_ke
# Set the pixel values to 255 at the calculated coordinates
image[img_y, img_x] = 255

# Process the image (fill gaps using morphological closing)
processed_image = process_mask(image, fill_kernel=fill_kernel)

# Save the image
result_image = Image.fromarray(processed_image)
result_image = Image.fromarray(image)
result_image.save(filename)


Expand Down Expand Up @@ -377,7 +352,7 @@ def footprint_open_cv(lon, lat, pixel_height=1800, path=None, threshold_value=18
# Create and save the image
filename = f"{path}/image_original_{uuid.uuid4()}.png"
processed_filename = f"{path}/image_processed_{uuid.uuid4()}.png"
write_image(filename, new_lat, new_lon, image_width=pixel_width, image_height=pixel_height, fill_kernel=fill_kernel)
write_image(filename, new_lat, new_lon, image_width=pixel_width, image_height=pixel_height)

img_th = read_and_threshold_image(filename, threshold_value)
img_cleaned = apply_morphological_operations(img_th, fill_kernel=fill_kernel, output_path=processed_filename)
Expand Down
4 changes: 2 additions & 2 deletions tests/input/SCATSAT1_ESDR_L2_WIND_STRESS_V1.1_open_cv.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"smooth_poles": [82,90]
},
"open_cv":{
"pixel_height":1800,
"pixel_height":900,
"simplify":0.5,
"fill_kernel": [10,10]
"fill_kernel": [20,20]
}
},
"footprinter": "forge-py"
Expand Down
2 changes: 1 addition & 1 deletion tests/results/footprint_result_open_cv.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
POLYGON ((-171.7 88.9, -171.8 88.2, -173.7 87.7, -175.3 88.5, -178.7 88.6, -180 87.3, -180 52.6, -178.2 51.7, -170 38.4, -165.1 26.3, -160.1 9.2, -155.1 -14.2, -148.7 -53, -146.4 -69.4, -146 -84.3, -144.1 -83.9, -142 -86.5, -140.2 -86.5, -138.5 -87.7, -138.5 -88.9, -136.4 -89.3, -128.5 -88.5, -128 -89.4, -127 -89.4, -126.9 -88.7, -125 -88.1, -123.4 -89, -121 -89, -116.8 -87.4, -112.9 -88.8, -109.1 -88.2, -107.4 -89.1, -102 -88.9, -100.1 -87.5, -99.4 -85.9, -98.3 -85.8, -92.3 -89.1, -87.1 -89, -86.8 -88.4, -85.3 -89.1, -81.4 -89, -80.3 -87.5, -78.2 -87.5, -76.3 -89.3, -73.3 -89.9, -72.4 -89.9, -72.3 -89.1, -60 -88.8, -57.8 -89.9, -54.8 -89.3, -51.7 -87.4, -50.8 -87.4, -49.1 -89.2, -45.8 -89.1, -45.2 -88.4, -38.7 -89.1, -35.4 -86.9, -34 -86.9, -33.7 -85.9, -31.6 -86.2, -29.1 -88.9, -23.7 -89, -22 -88.2, -18.1 -88.8, -14.3 -87.4, -11.1 -89.1, -7.7 -89, -7.1 -88.2, -4.7 -88.4, -4.1 -89.4, -3.1 -89.4, -2.6 -88.5, 7.2 -89, 7.3 -87.7, 9.5 -86.2, 11.1 -86.2, 11.7 -84.8, 12.9 -84.8, 13 -84, 14.9 -84.1, 15.6 -67.5, 20.4 -35.2, 26.8 -0.5, 33.2 24, 36.8 33.8, 41.6 43.6, 47.3 51.8, 53.3 57.8, 64.5 64.8, 80.3 69.9, 101.1 72.7, 121.5 73, 139.7 71.5, 157.3 67.6, 169.9 61.9, 179.9 53.9, 179.9 87.4, 177.2 86.9, 176.8 87.7, 174.3 88.3, 170.4 87.7, 168.7 88.4, 163.3 88.4, 160.8 85.6, 158.7 85.6, 158.4 86.6, 156.9 86.6, 153.7 88.6, 147.2 87.9, 146.6 88.6, 143.3 88.7, 141.6 86.9, 139.6 87, 137.6 88.8, 133.7 88.9, 133 87.8, 122.8 88.6, 118.1 87.2, 116.1 84, 114 83.5, 111 87.1, 106.1 88.6, 101.8 88, 97.2 88.5, 96.8 87.5, 95.9 87.5, 95.2 88.9, 91.3 88.8, 88.2 86.7, 87.3 86.7, 86.2 88.5, 82.3 88.6, 80.8 87.9, 80.5 88.5, 75.3 88.6, 70.2 85.4, 68.2 85.4, 67.5 87, 65.6 88.4, 60.2 88.6, 58.5 87.7, 54.7 88.3, 50.8 86.9, 47.6 88.6, 44.2 88.5, 43.5 87.6, 41.2 87.9, 40.6 88.9, 39.6 88.9, 39.1 88, 37.8 88.7, 36.5 88.7, 36.4 88.1, 29.3 88.5, 29.2 87.2, 27.5 86.1, 25.7 86.1, 24.8 84.2, 23.7 84.2, 23.5 83.4, 21.6 83.7, 21.8 73.2, 19.5 56.5, 9.2 -2.5, 2 -28.3, -6 -45.5, -10.9 -52.4, -16.1 -57.6, -22.2 -62.1, -31.3 -66.6, -39.6 -69.4, -52.1 -71.9, -64.5 -73.2, -72.3 -72.7, -89.5 -69.9, -105.5 -64, -111.5 -60.3, -117 -55.7, -125.3 -45.1, -130.4 -35.1, -134.7 -23.6, -141.3 2.3, -150.7 57.5, -152.6 75.1, -152.6 84, -154.6 83.6, -154.7 84.3, -155.9 84.3, -156.6 85.8, -158.2 85.8, -160.3 87.2, -160.4 88.5, -167.4 88.1, -167.5 88.7, -168.9 88.7, -170.2 88, -170.7 88.9, -171.7 88.9))
POLYGON ((-180 90, -180 50.8, -177.8 50.8, -173.6 45, -169.6 37.6, -165 26.2, -158.6 3.2, -152.8 -26.6, -147 -64.2, -145.8 -78, -146.2 -85, -147.6 -87.2, -149.2 -88, -149.2 -89.8, 18.2 -89.8, 18.2 -88, 15.8 -86.2, 14.8 -82.6, 15.8 -66.2, 22.2 -24.8, 28.2 5.6, 34.4 27.4, 39.8 40.2, 45.8 49.8, 53.4 57.8, 64.6 64.8, 72.4 67.8, 81.8 70.2, 103 72.8, 121.4 73, 141.6 71.2, 156.6 67.8, 167.6 63.2, 178 55.4, 179.8 55.4, 179.8 90, 16.6 90, 16.6 88, 19.2 87.2, 21 85.4, 21.8 82.2, 21.4 70.2, 16.6 36.8, 12.2 11.8, 6.6 -13.6, 2.2 -28, -4 -42.2, -10.8 -52.4, -16 -57.6, -22.2 -62.2, -29.6 -66, -38.6 -69.2, -49.6 -71.6, -63.4 -73.2, -72.2 -72.8, -89.4 -70, -104.4 -64.6, -111.4 -60.4, -116 -56.6, -124.6 -46.2, -130.6 -34.6, -136.4 -17.8, -140.8 0, -145.4 23.8, -150.6 56.8, -152.8 78.2, -152.2 84.8, -150.4 87, -147.6 88, -147.6 90, -180 90))

0 comments on commit 6069e74

Please sign in to comment.