I'm trying to export my houghlines as a shapefile, which works. But the output the lines in my shapefile are very different then the generated hough lines that are shown on the image.


The hough lines that are generated with the program

The output here is great, beacause I need the corner point of the building.

When I try to save the lines with Fiona, the output (in QGIS) becomes this:

enter image description here

Wich is a way less elegant result. How do i export the lines exactly like they are shown in the image?

My code:

import numpy as np
from numpy import array
import cv2
from shapely import ops
from shapely.geometry import LineString, MultiLineString, mapping
import fiona
from fiona.crs import from_epsg
#from PIL import Image

in_path  = 'D:\\Users\\PdeHaan\\Documents\\TestFase\\Python\\Data\\Test_kernel_error\\t2\\filtered_2871_10476_1_1_1_1.tif'
out_path = 'D:\\Users\\PdeHaan\\Documents\\TestFase\\Python\\Pictures\\hough_lines_TESTTTT.tif'

gray = cv2.imread(in_path)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
lines = cv2.HoughLinesP(image=edges,rho=3,theta=np.pi/180, threshold=200,lines=np.array([]), minLineLength=minLineLength,maxLineGap=30)

black = np.zeros_like(gray) 
a,b,c = lines.shape
for i in range(a):
    cv2.line(black, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 0, 0), 1, cv2.LINE_AA)


#print lines
arraylines = array(lines) 

arraylines = arraylines.astype(float) / 10.0
print arraylines

# Rotate image    
for arrayline in arraylines:
    for x1, y1, x2, y2 in arrayline:
        arrayline[:,0] = 50 - x1
        arrayline[:,1] = 50 - y1
        arrayline[:,2] = 50 - x2
        arrayline[:,3] = 50 - y2    

#print arraylines

# Georeference to CRS        
s_lines = []
for arrayline in arraylines:
    for x1, y1, x2, y2 in arrayline:
        x1 += (2870 * 50)
        y1 += (10474 * 50)
        x2 += (2870 * 50)
        y2 += (10474 * 50)

        s_line = LineString([(x1, y2), (x2, y1)])


multi_line = MultiLineString(s_lines)

merged_line = ops.linemerge(multi_line)

schema = {
    'geometry': 'LineString',
    'properties': {'id': 'int'}

with fiona.open('output_line_kernel.shp', 'w',  'ESRI Shapefile', schema,crs=from_epsg(28992)) as c:
            'geometry': mapping(merged_line),
            'properties': {'id': 0}})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.