Merging lines given end points

asked 2020-11-22 07:03:28 -0500

sachet12345 gravatar image

updated 2020-11-22 09:17:03 -0500

df contains all lines. (x1,y1), (x2,y2) are the endpoints. hl contains the horizontal lines only. I want to merege all horizontal lines if the gap falls under certain threshold i.e hgap.

import cv2
import pandas as pd
import numpy as np

#Read gray image
img = cv2.imread(r"C:\Users\Sachet\Desktop\LineDetection\lh.jpg",0)
_,binary = cv2.threshold(img,0,255,cv2.THRESH_OTSU)

# binary = ~binary

# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 1))
# imgtemp = cv2.erode(binary, kernel, iterations=3)
# imgtemp = ~imgtemp

#Create default parametrization LSD
lsd = cv2.createLineSegmentDetector(0)

#Detect lines in the image
lines = lsd.detect(binary)[0] #Position 0 of the returned tuple are the detected lines
linesint = lines.astype(int)
linescolumn = lines[:,0,:]
linescolumnint = linescolumn.astype(int)

#Draw detected lines in the image
# drawn_img = lsd.drawSegments(binary,lines)

# for x1,y1,x2,y2 in linescolumnint:
#     start_point = (x1,y1)
#     end_point = (x2,y2)
#     drawn_img = cv2.line(img, start_point, end_point, (0,0,255), 2)

cv2.imwrite(r'C:\Users\Sachet\Desktop\LineDetection\testnow.jpeg', drawn_img)

df = pd.DataFrame(linescolumnint, columns = ['x1', 'y1', 'x2', 'y2'])

#horizontal gap close
hgap = 10
hl=df.loc[df['y1'] == df['y2']]

for i in range(len(hl)):
    for j in range (len(hl)):
        if(hl.loc[i,'y1']==hl.loc[j,'y1'] and hl.loc[i,'x2']-hl.loc[j,'x1']<=hgap):
            hl.loc[i,'x2'] = hl.loc[j,'x2']
            hl.loc[j,'x1'] = hl.loc[i,'x1']

I am getting KeyError: 0. Is there any library that can help me do this? Or can someone guide me how to handle changing of the dataframe inside for loops.

Thank you.

Traceback (most recent call last):

File "C:\Users\Sachet\Desktop\LineDetection\", line 51, in <module> if(hl.loc[i,'y1']==hl.loc[j,'y1'] and hl.loc[i,'x2']-hl.loc[j,'x1']<=hgap):

File "C:\Users\Sachet\anaconda3\envs\opencv3\lib\site-packages\pandas\core\", line 1418, in __getitem__ return self._getitem_tuple(key)

File "C:\Users\Sachet\anaconda3\envs\opencv3\lib\site-packages\pandas\core\", line 805, in _getitem_tuple return self._getitem_lowerdim(tup)

File "C:\Users\Sachet\anaconda3\envs\opencv3\lib\site-packages\pandas\core\", line 929, in _getitem_lowerdim section = self._getitem_axis(key, axis=i)

File "C:\Users\Sachet\anaconda3\envs\opencv3\lib\site-packages\pandas\core\", line 1850, in _getitem_axis return self._get_label(key, axis=axis)

File "C:\Users\Sachet\anaconda3\envs\opencv3\lib\site-packages\pandas\core\", line 160, in _get_label return self.obj._xs(label, axis=axis)

File "C:\Users\Sachet\anaconda3\envs\opencv3\lib\site-packages\pandas\core\", line 3737, in xs loc = self.index.get_loc(key)

File "C:\Users\Sachet\anaconda3\envs\opencv3\lib\site-packages\pandas\core\indexes\", line 2899, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key))

File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc

File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc

File "pandas/_libs/hashtable_class_helper.pxi", line ... (more)

edit retag flag offensive close merge delete


where are you getting the keyerror ? please show us the complete stacktrace, and the image you use above

also, which opencv version is it (the LineSegmentDetector was removed lately, due to patent issues)

we also probably cannot help you with pandas ..

berak gravatar imageberak ( 2020-11-22 07:40:03 -0500 )edit

I am using opencv3. lines are detected properly and saved in df. I just wanted to know what method should i use to achieve the merging based on gap. Also, LSD is returning a vector of floats for line. Shouldn't it be int as pixels are int?

sachet12345 gravatar imagesachet12345 ( 2020-11-22 09:18:19 -0500 )edit