CoolFace
Apppublic

forestaiUs/treeextraction-ndvi

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
segmentation.cpython-312.pyc86 linesDownload Raw Back to __pycache__
1�

2��hk"���dZddlZddlZddlZddlZddlmZddlm	Z	m3Z4mZmZ			d	d�Z
			d5d�Zdd�Z				dd�Zy)
z�6Segmentation utilities for image processing inspired by CLIPSeg techniques.7This is a simplified version that does not require the full transformers library.8�N)�Image)�extract_contours�simplify_polygons�regularize_polygons�merge_nearby_polygonsc���	tj|�}|�Htj|�j	d�}tj|�}|dd�dd�ddd�f}tj|�\}}}	|	||g}9d|cxkrdkr	nn|10|}n$tj|tj�}|dkDrtj|d|�}tj||dtj�\}}
|r.tj||
�tjd|���|
S#t $r+}tj"d	t%|����Yd}~yd}~wwxYw)11a12    Segment an image based on color thresholding.13    This is a simple segmentation inspired by more complex models like CLIPSeg.14    15    Args:16        image_path (str): Path to the input image17        output_path (str, optional): Path to save the segmentation mask18        threshold (int): Pixel intensity threshold (0-255)19        color_channel (int): Color channel to use for thresholding (0=R, 1=G, 2=B)20        smoothing_sigma (float): Gaussian smoothing sigma21        22    Returns:23        numpy.ndarray: Segmentation mask24    N�RGB�����r��rr���Saved segmentation mask to �Error in segmentation: )�cv2�imreadr�open�convert�np�array�split�cvtColor�COLOR_BGR2GRAY�GaussianBlur�	threshold�
THRESH_BINARY�imwrite�logging�info�	Exception�error�str)�25image_path�output_pathr�
color_channel�smoothing_sigma�img�pil_img�b�g�r�channels�channel�_�mask�es               �1C:\Users\caoya\DEV\ForestAI\utils\segmentation.py�segment_by_color_thresholdr1
s8��"#��j�j��$���;��j�j��,�4�4�U�;�G��(�(�7�#�C��a��D�b�D�j�/�C��)�)�C�.���1�a��q�!�9���
�!��!��}�-�G��l�l�3��(:�(:�;�G��Q���&�&�w���H�G��-�-���C��9J�9J�K���4���K�K��T�*��L�L�6�{�m�D�E�������
�
�/��A��x�8�9����s�D+D.�.	E"�7!E�E"c��	tj|�}|�Htj|�j	d�}tj|�}|dd�dd�ddd�f}tj|tj�}|dkDrtj|d|�}tj|dtjtj||�}|r.tj||�tjd|���|S#t $r+}	tj"dt%|	����Yd}	~	yd}	~	wwxYw)	a�26    Segment an image using adaptive thresholding for better handling of27    lighting variations.28    29    Args:30        image_path (str): Path to the input image31        output_path (str, optional): Path to save the segmentation mask32        block_size (int): Size of the pixel neighborhood for threshold calculation33        c (int): Constant subtracted from the mean34        smoothing_sigma (float): Gaussian smoothing sigma35        36    Returns:37        numpy.ndarray: Segmentation mask38    Nr	r39rrr
rr)rrrrrrrrrr�adaptiveThreshold�ADAPTIVE_THRESH_GAUSSIAN_Crrrrrr r!)40r"r#�41block_size�cr%r&r'�grayr.r/s42          r0�segment_by_adaptive_thresholdr8Cs
��"��j�j��$���;��j�j��,�4�4�U�;�G��(�(�7�#�C��a��D�b�D�j�/�C��|�|�C��!3�!3�4���Q���#�#�D�&�/�B�D��$�$��#�s�5�5����z�1�43����K�K��T�*��L�L�6�{�m�D�E�������
�
�/��A��x�8�9����s�DD	�		D=�!D8�8D=c��	tj|�}|�Htj|�j	d�}tj|�}|dd�dd�ddd�f}tj|tj�}|dkDrtj|d|�}tj|ddtjtjz�\}}|r.tj||�tjd|���|S#t $r+}tj"dt%|����Yd}~yd}~wwxYw)	aH44    Segment an image using Otsu's automatic thresholding method.45    46    Args:47        image_path (str): Path to the input image48        output_path (str, optional): Path to save the segmentation mask49        smoothing_sigma (float): Gaussian smoothing sigma50        51    Returns:52        numpy.ndarray: Segmentation mask53    Nr	r54rrr
rr)rrrrrrrrrrrr�THRESH_OTSUrrrrr r!)	r"r#r%r&r'r7r-r.r/s	         r0�segment_by_otsur;us55����j�j��$���;��j�j��,�4�4�U�;�G��(�(�7�#�C��a��D�b�D�j�/�C��|�|�C��!3�!3�4���Q���#�#�D�&�/�B�D��-�-��a��c�.?�.?�#�/�/�.Q�R���4���K�K��T�*��L�L�6�{�m�D�E�������
�
�/��A��x�8�9����s�D56D
�
	E�!D<�<Ec��|j�dk(rt||ddd��}ni|j�dk(s|j�dk(rt||dd	d57��}n2|j�dk(rt||d
dd��}nt||d��}|�t	j58d�dgfSd}|s�tjjtjj|�tjjtjj|��d�d��}tj||�|}n|}t||��}	t	jdt!|	��d��|r4tjj#|�rt
j$|�t'|	|��}	|j�dk(rt)|	�}	t+|	|��}	t	jdt!|	��d��||	fS)ah59    Complete pipeline for segmentation and feature extraction.60    61    Args:62        image_path (str): Path to the input image63        output_mask_path (str, optional): Path to save the segmentation mask64        feature_type (str): Type of features to extract ("buildings", "trees", "water", "roads")65        min_area (int): Minimum feature area to keep66        simplify_tolerance (float): Tolerance for polygon simplification67        merge_distance (float): Distance for merging nearby polygons68        69    Returns:70        tuple: (mask, polygons) - Segmentation mask and list of simplified Shapely polygons71    �	buildings����?)r5r6r%�trees�72vegetation��g�?)rr$r%�water�xr�@)r%NzSegmentation failedz	_mask.png)�min_areaz73Extracted z initial polygons)�	tolerance)�distance_thresholdzAfter processing: z	 polygons)�lowerr8r1r;rr �os�path�join�dirname�splitext�basenamerrrr�len�exists�removerrr)74r"�output_mask_path�feature_typerH�simplify_tolerance�merge_distancer.�temp_mask_path�	mask_path�polygonss75          r0�segment_and_extract_featuresr\�s���&����{�*�,��(��Q��76��77�	�	�	��	(�L�,>�,>�,@�L�,P�)��(���C�78��79�	�	�	��	(�)��(���C�80����(�#�81���|��
�
�+�,��R�x���N��������G�G�O�O�J�'��w�w������ 0� 0�� <�=�a�@�A��K�82��	���N�D�)�"�	�$�	� �	�H�=�H��L�L�:�c�(�m�_�,=�>�?��"�'�'�.�.��8�83�	�	�.�!�!��5G�H�H�����{�*�&�x�0��%�X�.�Q�H��L�L�%�c�(�m�_�I�>�?���>��)N�rDr@)N�r?r@)Nr@)Nr=�2rGg@)�__doc__rLr�numpyrr�PILr�utils.geospatialrrrrr1r8r;r\�r]r0�<module>rfs\���8485���86��l�l�7;�;<�.1�4�l;?�23�14�0�d(�T?C�-8�@C�/2�Nr]