CoolFace
Apppublic

forestaiUs/treeextraction-ndvi

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
segmentation.cpython-310.pyc91 linesDownload Raw Back to __pycache__
1o

2&�hk"�@s�dZddlZddlZddlZddlZddlmZddlm	Z	m3Z4mZmZ			ddd	�Z
			ddd
�Zddd�Z				ddd�ZdS)z�5Segmentation utilities for image processing inspired by CLIPSeg techniques.6This is a simplified version that does not require the full transformers library.7�N)�Image)�extract_contours�simplify_polygons�regularize_polygons�merge_nearby_polygons����?c8Cs zrt�|�}|dur%t�|��d�}t�|�}|dd�dd�ddd�f}t�|�\}}}	|	||g}9d|kr<dkrCnn|10|}nt�|tj	�}|dkrUt�11|d|�}t�||dtj�\}}
|rpt�
||
�t�d|���|
WSty�}zt�d	t|����WYd}~dSd}~ww)12a13    Segment an image based on color thresholding.14    This is a simple segmentation inspired by more complex models like CLIPSeg.15    16    Args:17        image_path (str): Path to the input image18        output_path (str, optional): Path to save the segmentation mask19        threshold (int): Pixel intensity threshold (0-255)20        color_channel (int): Color channel to use for thresholding (0=R, 1=G, 2=B)21        smoothing_sigma (float): Gaussian smoothing sigma22        23    Returns:24        numpy.ndarray: Segmentation mask25    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)�26image_path�output_pathr�
color_channel�smoothing_sigma�img�pil_img�b�g�r�channels�channel�_�mask�e�r1�O/Users/dynamicpacific/Dropbox/DEV/ForestAI-TreeExtraction/utils/segmentation.py�segment_by_color_threshold
s.27282930��r3��c3132Cs�zUt�|�}|dur%t�|��d�}t�|�}|dd�dd�ddd�f}t�|tj�}|dkr7t�	|d|�}t�33|dtjtj||�}|rSt�
||�t�d|���|WStyr}	zt�dt|	����WYd}	~	dSd}	~	ww)	a�34    Segment an image using adaptive thresholding for better handling of35    lighting variations.36    37    Args:38        image_path (str): Path to the input image39        output_path (str, optional): Path to save the segmentation mask40        block_size (int): Size of the pixel neighborhood for threshold calculation41        c (int): Constant subtracted from the mean42        smoothing_sigma (float): Gaussian smoothing sigma43        44    Returns:45        numpy.ndarray: Segmentation mask46    Nr47rrr
rrr)rrrrrrrrrr�adaptiveThreshold�ADAPTIVE_THRESH_GAUSSIAN_Crrrrr r!r")48r#r$�49block_size�cr&r'r(�grayr/r0r1r1r2�segment_by_adaptive_thresholdCs,5051���r;c	52Cs�zWt�|�}|dur%t�|��d�}t�|�}|dd�dd�ddd�f}t�|tj�}|dkr7t�	|d|�}t�53|ddtjtj�\}}|rUt�
||�t�d|���|WStyt}zt�dt|����WYd}~dSd}~ww)	aH54    Segment an image using Otsu's automatic thresholding method.55    56    Args:57        image_path (str): Path to the input image58        output_path (str, optional): Path to save the segmentation mask59        smoothing_sigma (float): Gaussian smoothing sigma60        61    Returns:62        numpy.ndarray: Segmentation mask63    Nr64rrr
rrr)rrrrrrrrrrrr�THRESH_OTSUrrrr r!r")	r#r$r&r'r(r:r.r/r0r1r1r2�segment_by_otsuus&6566��r=�	buildings�2�@�@c67Csh|��dkrt||dddd�}n-|��dks|��dkr&t||dd	d68d�}n|��dkr6t||d
ddd�}nt||dd�}|durJt�d�dgfSd}|sotj�tj�	|�tj�69tj�|��d�d��}t�
||�|}n|}t||d�}	t�dt|	��d��|r�tj�|�r�t�|�t|	|d�}	|��dkr�t|	�}	t|	|d�}	t�dt|	��d��||	fS)ah70    Complete pipeline for segmentation and feature extraction.71    72    Args:73        image_path (str): Path to the input image74        output_mask_path (str, optional): Path to save the segmentation mask75        feature_type (str): Type of features to extract ("buildings", "trees", "water", "roads")76        min_area (int): Minimum feature area to keep77        simplify_tolerance (float): Tolerance for polygon simplification78        merge_distance (float): Distance for merging nearby polygons79        80    Returns:81        tuple: (mask, polygons) - Segmentation mask and list of simplified Shapely polygons82    r>�r5r	)r8r9r&�trees�83vegetation�rg�?)rr%r&�water�xrr@)r&NzSegmentation failedz	_mask.png)�min_areaz84Extracted z initial polygons)�	tolerance)�distance_thresholdzAfter processing: z	 polygons)�lowerr;r3r=rr!�os�path�join�dirname�splitext�basenamerrrr�len�exists�removerrr)85r#�output_mask_path�feature_typerH�simplify_tolerance�merge_distancer/Ztemp_mask_pathZ	mask_path�polygonsr1r1r2�segment_and_extract_features�sP����8687�88rZ)Nrrr	)Nr4r5r	)Nr	)Nr>r?r@rA)�__doc__rLr�numpyrr�PILr�utils.geospatialrrrrr3r;r=rZr1r1r1r2�<module>s*89�690�912*�