
    TAi"                     R    S r SSKrSSKrSS jr SS jrS rS rS rS r	S	 r
S
 rg)zpFunctions useful for detecting graphical elements from the image to using OpenCV to reconstruct / detect tables.    Nc                 $   [         R                  " U 5      n[         R                  " U[         R                  5      nU(       d  [        R
                  " U5      n[         R                  " US[         R                  [         R                  X#5      nXF4$ )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npinvertadaptiveThresholdADAPTIVE_THRESH_GAUSSIAN_CTHRESH_BINARY)	imagenameprocess_background	blocksizecimggray	thresholds          T/var/www/html/land-ocr/venv/lib/python3.13/site-packages/camelot/image_processing.pyadaptive_thresholdr      si    : **Y
C<<S//0Dyy%%c31133D3DiI >    c                 0   US;  a  [        S5      e[        XU5      u  pV[        X5      n [        XU5      n[        R
                  " UR                  [        R                  5      [        R                  [        R                  5      u  p[        X5      n
Xz4$ )a  
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: 40)
    Factor by which the page dimensions will be divided to get
    smallest length of lines that should be detected.
iterations : int, optional (default: 0)
    Number of times for erosion/dilation is applied.

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.
)vertical
horizontalz6Specify direction as either 'vertical' or 'horizontal')
ValueErrorcreate_structuring_elementapply_region_maskprocess_imager   findContoursastyper	   uint8RETR_EXTERNALCHAIN_APPROX_SIMPLEextract_lines_from_contours)r   regions	direction
line_scale
iterationselsizeprocessed_thresholdcontours_liness              r   
find_linesr/   .   s    @ 22QRR))
KHB!)5I'	zB""""288,c.?.?AXAXKH (<E%%r   c                     US:X  a<  U R                   S   U-  n[        R                  " [        R                  SU45      nXC4$ U R                   S   U-  n[        R                  " [        R                  US45      nXC4$ )ar  
Create a structuring element based on the specified direction.

Parameters
----------
threshold : object
    numpy.ndarray representing the thresholded image.
direction : string
    Direction to create the structuring element.
line_scale : int
    Factor for scaling the size of the structuring element.

Returns
-------
tuple
    The structuring element and its size.
r   r      )shaper   getStructuringElement
MORPH_RECT)r   r&   r'   r*   r)   s        r   r   r   ^   sx    $ Jq!Z/&&s~~4yA
 8O q!Z/&&s~~ayA8Or   c                     Ub_  [         R                  " U R                  [         R                  S9nU H  nUu  pEpgSX%XW-   2XDU-   24'   M     [         R                  " X5      n U $ )a  
Apply a mask to the threshold image based on specified regions.

Parameters
----------
threshold : object
    numpy.ndarray representing the thresholded image.
regions : list
    List of regions to apply the mask.

Returns
-------
numpy.ndarray
    The masked threshold image.
)dtyper1   )r	   zerosr2   r!   multiply)r   r%   region_maskregionxywhs           r   r   r   z   sd      hhybhh?FJA!01KAE	11u9,-  KK	7	r   c                     [         R                  " X5      n [         R                  " X5      n [         R                  " XUS9nU$ )aT  
Apply morphological operations to the threshold image.

Parameters
----------
threshold : object
    numpy.ndarray representing the thresholded image.
el : object
    Structuring element for morphological operations.
iterations : int
    Number of iterations for dilation.

Returns
-------
numpy.ndarray
    The processed threshold image.
)r(   )r   erodedilate)r   r)   r(   dmasks       r   r   r      s5    $ 		)(I

9)IJJy<ELr   c                     / nU  Ht  n[         R                  " U5      u  pEpgXDU-   pXUU-   pUS:X  a!  UR                  X-   S-  XU	-   S-  U
45        MM  US:X  d  MU  UR                  XU-   S-  XU-   S-  45        Mv     U$ )a5  
Extract lines from contours based on the specified direction.

Parameters
----------
contours : list
    List of contours found in the image.
direction : string
    Specifies whether to extract vertical or horizontal lines.

Returns
-------
list
    List of tuples representing the coordinates of the lines.
r      r   )r   boundingRectappend)r,   r&   r.   r   r;   r<   r=   r>   x1x2y1y2s               r   r$   r$      s      E%%a(
aEBEB
"LL27q."Bw1nbAB,&LL"Bw1nb7q.AB  Lr   c                    X-   n[         R                  " UR                  [        R                  5      [         R
                  [         R                  5      u  p4[        U[         R                  SS9SS n/ nU HG  n[         R                  " USS5      n[         R                  " U5      u  ppUR                  XX45        MI     U$ )a  Find 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#   sortedcontourAreaapproxPolyDPrE   rF   )r   r   maskr,   __contr   c_polyr;   r<   r=   r>   s               r   find_contoursrW      s    $  D##BHHs00#2I2ILH hCOOTB3BGHD!!!Q-%%f-
aQ1L!  Kr   c                    [         R                  " X5      n0 nU  H  nUu  pgpX7Xy-   2XfU-   24   n
[        R                  " U
R	                  [         R
                  5      [        R                  [        R                  5      u  p[        U5      S::  a  Mz  / nU HM  n[        R                  " U5      u  nnnnUSU-  U-   S-  -   USU-  U-   S-  -   nnUR                  UU45        MO     XXgU	-   Xh-   U4'   M     U$ )a  Find 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.

   rD   )r	   r8   r   r   r    r!   
RETR_CCOMPr#   lenrE   rF   )r,   r   r   jointstablesr   r;   r<   r=   r>   roijcrT   joint_coordsjjxjyjwjhc1c2s                        r   find_jointsrh      s   . [[.FF
aYE	)*!!JJrxx #..#2I2I
 r7a<A --a0NBB!b&2+!++Q!b&2+!1C-CBR)  (4q5!%#$  Mr   )F   )Nr   (   r   )__doc__r   numpyr	   r   r/   r   r   r   r$   rW   rh    r   r   <module>ro      s@    v 
 $P PQ-&`842:D(r   