How do I extract each road in terms of the pixel coordinates from Google Map Screenshot and place them into different lists?

Viewed 53

I'm working on a project related to road recognition from a standard Google Map view. Some navigation features will be added to the project later on.

I already extracted all the white pixels (representing road on the map) according to the RGB criteria. Also, I stored all the white pixel (roads) coordinates (2D) in one list named "all_roads". Now I want to extract each road in terms of the pixel coordinates and place them into different lists (one road in one list), but I'm lacking ideas.

I'd like to use Dijkstra's algorithm to calculate the shortest path between two points, but I need to create "nodes" on each road intersection. That's why I'd like to store each road in the corresponding list for further processing.

I hope someone could provide some ideas and methods. Thank you!

Note: The RGB criteria ("if" statements in "threshold" method) seems unnecessary for the chosen map screenshot, but it becomes useful in some other map screenshot with other road colours other than white. (NOT the point of the question anyway but I hope to avoid unnecessary confusion)

# Import numpy to enable numpy array
import numpy as np

# Import time to handle time-related task
import time

# Import mean to calculate the averages of the pixals
from statistics import mean

# Import cv2 to display the image
import cv2 as cv2

def threshold(imageArray):
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    Purpose:     Display a given image  with road in white according to pixel RGBs 
    Argument(s): A matrix generated from a given image.
    Return:      A matrix of the same size but only displays white and black.
    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    newAr = imageArray
    for eachRow in newAr:
        for eachPix in eachRow:
            if eachPix[0] == 253 and eachPix[1] == 242:
                eachPix[0] = 255
                eachPix[1] = 255
                eachPix[2] = 255
            else:
                pass
    return newAr

# Import the image
g1 = cv2.imread("1.png")

# fix the output image with resolution of 800 * 600
g1 = cv2.resize(g1,(800,600))

# Apply threshold method to the imported image
g2 = threshold(g1)

index = np.where(g2 == [(255,255,255)])

# x coordinate of the white pixels (roads)
print(index[1])

# y coordinate of the white pixels (roads)
print(index[0])

# Storing the 2D coordinates of white pixels (roads) in a list
all_roads = []
for i in range(len(index[0]))[0::3]:
    all_roads.append([index[1][i], index[0][i]])

#Display the modified image
cv2.imshow('g2', g2)
cv2.waitKey(0)
cv2.destroyAllWindows()

enter image description here

0 Answers
Related