Tags

, , , , , , , , , , , , ,

Arkwood, my decaying Belgian buddy, has been popping vitamins in order to arrest his decline. He has taken to baths of leeches, convinced the archaic treatment will cure him of ills. Yesterday, he said to me, ‘Find me the greenest city. I must live amongst the foliage!’ Well, as luck would have it, I had just the answer.

You see, in my filing cabinet lay the remains of some Python code. This code what I wrote, it could step through Google Street View. I documented it all in my post, Replay a Google Street View stroll.

To find the greenest city on the planet, there really is no need for emission monitoring or political assertions. Why not simply inspect each screenshot of Google Street View that my program yields as it walks the streets of a city, checking for trees and bushes.

OpenCV Changing Colorspaces provides the code, which I’ve worked into a handy class:

import ImageGrab
import numpy
import cv2

class City(object):

    # grab screenshot
    def _grab_screenshot(self):
        screenshot = ImageGrab.grab(bbox=(0,100,1500,850))
        return cv2.cvtColor(numpy.array(screenshot), cv2.COLOR_RGB2BGR)

    # get greenness of city
    def get_greenness(self):
 
        # grab screenshot
        img = self._grab_screenshot()

        # convert BGR to HSV
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

        # define range of green colour in HSV
        lower_green = numpy.array([38,0,0])
        upper_green = numpy.array([58,155,155])

        # threshold the HSV image to get only green colors
        mask = cv2.inRange(hsv, lower_green, upper_green)

        # return greenness
        return cv2.countNonZero(mask)

The first thing our get_greenness method does is grab a screenshot of Google Street View, which is rendered in a browser on my PC. Remember, the code from my previous post takes care of strolling through Google Street View, so all we need do is hook our class into the process so that it can grab a screenshot of each step!

Once we have our screenshot, we convert it to HSV format, which will work well for detecting the greenness in the screenshot.

We define the range of colours that constitute green. Stackoverflow provided some useful detail on adjusting the HSV values I obtained via GIMP to OpenCV.

Finally, we obtain a mask of our green colours in the screenshot and return their pixel count. The higher the pixel count, the more plants the street has!

Okay, let’s find out what city on the planet is greenest. We’ll start with Hong Kong, which, yes, is not a city but an administrative region:

greencities_hongkong_screenshot_1
greencities_hongkong_postmask_1

greencities_hongkong_screenshot_2
greencities_hongkong_postmask_2

greencities_hongkong_screenshot_3
greencities_hongkong_postmask_3

Twenty steps through the Wan Chai District of Hong Kong shows a good deal of foliage and an average green score of 122,555.

Cool. What about New York?

greencities_newyork_screenshot_1
greencities_newyork_postmask_1

greencities_newyork_screenshot_2
greencities_newyork_postmask_2

greencities_newyork_screenshot_3
greencities_newyork_postmask_3

Twenty steps through the Wall Street area of New York uncovers a distinct absence of leaves and branches. And a paltry green score of 62,848.

Okay. Let’s try London.

greencities_london_screenshot_1
greencities_london_postmask_1

greencities_london_screenshot_2
greencities_london_postmask_2

greencities_london_screenshot_3
greencities_london_postmask_3

Oh dear. Save a few hanging baskets, London is dry as gin. A pathetic green score of 46,176.

So there you have it. Hong Kong is the greenest city (yes yes, an administrative region) on the planet.

Granted, the sample rate was a bit low. And there are more cities on the globe, other than Hong Kong, New York and London. Plus our colour range seems to pick up a few artefacts besides plants (don’t think I didn’t notice your underhand tactics London – planting a man in a green T-shirt to get your score up).

Still. Looks like Rodger Saltwash picked a fine city in which to dwell and misdemeanour.

Ciao!

Advertisements