Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Merging lines given end points

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


#Pre-processing
# 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.

Merging lines given end points

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


#Pre-processing
# 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\lsd.py", 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\indexing.py", line 1418, in __getitem__ return self._getitem_tuple(key)

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

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

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

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

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

File "C:\Users\Sachet\anaconda3\envs\opencv3\lib\site-packages\pandas\core\indexes\base.py", 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 992, in pandas._libs.hashtable.Int64HashTable.get_item

File "pandas/_libs/hashtable_class_helper.pxi", line 998, in pandas._libs.hashtable.Int64HashTable.get_item

KeyError: 0