
    :Qg*                     2    d dl Z d dlZddZ	 ddZd Zd Zy)    Nc                    t        j                  |       }t        j                  |t         j                        }|r:t        j                  |dt         j
                  t         j                  ||      }||fS t        j                  t        j                  |      dt         j
                  t         j                  ||      }||fS )a\  Thresholds an image using OpenCV's adaptiveThreshold.

    Parameters
    ----------
    imagename : string
        Path to image file.
    process_background : bool, optional (default: False)
        Whether or not to process lines that are in background.
    blocksize : int, optional (default: 15)
        Size of a pixel neighborhood that is used to calculate a
        threshold value for the pixel: 3, 5, 7, and so on.

        For more information, refer `OpenCV's adaptiveThreshold <https://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html#adaptivethreshold>`_.
    c : int, optional (default: -2)
        Constant subtracted from the mean or weighted mean.
        Normally, it is positive but may be zero or negative as well.

        For more information, refer `OpenCV's adaptiveThreshold <https://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html#adaptivethreshold>`_.

    Returns
    -------
    img : object
        numpy.ndarray representing the original image.
    threshold : object
        numpy.ndarray representing the thresholded image.

       )	cv2imreadcvtColorCOLOR_BGR2GRAYadaptiveThresholdADAPTIVE_THRESH_GAUSSIAN_CTHRESH_BINARYnpinvert)	imagenameprocess_background	blocksizecimggray	thresholds          U/var/www/html/answerous/venv/lib/python3.12/site-packages/camelot/image_processing.pyadaptive_thresholdr      s    8 **Y
C<<S//0D))#s55s7H7H)UV
	 	> ))IIdO**
	 	>    c                    g }|dk(  r9| j                   d   |z  }t        j                  t        j                  d|f      }nK|dk(  r9| j                   d   |z  }t        j                  t        j                  |df      }n|t	        d      |Tt        j                  | j                         }|D ]  }	|	\  }
}}}d||||z   |
|
|z   f<    t        j                  | |      } t        j                  |       } t        j                  | |      } t        j                  | ||      }	 t        j                  | j                  t
        j                        t        j                  t        j                        \  }}}|D ]x  }t        j                  |      \  }
}}}|
|
|z   }}|||z   }}|dk(  r"|j!                  ||z   dz  |||z   dz  |f       R|dk(  sX|j!                  |||z   dz  |||z   dz  f       z ||fS # t        $ rV t        j                  | j                  t
        j                        t        j                  t        j                        \  }}Y w xY w)ai  Finds horizontal and vertical lines by applying morphological
    transformations on an image.

    Parameters
    ----------
    threshold : object
        numpy.ndarray representing the thresholded image.
    regions : list, optional (default: None)
        List of page regions that may contain tables of the form x1,y1,x2,y2
        where (x1, y1) -> left-top and (x2, y2) -> right-bottom
        in image coordinate space.
    direction : string, optional (default: 'horizontal')
        Specifies whether to find vertical or horizontal lines.
    line_scale : int, optional (default: 15)
        Factor by which the page dimensions will be divided to get
        smallest length of lines that should be detected.

        The larger this value, smaller the detected lines. Making it
        too large will lead to text being detected as lines.
    iterations : int, optional (default: 0)
        Number of times for erosion/dilation is applied.

        For more information, refer `OpenCV's dilate <https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html#dilate>`_.

    Returns
    -------
    dmask : object
        numpy.ndarray representing pixels where vertical/horizontal
        lines lie.
    lines : list
        List of tuples representing vertical/horizontal lines with
        coordinates relative to a left-top origin in
        image coordinate space.

    verticalr      
horizontalz6Specify direction as either 'vertical' or 'horizontal')
iterations   )shaper   getStructuringElement
MORPH_RECT
ValueErrorr   zerosmultiplyerodedilatefindContoursastypeuint8RETR_EXTERNALCHAIN_APPROX_SIMPLEboundingRectappend)r   regions	direction
line_scaler   linessizeelregion_maskregionxywhdmask_contoursr   x1x2y1y2s                         r   
find_linesr@   6   sL   L EJq!Z/&&s~~4yA	l	"q!Z/&&s~~ayA		QRRhhy/ 	2FJAq!Q01KAE	1q1u9,-	2 KK	;7			)R(I

9b)IJJy"<E
))RXX&(9(93;R;R
8Q  C%%a(
1aAEBAEB
"LL27q."rBw1nbAB,&LL"rBw1nb27q.ABC %<  
&&RXX&(9(93;R;R
!
s   'AG< <AIIc                 Z   | |z   }	 t        j                  |j                  t        j                        t         j
                  t         j                        \  }}}t        |t         j                  d      dd }g }|D ]H  }t        j                  |dd      }t        j                  |      \  }}	}
}|j                  ||	|
|f       J |S # t        $ rV t        j                  |j                  t        j                        t         j
                  t         j                        \  }}Y w xY w)a  Finds table boundaries using OpenCV's findContours.

    Parameters
    ----------
    vertical : object
        numpy.ndarray representing pixels where vertical lines lie.
    horizontal : object
        numpy.ndarray representing pixels where horizontal lines lie.

    Returns
    -------
    cont : list
        List of tuples representing table boundaries. Each tuple is of
        the form (x, y, w, h) where (x, y) -> left-top, w -> width and
        h -> height in image coordinate space.

    T)keyreverseN
      )r   r&   r'   r   r(   r)   r*   r!   sortedcontourAreaapproxPolyDPr+   r,   )r   r   mask__r;   contr   c_polyr5   r6   r7   r8   s               r   find_contoursrM      s   $ j D
++KK!3#4#4c6M6M
Hb hCOOTB3BGHD "!!!Q-%%f-
1aQ1aL!" K  
''KK!3#4#4c6M6M
"
s   AC AD*)D*c                    t        j                  ||      }i }| D ]  }|\  }}}}	||||	z   |||z   f   }
	 t        j                  |
j	                  t         j
                        t        j                  t        j                        \  }}}t        |      dk  rg }|D ]K  }t        j                  |      \  }}}}|d|z  |z   dz  z   |d|z  |z   dz  z   }}|j                  ||f       M |||||	z   ||z   |f<    |S # t        $ rV t        j                  |
j	                  t         j
                        t        j                  t        j                        \  }}Y w xY w)a  Finds joints/intersections present inside each table boundary.

    Parameters
    ----------
    contours : list
        List of tuples representing table boundaries. Each tuple is of
        the form (x, y, w, h) where (x, y) -> left-top, w -> width and
        h -> height in image coordinate space.
    vertical : object
        numpy.ndarray representing pixels where vertical lines lie.
    horizontal : object
        numpy.ndarray representing pixels where horizontal lines lie.

    Returns
    -------
    tables : dict
        Dict with table boundaries as keys and list of intersections
        in that boundary as their value.
        Keys are of the form (x1, y1, x2, y2) where (x1, y1) -> lb
        and (x2, y2) -> rt in image coordinate space.

       r   )r   r#   r   r&   r'   r(   
RETR_CCOMPr*   r!   lenr+   r,   )r;   r   r   jointstablesr   r5   r6   r7   r8   roirJ   jcjoint_coordsjjxjyjwjhc1c2s                        r   find_jointsr^      so   . [[:.FF 4
1aQQYAE	)*	))

288$cnnc6M6MJBB r7a< 	*A --a0NBB!b&2+!++Q!b&2+!1C-CBR)	* (41q5!a%#$'4* M  	%%

288$cnnc6M6MFB	s   AC??AEE)F   )Nr   r_   r   )r   numpyr   r   r@   rM   r^    r   r   <module>rc      s,     ,` PQOd%P.r   