[Python] Drawing out roof faces from satellite images with OpenCV

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to OpenCV and I am trying to make this output from that satellite image :

    [​IMG]

    But I am running into a couple issues. I first thought about using a hough transform. But it started drawing lines like this :

    [​IMG]

    img = cv2.imread('2 faced roof.png', cv2.IMREAD_GRAYSCALE)
    blur = cv2.GaussianBlur(img, (21,21), 3)
    cannyEdge = cv2.Canny(blur,50,100)
    for curLine in lines:
    rho,theta = curLine[0]
    dhat = np.array([[np.cos(theta)], [np.sin(theta)]])
    d = rho * dhat
    lhat = np.array([[-np.sin(theta)], [np.cos(theta)]])
    p1 = d + k*lhat
    p2 = d - k*lhat
    p1 = p1.astype(int)
    p2 = p2.astype(int)
    cv2.line(img, (p1[0][0], p1[1][0]), (p2[0][0], p2[1][0]), (255,255,255), 2)


    which is close but the lines don't make a perfect square and they pick up other lines from surrounding houses. So then I tried that:

    [​IMG]

    and it started picking up extra lines.

    ret, thresh = cv.threshold(gray,0,255,cv.THRESH_BINARY_INV+cv.THRESH_OTSU)

    kernel = np.ones((3,3),np.uint8)
    opening = cv.morphologyEx(thresh,cv.MORPH_OPEN,kernel, iterations = 2)

    # sure background area
    sure_bg = cv.dilate(opening,kernel,iterations=3)

    # Finding sure foreground area
    dist_transform = cv.distanceTransform(opening,cv.DIST_L2,5)
    ret, sure_fg = cv.threshold(dist_transform,0.17*dist_transform.max(),255,0)

    # Finding unknown region
    sure_fg = np.uint8(sure_fg)
    unknown = cv.subtract(sure_bg,sure_fg)

    ret, markers = cv.connectedComponents(sure_fg)
    markers = markers+1

    # mark unknown with zero
    markers[unknown==255] = 0
    markers = cv.watershed(img,markers)
    img[markers == -1] = [0,0,255]

    display_markers = cv.normalize(markers.astype('float32'), None, 0, 255, cv.NORM_MINMAX)
    display_markers = np.uint8(display_markers)


    But still no bueno. I am not sure how to make this work as I will need to figure out how to detect the roof faces and mark them out. If anyone knows how I could do this that would be greatly appreciated.

    Original Photo :

    [​IMG]

    Continue reading...

Compartilhe esta Página