+
    åûj¨y  ã                   ó0  a € R t#0 t ^ RIt^ RIHtHtHtHtHtHtH	t	 ^ RI
t
^ RI
Ht ^ RIHt ^ RIHtHt ^ RIHtHt ^ RIHtHtHtHt ^ RIHt ^R	IHt / t] ^ k ]! R
R4      R.]R&    ! R R]P<                  4      t ! R R4      t R R lt!RR R llt"R# )é    N)ÚAnyÚDictÚListÚOptionalÚTupleÚUnionÚcast)Únn)ÚKORNIA_CHECK_SHAPE)Úhf_urlÚload_state_dict_from_url)Údataclass_to_dictÚdict_to_dataclass)ÚDetectorCfgÚHeatMapRefineCfgÚJunctionRefineCfgÚLineDetectorCfg)Únms)ÚSOLD2NetÚsold2zsold2_wireframe.pthz=https://cmp.felk.cvut.cz/~mishkdmy/models/sold2_wireframe.pthÚ	wireframec                   ój   a a€ ] tR t^'t oRtR	V3R lV 3R llltV3R lR ltV3R lR ltRtVt	V ;t
# )
ÚSOLD2_detectoru
  Detect line segments in grayscale images with SOLD2, without describing or matching them.

``detector = SOLD2_detector()`` builds the network with its pretrained weights, in evaluation mode, and
``outputs = detector(images)`` takes grayscale images ``(B, 1, H, W)`` in :math:`[0, 1]`, with ``H`` and ``W``
multiples of 8, and returns a dict: ``"line_segments"``, one ``(N, 2, 2)`` tensor of ``(row, col)`` endpoints in
pixels per image, and the ``"junction_heatmap"`` and ``"line_heatmap"``, ``(B, H, W)`` each. With the same weights
and ``config``, these are the segments and heatmaps of :class:`SOLD2`; to also describe the segments and match them
between images, use :class:`SOLD2` instead. This is based on the original code from the paper "SOLDÂ²:
Self-supervised Occlusion-aware Line Detector and Descriptor". See :cite:`SOLD22021` for more details.

Convention:
    - Sizes that are not multiples of 8 raise an error or give heatmaps of another size.
    - ``line_segments`` holds one ``(N, 2, 2)`` tensor per image: the two endpoints of each segment as
      ``(row, col)`` in the pixel convention of :func:`~kornia.feature.laf_from_center_scale_ori`
      (see :ref:`Local features <local-features-conventions>`), so ``flip(-1)`` gives ``(x, y)``. The endpoints
      of a segment, and the segments by their first endpoint, are in raster order: the smaller row first or, on
      the same row, the smaller column, whatever the junction scores.
    - The endpoints are junction coordinates. The default junction refinement (``use_junction_refinement`` of
      ``LineDetectorCfg``) can produce sub-pixel endpoints in 0.25-pixel steps, up to one pixel per axis.

Args:
    config (DetectorCfg): Configuration object containing all parameters, or a dict of its fields. None will load
        the default parameters, which are tuned for images in the range 400~800 px. A ``config`` with
        ``use_descriptor=True`` builds the descriptor head, which the outputs do not use.
    pretrained (bool): If True, download and set pretrained weights to the model, and return it in eval mode;
        otherwise the weights are random and the model is in training mode.

Returns:
    The raw junction and line heatmaps, as well as the list of detected line segments (ij coordinates convention).

See Also:
    - :class:`SOLD2`: the same line detection plus the descriptor map and line matching.

Example:
    >>> img = torch.rand(1, 1, 128, 128)
    >>> sold2_detector = SOLD2_detector(pretrained=False).eval()
    >>> line_segments = sold2_detector(img)["line_segments"]

c          	      óf   <€ V ^8„  d   QhRS[ RS[S[S[S[S[S[3,          3,          ,          RR/# )é   Ú
pretrainedÚconfigÚreturnN)Úboolr   r   r   r   Ústrr   )ÚformatÚ__classdict__s   "€Ú_C:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\feature\sold2\sold2_detector.pyÚ__annotate__ÚSOLD2_detector.__annotate__P   s?   ø€ ÷ ñ ¡4ð ¹ÁÁ{ÑTXÑY\Ñ^aÐYaÕTbÐGbÕAcÕ8dð Ðptñ ó    c                ó
  <€ \        V\        4      '       d0   \        \        \        \
        \        3,          V4      \        4      p\        SV `%  4        Ve   TM	\        4       V n
        V P                  P                  V n        V P                  P                  V n        V P                  P                  V n        \        \!        V P                  4      4      V n        V'       d`   \%        \&        R,          \(        P*                  ! R4      R7      pV P-                  VR,          4      pV P"                  P/                  V4       \1        V P                  P2                  4      V n        V'       d   V P7                  4        R # R # )Nr   Úcpu)Úmap_locationÚmodel_state_dict)Ú
isinstanceÚdictr   r	   r   r    r   r   ÚsuperÚ__init__r   Ú	grid_sizeÚdetection_threshÚjunc_detect_threshÚmax_num_junctionsr   r   Úmodelr   ÚurlsÚtorchÚdeviceÚadapt_state_dictÚload_state_dictÚLineSegmentDetectionModuleÚline_detector_cfgÚline_detectorÚeval)Úselfr   r   Úpretrained_dictÚ
state_dictÚ	__class__s   &&&  €r#   r.   ÚSOLD2_detector.__init__P   s  ø€ Ü�fœd×#Ò#Ü&¤t¬D´´c°­N¸FÓ'CÄ[ÓQˆFÜ‰ÑÔà &Ò 2‘f¼»ˆŒØŸ™×.Ñ.ˆŒØ"&§+¡+×">Ñ">ˆÔØ!%§¡×!>Ñ!>ˆÔô Ô/°·±Ó<Ó=ˆŒ
çÜ6´t¸KÕ7HÔW\×WcÒWcÐdiÓWjÔkˆOØ×.Ñ.¨Ð?QÕ/RÓSˆJØ�J‰J×&Ñ& zÔ2ô 8¸¿¹×8UÑ8UÓVˆÔßØ�I‰IŽKñ r&   c                óR   <€ V ^8„  d   QhRS[ S[S[3,          RS[ S[S[3,          /# )r   r?   r   )r   r    r   )r!   r"   s   "€r#   r$   r%   g   s+   ø€ ÷ ñ ©4±±S°­>ð ¹dÁ3ÉÀ8½nñ r&   c                óT   € VR VR VR VR,          VR&   VR,          VR&   VR VR V# )a  Adapt pretrained checkpoint keys to this module implementation.

Args:
    state_dict: Checkpoint state dictionary whose keys are adapted to this module layout.

Returns:
    State dictionary with checkpoint keys renamed or removed so it can be loaded by the current module.
Úw_juncÚ	w_heatmapÚw_descz'heatmap_decoder.conv_block_lst.2.weightz)heatmap_decoder.conv_block_lst.2.0.weightz%heatmap_decoder.conv_block_lst.2.biasz'heatmap_decoder.conv_block_lst.2.0.bias© )r=   r?   s   &&r#   r7   ÚSOLD2_detector.adapt_state_dictg   sV   € ð �xÐ Ø�{Ð#Ø�xÐ ØBLÐMvÕBwˆ
Ð>Ñ?Ø@JÐKrÕ@sˆ
Ð<Ñ=ØÐ@ÐAØÐ>Ð?ØÐr&   c                óP   <€ V ^8„  d   QhRS[ P                  RS[S[S[3,          /# )r   Úimgr   )r5   ÚTensorr   r    r   )r!   r"   s   "€r#   r$   r%   y   s'   ø€ ÷ ñ ™5Ÿ<™<ð ©D±±c°­Nñ r&   c                ó�  € \        V. RO4       / pV P                  V4      pVR,          VR&   VR,          VR&   . p\        VR,          VR,          4       Fh  w  rV\        WPP                  V P
                  V P                  4      pV P                  P                  Wv4      w  r‡p	VP                  \        Wx4      4       Kj  	  WBR&   V# )a<  Run forward.

Args:
    img: batched images with shape ``(B, 1, H, W)``.

Returns:
    line_segments: a list of ``B`` tensors ``(N, 2, 2)``, the ``(row, col)`` segment endpoints per image.
    junction_heatmap: raw junction heatmap of shape ``(B, H, W)``.
    line_heatmap: raw line heatmap of shape ``(B, H, W)``.

Ú	junctionsÚjunction_heatmapÚheatmapÚline_heatmapÚline_segments)ÚBÚ1ÚHÚW)r   r3   ÚzipÚprob_to_junctionsr/   r1   r2   r;   ÚdetectÚappendÚline_map_to_segments)
r=   rJ   ÚoutputsÚnet_outputsÚlinesÚ	junc_probrO   rM   Úline_mapÚ_s
   &&        r#   ÚforwardÚSOLD2_detector.forwardy   sÅ   € ô 	˜3Ò 4Ô5Øˆð —j‘j “oˆØ&1°+Õ&>ˆÐ"Ñ#Ø"-¨iÕ"8ˆ�Ñð ˆÜ"% k°+Õ&>ÀÈIÕ@VÖ"WÑˆIä)¨)·^±^ÀT×E\ÑE\Ð^b×^tÑ^tÓuˆIð &*×%7Ñ%7×%>Ñ%>¸yÓ%RÑ"ˆH Ø�L‰LÔ-¨iÓBÖCñ #Xð $)�Ñ àˆr&   )r   r/   r1   r;   r2   r3   )TN)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r.   r7   ra   Ú__static_attributes__Ú__classdictcell__Ú__classcell__)r@   r"   s   @@r#   r   r   '   s.   ù‡ € ñ&÷Põ ÷.ð ÷$÷ ð r&   r   c                   óÒ   a € ] tR t^›t o RtRV 3R lR lltV 3R lR ltRV 3R lR lltRV 3R	 lR
 lltV 3R lR lt	V 3R lR lt
V 3R lR ltV 3R lR ltV 3R lR ltRtV tR# )r9   a�  nn.Module extracting line segments from junctions and line heatmaps.

Args:
    config (LineDetectorCfg): Configuration dataclass containing all settings required for line segment detection.
        - detect_thresh (float): Probability threshold for mean activation (0. ~ 1.).
        - num_samples (int): Number of sampling locations along the line segments.
        - inlier_thresh (float): Minimum inlier ratio to satisfy (0. ~ 1.) => 0. means no threshold.
        - heatmap_low_thresh (float): Lowest threshold for pixel considered as a candidate in junction recovery.
        - heatmap_high_thresh (float): Higher threshold for NMS in junction recovery.
        - max_local_patch_radius (float): Maximum patch to be considered in local maximum search.
        - lambda_radius (float): Lambda factor in linear local maximum search formulation.
        - use_candidate_suppression (bool): Apply candidate suppression to break long segments into sub-segments.
        - nms_dist_tolerance (float): Distance tolerance for NMS. Decides whether the junctions are on the line.
        - use_heatmap_refinement (bool): Whether to use heatmap refinement methods.
        - heatmap_refine_cfg: Configuration for heatmap refinement methods.
        - use_junction_refinement (bool): Whether to use junction refinement methods.
        - junction_refine_cfg: Configuration for junction refinement methods.

Example:
    >>> config = LineDetectorCfg(detect_thresh=0.6, use_heatmap_refinement=True)
    >>> module = LineSegmentDetectionModule(config)
    >>> junctions, heatmap = torch.rand(10, 2), torch.rand(256, 256)
    >>> line_map, junctions, _ = module.detect(junctions, heatmap)

Nc                ó4   <€ V ^8„  d   QhRS[ S[,          RR/# )r   r   r   N)r   r   )r!   r"   s   "€r#   r$   Ú'LineSegmentDetectionModule.__annotate__¶   s$   ø€ ÷ %Lñ %L™x©Õ8ð %LÀDñ %Lr&   c                óê  € Vf   \        4       pWn        V P                  P                  V n        V P                  P                  V n        V P                  P                  V n        V P                  P
                  V n        V P                  P                  V n        V P                  P                  V n	        V P                  P                  V n        \        P                  ! ^ ^V P                  4      V n        V P                  P                  V n        V P                  P                   V n        V P                  P"                  V n        V P                  P$                  V n        V P"                  '       d   V P$                  f   \'        R4      hV P                  P(                  V n        V P                  P*                  V n        V P(                  '       d   V P*                  f   \'        R4      hR # R # )Nz*[Error] Missing heatmap refinement config.z+[Error] Missing junction refinement config.)r   r   Údetect_threshÚnum_samplesÚinlier_threshÚmax_local_patch_radiusÚlocal_patch_radiusÚlambda_radiusÚheatmap_low_threshÚ
low_threshÚheatmap_high_threshÚhigh_threshr5   ÚlinspaceÚtorch_samplerÚuse_candidate_suppressionÚnms_dist_toleranceÚuse_heatmap_refinementÚheatmap_refine_cfgÚ
ValueErrorÚuse_junction_refinementÚjunction_refine_cfg)r=   r   s   &&r#   r.   Ú#LineSegmentDetectionModule.__init__¶   su  € àŠ>Ü$Ó&ˆFØŒð "Ÿ[™[×6Ñ6ˆÔð  Ÿ;™;×2Ñ2ˆÔØ!Ÿ[™[×6Ñ6ˆÔØ"&§+¡+×"DÑ"DˆÔØ!Ÿ[™[×6Ñ6ˆÔð Ÿ+™+×8Ñ8ˆŒØŸ;™;×:Ñ:ˆÔô #Ÿ^š^¨A¨q°$×2BÑ2BÓCˆÔð *.¯©×)NÑ)NˆÔ&Ø"&§+¡+×"@Ñ"@ˆÔð '+§k¡k×&HÑ&HˆÔ#Ø"&§+¡+×"@Ñ"@ˆÔØ×&×&Ð&¨4×+BÑ+BÒ+JÜÐIÓJÐJð (,§{¡{×'JÑ'JˆÔ$Ø#'§;¡;×#BÑ#BˆÔ Ø×'×'Ð'¨D×,DÑ,DÒ,LÜÐJÓKÐKñ -MÑ'r&   c          	      óª   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[S[ P                  S[ P                  S[ P                  3,          /# )r   rM   rO   r   )r5   rK   r   )r!   r"   s   "€r#   r$   rm   Ý   sM   ø€ ÷ f1ñ f1¡§¡ð f1±u·|±|ð f1ÉÉeÏlÉlÑ\a×\hÑ\hÑjo×jvÑjvÐNvÕHwñ f1r&   c           
     ó¦  € \        VRR.4       VP                  w  r4VP                  pV P                  '       d÷   \	        V P
                  \        4      '       d×   V P
                  P                  R8X  d<   V P                  W P
                  P                  V P
                  P                  4      pM€V P
                  P                  R8X  df   V P                  VV P
                  P                  V P
                  P                  V P
                  P                  V P
                  P                  4      p\        V4      p\        P                   ! Wf.V\        P"                  R7      pV^8  d   WqV3# \        P$                  ! \        P&                  ! Wf.V\        P"                  R7      ^R7      pV P(                  '       d   V P+                  W4      p\        P,                  ! V4      p	\        P.                  ! V	^ ,          R,          V	^,          R,          .R4      p
WR,          ,          pWR,          ,          pV P0                  P3                  V4      R,          pVR,          V,          VR,          ^V,
          ,          ,           pVR,          V,          VR,          ^V,
          ,          ,           p\        P4                  ! V^ V^,
          R	7      p\        P4                  ! V^ V^,
          R	7      p\        P6                  ! \        P8                  ! VP3                  \        P:                  4      VP3                  \        P:                  4      ,
          ^,          RR
7      4      pVV^,          V^,          ,           R,          ,          p\        V4      pRpVV8”  Ed   \<        P>                  ! VV,          4      p. p\A        V4       FË  pVV^,
          8w  d`   VVV,          V^,           V,          1R3,          pVVV,          V^,           V,          1R3,          pVVV,          V^,           V,           pM4VVV,          R1R3,          pVVV,          R1R3,          pVVV,          R pV PC                  VVVW4VV4      pVPE                  V4       KÍ  	  \        P.                  ! V^ 4      pMV PC                  VVVW4VV4      p\        PF                  ! VRR
7      V PH                  8„  pV PJ                  ^ 8”  dh   \        P8                  ! VV PH                  8„  RR
7      P3                  VPL                  4      V PN                  ,          pVV PJ                  8¬  p VV ,          pV
V,          p!^VV!R,          V!R,          3&   ^VV!R,          V!R,          3&   V PP                  '       d&   \        V!4      ^ 8”  d   V PS                  WW#WE4      w  rWqV3# )zPerform line segment detection.rT   rU   ÚglobalÚlocal©r6   Údtype©ÚdiagonalNºNNN©ÚminÚmax©Údimç      à?i'  ©.Néÿÿÿÿ©r‹   r   ©r‹   é   ©r‹   :r   r–   N©r‹   :r–   r   N)*r   Úshaper6   r}   r+   r~   r   ÚmodeÚrefine_heatmapÚratioÚvalid_threshÚrefine_heatmap_localÚ
num_blocksÚoverlap_ratioÚlenr5   ÚzerosÚint32ÚtriuÚonesr{   Úcandidate_suppressionÚwhereÚcatrz   ÚtoÚclampÚsqrtÚsumÚfloat32ÚmathÚceilÚrangeÚdetect_local_maxrY   Úmeanro   rq   rˆ   rp   r€   Úrefine_junction_perturb)"r=   rM   rO   rT   rU   r6   Únum_junctionsÚline_map_predÚcandidate_mapÚcandidate_indicesÚcandidate_index_mapÚcandidate_junc_startÚcandidate_junc_endÚsamplerÚcand_samples_hÚcand_samples_wÚcand_hÚcand_wÚsegments_lengthÚnormalized_seg_lengthÚnum_candÚ
group_sizeÚnum_iterÚsampled_feat_lstÚiter_idxÚcand_h_Úcand_w_Únormalized_seg_length_Úsampled_feat_Úsampled_featÚdetection_resultsÚinlier_ratioÚdetection_results_inlierÚdetected_junc_indexess"   &&&                               r#   rX   Ú!LineSegmentDetectionModule.detectÝ   s%  € ä˜7 S¨# JÔ/Ø�}‰}‰ˆØ×!Ñ!ˆð ×&×&Ð&¬:°d×6MÑ6MÔO_×+`Ò+`Ø×&Ñ&×+Ñ+¨xÔ7Ø×-Ñ-Ø×4Ñ4×:Ñ:¸D×<SÑ<S×<`Ñ<`ó‘ð ×(Ñ(×-Ñ-°Ô8Ø×3Ñ3ØØ×+Ñ+×6Ñ6Ø×+Ñ+×9Ñ9Ø×+Ñ+×1Ñ1Ø×+Ñ+×8Ñ8ó�ô ˜I›ˆÜŸš ]Ð$BÈ6ÔY^×YdÑYdÔeˆð ˜1ÔØ ¨WÐ4Ð4ô Ÿ
š
Ü�JŠJ˜Ð5¸fÌEÏKÉKÔXÐcdô
ˆð
 ×)×)Ð)Ø ×6Ñ6°yÓPˆMô "ŸKšK¨Ó6ÐÜ#ŸišiÐ):¸1Õ)=¸iÕ)HÐJ[Ð\]ÕJ^Ð_hÕJiÐ(jÐlnÓoÐð  )¸TÕ)BÕCÐØ&¸4Õ'@ÕAÐð ×$Ñ$×'Ñ'¨Ó/°Õ5ˆØ-¨fÕ5¸Õ?ÐBTÐU[ÕB\Ð`aÐdkÕ`kÕBlÕlˆØ-¨fÕ5¸Õ?ÐBTÐU[ÕB\Ð`aÐdkÕ`kÕBlÕlˆô —’˜^°¸¸A½Ô>ˆÜ—’˜^°¸¸A½Ô>ˆô  Ÿ*š*Ü�IŠIÐ+×.Ñ.¬u¯}©}Ó=Ð@R×@UÑ@UÔV[×VcÑVcÓ@dÕdÐijÕjÐprÔsó
ˆð !0°Q¸µT¸aÀ½dµOÈÕ3KÕ LÐô �v“;ˆØˆ
Ø�jÕ Ü—y’y ¨JÕ!6Ó7ˆHØ!ÐÜ! (žO�Ø˜x¨!�|Ô+Ø$ X°
Õ%:¸hÈ½lÈjÕ=XÐ%XÐZ[Ð%[Õ\�GØ$ X°
Õ%:¸hÈ½lÈjÕ=XÐ%XÐZ[Ð%[Õ\�GØ-BÀ8ÈjÕCXÐ\dÐghÕ\hÐlvÕ[vÐ-wÑ*à$ X°
Õ%:Ñ%<¸aÐ%?Õ@�GØ$ X°
Õ%:Ñ%<¸aÐ%?Õ@�GØ-BÀ8ÈjÕCXÐCZÐ-[Ð*Ø $× 5Ñ 5°g¸wÈÐQRÐWmÐouÓ v�Ø ×'Ñ'¨Ö6ñ ,ô !Ÿ9š9Ð%5°qÓ9‰Là×0Ñ0°¸&À&È!ÐPeÐgmÓnˆLô "ŸJšJ |¸Ô<¸t×?QÑ?QÑQÐð ×Ñ Ô!Ü Ÿ9š9 \°D×4FÑ4FÑ%FÈBÔO×RÑRÐSZ×S`ÑS`ÓaÐdh×dtÑdtÕtˆLØ'3°t×7IÑ7IÑ'IÐ$Ø 1Ð4LÕ LÐð !4Ð4EÕ FÐØRSˆÐ+¨DÕ1Ð3HÈÕ3NÐNÑOØRSˆÐ+¨DÕ1Ð3HÈÕ3NÐNÑOð ×'×'Ð'¬CÐ0EÓ,FÈÔ,JØ'+×'CÑ'CÀIÐ^eÐjkÓ'tÑ$ˆIà¨Ð0Ð0r&   c                óZ   <€ V ^8„  d   QhRS[ P                  RS[RS[RS[ P                  /# )r   rO   rœ   r�   r   )r5   rK   Úfloat)r!   r"   s   "€r#   r$   rm   E  s5   ø€ ÷ >ñ >¡e§l¡lð >¹5ð >ÑV[ð >Ñgl×gsÑgsñ >r&   c                ó"  € WV8„  ,          p\         P                  ! VRR7      ^ ,          p\        P                  ! VP                  ^ ,          V,          4      p\         P
                  ! VRV 4      p\         P                  ! W,          RRR7      # )z!Global heatmap refinement method.T)Ú
descendingNç        ç      ð?rŒ   )r5   Úsortr®   r¯   r™   r²   rª   )r=   rO   rœ   r�   Úheatmap_valuesÚsorted_valuesÚ	top10_lenÚmax20s   &&&&    r#   r›   Ú)LineSegmentDetectionModule.refine_heatmapE  sn   € ð !¨<Ñ!7Õ8ˆÜŸ
š
 >¸dÔCÀAÕFˆÜ—I’I˜m×1Ñ1°!Õ4°uÕ<Ó=ˆ	Ü—
’
˜=¨¨)Ð4Ó5ˆÜ�{Š{˜7�?°¸Ô=Ð=r&   c                óf   <€ V ^8„  d   QhRS[ P                  RS[RS[RS[RS[RS[ P                  /# )r   rO   rŸ   r    rœ   r�   r   )r5   rK   ÚintrÒ   )r!   r"   s   "€r#   r$   rm   N  sW   ø€ ÷ !Iñ !Iá—‘ð!Iñ ð!Iñ ð	!Iñ
 ð!Iñ ð!Iñ 
�‰ñ!Ir&   c                óÐ  € VP                   w  rg^V,
          p\        V^V^,
          V,          ,           ,          4      p	\        V^V^,
          V,          ,           ,          4      p
\        P                  ! VP                   \        P                  VP
                  R7      p\        P                  ! VP                   \        P                  VP
                  R7      p\        V4       FÞ  p\        V4       FÌ  p\        WÙ,          V,          4      p\        Wê,          V,          4      pWÒ^,
          8  d	   Wù,           MTpWâ^,
          8  d
   VV
,           MTpWV1VV13,          pVP                  4       V8”  d   V P                  VWER7      pWÏV1VV13;;,          V,          uu&   W¿V1VV13;;,          ^,          uu&   KÎ  	  Kà  	  \        P                  ! WË,          RRR7      # )z Local heatmap refinement method.)rˆ   r6   )r�   rÖ   rÕ   )rŽ   r�   )r™   Úroundr5   r¢   rÞ   r6   rÒ   r°   rŽ   r›   rª   )r=   rO   rŸ   r    rœ   r�   rT   rU   Úincrease_ratioÚh_blockÚw_blockÚ	count_mapÚheatmap_outputÚh_idxÚw_idxÚh_startÚw_startÚh_endÚw_endÚ
subheatmaps   &&&&&&              r#   rž   Ú/LineSegmentDetectionModule.refine_heatmap_localN  s‚  € ð �}‰}‰ˆØ˜]Õ*ˆÜ˜˜Q *¨q¥.°NÕ!BÕBÕCÓDˆÜ˜˜Q *¨q¥.°NÕ!BÕBÕCÓDˆô —K’K §¡´U·Y±YÀwÇ~Á~ÔVˆ	ÜŸš W§]¡]¼%¿+¹+ÈgÏnÉnÔ]ˆÜ˜:Ö&ˆEÜ˜zÖ*�ä ¥°.Õ @ÓA�Ü ¥°.Õ @ÓA�Ø-2À!µ^Ô-C˜Ö)È�Ø-2À!µ^Ô-C˜ 'Ö)È�à$¨U ]°G¸E°MÐ%AÕB�
Ø—>‘>Ó# lÔ2Ø!%×!4Ñ!4°ZÀÐ!4Ó!b�Jð  u˜}¨g°e¨mÐ;×<À
ÕJÓ<Ø %˜-¨°¨Ð6×7¸1Õ<Õ7ó +ñ 'ô �{Š{˜>Õ5¸3ÀCÔHÐHr&   c                óh   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  /# )r   rM   r¶   r   ©r5   rK   )r!   r"   s   "€r#   r$   rm   q  s1   ø€ ÷ -ñ -©u¯|©|ð -ÉEÏLÉLð -Ñ]b×]iÑ]iñ -r&   c                ó@  € V P                   p\        P                  ! \        P                  ! V^R7      VR,          ,
          ^,          RR7      R,          p\        P                  ! \        P
                  ! V^R7      4      pV^ ,          pV^,          pWR3,          pWR3,          p	WFV3,          p
W˜,
          \        P                  ! W˜,
          RR7      R,          ,          pVR,          VP                  ^R7      ,
          p\        P                  ! VRR7      p\        P                  ! RWËR,          4      V
R	,          ,          pV^ 8¬  V^8*  ,          p\        P                  ! \        P                  ! RWËR,          4      VR,          ,          4      pVR,          \        P                  ! V4      ,          pVV8*  pVV,          p\        V4      p\        P                  ! V^^.R7      pVVR
,          \        P                  ! ^ V4      V3,          P                  \        P                  4      ,          pVVR
,          \        P                  ! ^ V4      V3,          P                  \        P                  4      ,          pV^ 8„  p^ W&V,          VV,          3&   V# )z:Suppress overlapping long lines in the candidate segments.r�   r‘   r‰   r‹   zbij,bjk->bik)N.r“   r’   ).NN©.r   )r|   r5   r¬   Ú	unsqueezer§   r¤   ÚnormÚeinsumÚacosÚsinr¡   Úaranger©   rÞ   )r=   rM   r¶   Údist_toleranceÚline_dist_mapÚseg_indexesÚstart_point_idxsÚend_point_idxsÚstart_pointsÚ
end_pointsÚ
line_distsÚdir_vecsÚ	cand_vecsÚcand_vecs_normÚprojÚ	proj_maskÚcand_anglesÚ
cand_distsÚjunc_dist_maskÚ	junc_maskÚnum_segsÚjunc_countsÚ
final_masks   &&&                    r#   r¦   Ú0LineSegmentDetectionModule.candidate_suppressionq  s.  € ð ×0Ñ0ˆô Ÿ	š	¤5§?¢?°9À!Ô#DÀyÐQZÕG[Õ#[Ð`aÕ"aÐgiÔjÐnqÕqˆô —k’k¤%§*¢*¨]ÀQÔ"GÓHˆØ& q�>ÐØ$ Q�ˆØ °1Ð!4Õ5ˆØ¨qÐ0Õ1ˆ
ð #°^Ð#CÕDˆ
ð Õ-´·²¸JÕ<UÐ[]Ô1^Ð_hÕ1iÕiˆà˜iÕ(¨<×+AÑ+AÀaÐ+AÓ+HÕHˆ	ÜŸš I°2Ô6ˆä�|Š|˜N¨IÀ	Õ7JÓKÈjÐYhÕNiÕiˆà˜Q‘Y 4¨1¡9Õ-ˆ	Ü—j’jÜ�LŠL˜¨¸YÕ4GÓHÈ>ÐZcÕKdÕdó
ˆð $ IÕ.´·²¸;Ó1GÕGˆ
Ø# ~Ñ5ˆØ" YÕ.ˆ	ô Ð'Ó(ˆÜ—i’i 	°°1¨vÔ6ˆØ�y Õ(¬¯ª°a¸Ó)BÐDTÐ)TÕU×XÑXÔY^×YbÑYbÓcÕcˆØ�y Õ(¬¯ª°a¸Ó)BÀNÐ)RÕS×VÑVÔW\×W`ÑW`ÓaÕaˆð ! 1‘_ˆ
ØRSˆ zÕ2°NÀ:Õ4NÐNÑOàÐr&   c                óÒ   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  RS[RS[RS[ P                  RS[S[ P                  S[ P                  3,          /# )r   rM   r_   rO   rT   rU   r6   r   )r5   rK   rÞ   r6   r   )r!   r"   s   "€r#   r$   rm      s|   ø€ ÷ N+ñ N+á—<‘<ðN+ñ —,‘,ðN+ñ —‘ð	N+ñ
 ðN+ñ ðN+ñ —‘ðN+ñ 
‰u�|‰|™UŸ\™\Ð)Õ	*ñN+r&   c                ó^  € \        V P                  \        4      '       g"   \        R\	        V P                  4       24      hV P                  P
                  pV P                  P                  pV^,
          ^,          p	\        P                  ! V) V	,          W‰^,           ,          VVR7      p
\        P                  ! WªWª.RR7      w  r¼rÞ\        P                  ! VR	,          VR	,          VR	,          VR	,          .R
4      pVP                  R
^^4      p\        P                  ! \        P                  ! V^R7      4      pVV^ ,          ,          pVV^,          ,          p\        P                  ! VV.^4      pVP                  ^R7      VR,          ,           p\        P                   ! VR,          ^ V^,
          R7      VR&   \        P                   ! VR,          ^ V^,
          R7      VR&   . p\#        V4      p\%        V4       EF9  pVV,          pVR,          pVR,          pV P&                  P)                  V4      R,          pVR,          V,          VR,          ^V,
          ,          ,           pVR,          V,          VR,          ^V,
          ,          ,           p\        P                   ! V^ V^,
          R7      p\        P                   ! V^ V^,
          R7      p V P+                  VVV 4      p!\        P,                  ! V!R
R7      p"\        P.                  ! V"4      p#VP1                  VV#,          R,          4       EK<  	  \        P                  ! V^ 4      p$\        P                  ! V$R,          V$R,          .^ 4      p%\        P2                  ! V%^ R7      p%V P5                  V%V$4      p&V%V&3# )z5Refine the line endpoints in a similar way as in LSD.zFExpected to have dataclass of type JunctionRefineCfg for junction.Got )ÚstartÚendÚstepr6   Úij)ÚindexingNr‰   r�   rŒ   r’   r“   rñ   ).r–   r”   r•   r—   r˜   )r‹   r   r‹   )r‹   r–   r‹   )r+   r�   r   Ú	TypeErrorÚtypeÚnum_perturbsÚperturb_intervalr5   r÷   Úmeshgridr¨   Úviewr§   r¤   Ústackrò   rª   r¡   r°   rz   r©   Údetect_bilinearr²   ÚargmaxrY   ÚuniqueÚsegments_to_line_map)'r=   rM   r_   rO   rT   rU   r6   r  r  Úside_perturbsÚperturb_vecÚh1_gridÚw1_gridÚh2_gridÚw2_gridÚperturb_tensorÚperturb_tensor_flatÚdetected_seg_indexesrý   rþ   rQ   Úline_segment_candidatesÚrefined_segment_lstÚnum_segmentsÚidxÚsegmentr¹   rº   r»   r¼   r½   r¾   r¿   Úsegment_featÚsegment_resultsÚmax_idxÚrefined_segmentsÚjunctions_newÚline_map_news'   &&&&&&&                                r#   r³   Ú2LineSegmentDetectionModule.refine_junction_perturb   sN  € ô ˜$×2Ñ2Ô4E×FÒFÜðÜ˜D×4Ñ4Ó5Ð6ð8óð ð ×/Ñ/×<Ñ<ˆØ×3Ñ3×DÑDÐØ%¨Õ)¨aÕ/ˆô —l’lØ#Ð# mÕ3Ø °AÕ$5Õ6Ø!Øô	
ˆô .3¯^ª^Ø {Ð@È4ô.
Ñ*ˆ˜'ô Ÿš G¨IÕ$6¸À	Õ8JÈGÐT]ÕL^Ð`gÐhqÕ`rÐ#sÐuwÓxˆØ,×1Ñ1°"°a¸Ó;Ðô  %Ÿ{š{¬5¯:ª:°hÈÔ+KÓLÐØ Ð!5°aÕ!8Õ9ˆØÐ3°AÕ6Õ7ˆ
ÜŸš \°:Ð$>ÀÓBˆà"/×"9Ñ"9¸aÐ"9Ó"@ÐCVÐW[ÕC\Õ"\Ðä*/¯+ª+Ð6MÈfÕ6UÐ[\ÐbcÐfgÕbgÔ*hÐ Ñ'Ü*/¯+ª+Ð6MÈfÕ6UÐ[\ÐbcÐfgÕbgÔ*hÐ Ñ'ð !ÐÜ˜=Ó)ˆÜ˜×&ˆCØ-¨cÕ2ˆGà#*¨4¥=Ð Ø!(¨¥Ðð ×(Ñ(×+Ñ+¨FÓ3°DÕ9ˆGØ1°&Õ9¸GÕCÐFXÐY_ÕF`ÐdeÐhoÕdoÕFpÕpˆNØ1°&Õ9¸GÕCÐFXÐY_ÕF`ÐdeÐhoÕdoÕFpÕpˆNô —[’[ °Q¸AÀ½EÔBˆFÜ—[’[ °Q¸AÀ½EÔBˆFð  ×/Ñ/°¸ÀÓHˆLÜ#Ÿjšj¨¸2Ô>ˆOÜ—l’l ?Ó3ˆGØ×&Ñ& w¨wÕ'7¸Õ'=×>ñ' 'ô, !Ÿ9š9Ð%8¸!Ó<Ðô Ÿ	š	Ð#3°GÕ#<Ð>NÈwÕ>WÐ"XÐZ[Ó\ˆÜŸš ]¸Ô:ˆØ×0Ñ0°Ð@PÓQˆà˜lÐ*Ð*r&   c                óh   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  /# )r   rM   Úsegmentsr   rï   )r!   r"   s   "€r#   r$   rm   ð  s1   ø€ ÷ ñ ©e¯l©lð ÁeÇlÁlð ÑW\×WcÑWcñ r&   c                óv  € \        V4      p\        P                  ! W3.VP                  R7      p\        P                  ! \        P
                  ! VR,          VR,          8H  ^R7      4      w  rV\        P                  ! \        P
                  ! VR,          VR,          8H  ^R7      4      w  rW^WFV3&   ^WGV3&   V# )z)Convert the list of segments to line map.©r6   Nr�   )r‹   Nr   )r‹   Nr–   )r¡   r5   r¢   r6   r§   Úall)r=   rM   r5  r´   r_   r`   Ú	idx_junc1Ú	idx_junc2s   &&&     r#   r  Ú/LineSegmentDetectionModule.segments_to_line_mapð  s�   € ô ˜I›ˆÜ—;’; Ð=Ài×FVÑFVÔWˆô —{’{¤5§9¢9¨Y°t­_ÀÈÕ@TÑ-TÐZ[Ô#\Ó]‰ˆÜ—{’{¤5§9¢9¨Y°t­_ÀÈÕ@TÑ-TÐZ[Ô#\Ó]‰ˆð *+ˆ˜IÐ%Ñ&Ø)*ˆ˜IÐ%Ñ&àˆr&   c                ó‚   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  RS[ P                  /# )r   rO   r¾   r¿   r   rï   )r!   r"   s   "€r#   r$   rm      s?   ø€ ÷ 
ñ 
¡u§|¡|ð 
¹U¿\¹\ð 
ÑSX×S_ÑS_ð 
Ñdi×dpÑdpñ 
r&   c                óÈ  € \         P                  ! V4      P                  \         P                  4      pV^,           P	                  VP
                  ^ ,          ^,
          R7      p\         P                  ! V4      P                  \         P                  4      pV^,           P	                  VP
                  ^,          ^,
          R7      pW$,
          pW6,
          p	WV3,          ^V,
          ,          ^V	,
          ,          WV3,          ^V,
          ,          V	,          ,           WV3,          V,          ^V	,
          ,          ,           WV3,          V,          V	,          ,           # )aÀ  Detect by bilinear sampling.

The reference SOLD2 implementation takes ``ceil`` as the second neighbour, so at an integer coordinate, where
``floor`` and ``ceil`` coincide, both weights of that axis are 0 and the sample scores 0. kornia takes
``floor + 1`` (clipped at the image border) instead: an integer sample keeps its pixel value, and the junction
refinement no longer moves horizontal and vertical segments off the pixels that support them.
)rŽ   )r5   Úfloorr©   Úlongrª   r™   )
r=   rO   r¾   r¿   Úcand_h_floorÚcand_h_ceilÚcand_w_floorÚcand_w_ceilÚh_weightÚw_weights
   &&&&      r#   r  Ú*LineSegmentDetectionModule.detect_bilinear   s  € ô —{’{ 6Ó*×-Ñ-¬e¯j©jÓ9ˆØ# aÕ'×.Ñ.°7·=±=ÀÕ3CÀaÕ3GÐ.ÓHˆÜ—{’{ 6Ó*×-Ñ-¬e¯j©jÓ9ˆØ# aÕ'×.Ñ.°7·=±=ÀÕ3CÀaÕ3GÐ.ÓHˆØÕ(ˆØÕ(ˆð  ,Ð.Õ/°1°xµ<Õ@ÀAÈÅLÕQØ KÐ/Õ0°A¸µLÕAÀHÕLõMà <Ð/Õ0°8Õ;¸qÀ8½|ÕLõMð  ;Ð.Õ/°(Õ:¸XÕEõFð	
r&   c                óÂ   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  RS[RS[RS[ P                  RS[ P                  RS[ P                  /# )	r   rO   r¾   r¿   rT   rU   rÁ   r6   r   )r5   rK   rÞ   r6   )r!   r"   s   "€r#   r$   rm     ss   ø€ ÷ 6!ñ 6!á—‘ð6!ñ —‘ð6!ñ —‘ð	6!ñ
 ð6!ñ ð6!ñ  %Ÿ|™|ð6!ñ —‘ð6!ñ 
�‰ñ6!r&   c           	     ó
  € RV P                   V,          ,           p\        P                  ! VR,          V P                  RR7      p\        P                  ! VR,          VR,          .R4      p	\        P
                  ! V	4      p
\        P                  ! \        ^V P                  ,          ^,           4      \        ^V P                  ,          ^,           4      .VR7      p\        P                  ! V P                  V P                  ..V\        P                  R7      p\        P                  ! V^ 8¬  4      w  rÞ\        P                  ! VR,          VR,          .R4      p\        P                  ! \        P                  ! Wü,
          ^,          RR7      4      pVVV P                  8*  R3,          pWðP                  ,
          p\        P                  ! V
^R7      VR	,          ,           p\        P                  ! \        P                  ! \        P                  ! V	^R7      V,
          ^,          RR7      4      pVVR,          8  p\        P                  ! VR
,          ^ V^,
          R7      P!                  \        P"                  4      p\        P                  ! VR,          ^ V^,
          R7      P!                  \        P"                  4      p\        P                  ! VR,          VR,          .R4      pVVR
,          VR,          3,          pVVP!                  \        P                  4      ,          p\%        V4      ^ 8X  d$   \        P&                  ! ^ V P                  4      pV# \        P(                  ! VRR7      ^ ,          pV# )zDetect by local maximum search.r�   r7  r‡   r‹   rŒ   gÍ;fž æ?r’   r“   )NN)r‹   r‹   r‹   r   )r‹   r‹   r‹   r–   )rt   r5   Úrepeat_interleaverp   r¨   rà   r¢   rÞ   rs   Útensorr­   r§   r«   r¬   rò   rª   r©   r?  r¡   ÚemptyrŽ   )r=   rO   r¾   r¿   rT   rU   rÁ   r6   Údist_threshÚcand_pointsÚcand_points_roundÚ
patch_maskÚpatch_centerÚH_patch_pointsÚW_patch_pointsÚpatch_pointsÚpatch_center_distÚpatch_points_shiftedÚ
patch_distÚpatch_dist_maskÚpoints_HÚpoints_WÚpointsrË   Úsampled_feat_lmaxs   &&&&&&&&                 r#   r±   Ú+LineSegmentDetectionModule.detect_local_max  sÃ  € ð % t×'9Ñ'9Ð<QÕ'QÕQˆä×-Ò-¨k¸)Õ.DÀd×FVÑFVÐ\^Ô_ˆô —i’i ¨	Õ!2°F¸9Õ4EÐ FÈÓKˆÜ!ŸKšK¨Ó4Ðô —[’[Ü��T×,Ñ,Õ,¨qÕ0Ó1´3°q¸4×;RÑ;RÕ7RÐUVÕ7VÓ3WÐXÐagô
ˆ
ô —|’|Ø×%Ñ% t×'>Ñ'>Ð?Ð@ÈÔW\×WdÑWdô
ˆô */¯ª°ZÀ1±_Ó)EÑ&ˆÜ—y’y .°Õ";¸^ÈIÕ=VÐ!WÐY[Ó\ˆä!ŸJšJ¤u§y¢y°,Õ2MÐRSÕ1SÐY[Ô'\Ó]ÐØ#Ð$5¸×9PÑ9PÑ$PÐRSÐ$SÕTˆà#×&=Ñ&=Õ=ˆô  %ŸšÐ/@ÀaÔHÈ<ÐXbÕKcÕcÐÜ—Z’Z¤§	¢	¬5¯?ª?¸;ÈAÔ+NÐQeÕ+eÐjkÕ*kÐqsÔ tÓuˆ
Ø$ {°9Õ'=Ñ=ˆô —;’;Ð3°JÕ?ÀQÈAÐPQÍEÔR×UÑUÔV[×V`ÑV`ÓaˆÜ—;’;Ð3°JÕ?ÀQÈAÐPQÍEÔR×UÑUÔV[×V`ÑV`ÓaˆÜ—’˜H YÕ/°¸)Õ1DÐEÀrÓJˆð ˜v jÕ1°6¸*Õ3EÐEÕFˆà# o×&8Ñ&8¼¿¹Ó&GÕGˆÜˆ|Ó Ô!Ü %§¢¨A¨t×/?Ñ/?Ó @Ðð !Ð ô !&§	¢	¨,¸BÔ ?ÀÕ BÐà Ð r&   )r   ro   r~   rx   rq   r�   rt   rs   rv   r|   rp   rz   r{   r}   r€   )N)çš™™™™™É?ç{®Gáz„?)é   r‘   r]  gü©ñÒMb`?)rc   rd   re   rf   rg   r.   rX   r›   rž   r¦   r³   r  r  r±   rh   ri   )r"   s   @r#   r9   r9   ›   sq   ø‡ € ñ÷4%Lò %L÷Nf1ð f1÷P>ò >÷!Iò !I÷F-ð -÷^N+ð N+÷`ð ÷ 
ð 
÷06!ö 6!r&   r9   c                óx   € V ^8„  d   QhR\         P                  R\         P                  R\         P                  /# )r   rM   r_   r   rï   )r!   s   "r#   r$   r$   Q  s2   € ÷ Hñ H¤E§L¡Lð H¼E¿L¹Lð HÌUÏ\É\ñ Hr&   c                ó¤   € \         P                  ! \         P                  ! V4      4      w  r#\         P                  ! W,          W,          .^4      # )zHConvert a junction connectivity map to a Nx2x2 torch.Tensor of segments.)r5   r§   r¤   r  )rM   r_   Ú	junc_loc1Ú	junc_loc2s   &&  r#   rZ   rZ   Q  s8   € ä Ÿ;š;¤u§z¢z°(Ó';Ó<Ñ€IÜ�;Š;˜	Õ,¨iÕ.BÐCÀQÓGÐGr&   c          
      ó|   € V ^8„  d   QhR\         P                  R\        R\        R\        R\         P                  /# )r   ÚprobÚdistÚprob_threshÚtop_kr   )r5   rK   rÒ   rÞ   )r!   s   "r#   r$   r$   W  sB   € ÷ $fñ $fœEŸL™Lð $f´ð $fÄEð $fÔY\ð $fÔej×eqÑeqñ $fr&   c                óˆ  € \         P                  ! \         P                  ! W8¬  4      R4      P                  4       p\	        V4      ^ 8X  d   V# \
        P                  ! V4      R,           p\         P                  ! WE^,          ,
          WE^,          ,           .^4      pW V8¬  ,          p\        WgR4      pWH,          pV^ 8”  d   \        \	        V4      V4      p	VRV	 pV\         P                  ! VR,          P                  4       V P                  R,          ,          VR,          P                  4       ,           4      ,          # )a5  Extract junctions from a probability map, apply NMS, and extract the top k candidates.

The strongest junction is kept first and suppresses every other junction within ``dist`` pixels of it along both
axes, ``dist`` itself included, like ``super_nms`` of the SOLD2 reference implementation. The ``top_k`` strongest
survivors are returned in raster order (by row, then by column), so the first endpoint of a line segment built
from the junction indices is the one with the smaller row or, on the same row, the smaller column.

Args:
    prob: junction probability map with shape ``(H, W)``.
    dist: suppression distance in pixels.
    prob_thresh: minimum probability of a junction.
    top_k: number of junctions to keep; ``0`` keeps all.

Returns:
    Junction locations in ``(row, col)`` order with shape ``(N, 2)``.
r‘   rÕ   Nr“   r”   r•   )r5   r  r§   rÒ   r¡   r®   r>  r¨   r   r�   Úargsortr?  r™   )
re  rf  rg  rh  rM   ÚsideÚboxesÚscoresÚ
remainingsÚks
   &&&&      r#   rW   rW   W  sû   € ô$ —’œEŸKšK¨Ñ(;Ó<¸bÓA×GÑGÓI€IÜ
ˆ9ƒ~˜ÔØÐô �:Š:�dÓ˜cÕ!€DÜ�IŠI�y¨!¥8Õ+¨YÀ½Õ-AÐBÀAÓF€EØ˜+Ñ%Õ&€FÜ�U CÓ(€JØÕ%€Ið ˆq„yÜ”�I“ Ó&ˆØ˜b˜q�Mˆ	ð ”U—]’] 9¨T¥?×#7Ñ#7Ó#9¸D¿J¹JÀr½NÕ#JÈYÐW[Í_×MaÑMaÓMcÕ#cÓdÕeÐer&   c                óx   € V ^8„  d   Qh/ ^ \         9   d&   \        \        \        \        ,          3,          ;R&   # )r   r4   )Ú__conditional_annotations__r   r    r   )r!   s   "r#   r$   r$      s%   € ‡�÷  Ò „dŒ3””S•	ˆ>ÕÑ ò r&   )r^  r   )$rq  r®   Útypingr   r   r   r   r   r   r	   r5   r
   Úkornia.core.checkr   Úkornia.core.downloadr   r   Úkornia.core.utilsr   r   Úkornia.feature.sold2.structuresr   r   r   r   Úkornia.geometry.bboxr   Ú	backbonesr   r4   ÚModuler   r9   rZ   rW   r$   )rq  s   @r#   Ú<module>rz     s…   øð÷$ €ß @× @Ñ @ã Ý å 0ß Aß Bß mÓ mÝ $å à€Ó á
ˆ7Ð)Ó*ØCð€€[Ñ ôq�R—Y‘Yô q÷hs!ñ s!õlH÷$fñ $fr&   