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

Arkwood swooned on the sofa, his pupils dilating (positively two spinning discs, good Lord!). ‘I am in love,’ he announced, before kissing our cat and disrobing to skip upon a buttercup-strewn field. Venus was her name, the Roman goddess of love.

You see, I have used a bit of Python code to detect the planet Venus in the night sky. Here’s how…

import webbrowser
from time import sleep
import ImageGrab
import numpy
import cv2

# constants
WWT_URL = "http://www.worldwidetelescope.org/webclient/#/place=Solar_System_(Sky).3&lookAt=Planet&ra=6.71028&dec=25.82938&fov="
WWT_FOVS = ["0.18572", "0.10970", "0.05391"]

# grab screenshot of space
def grab_screenshot(url):

    screenshot = ImageGrab.grab(bbox=(0,260,1680,940))
    return cv2.cvtColor(numpy.array(screenshot), cv2.COLOR_RGB2BGR)

# get contours from image
def get_contours(image):
    edges = cv2.Canny(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), 100, 200)
    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    return contours

# get contour centroid
def get_contour_centroid(contour):
        M = cv2.moments(contour)
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])
        return (cx, cy)
        return (0, 0)

# do Venus fly-by
for fov in WWT_FOVS:
    # grab a screenshot
    image = grab_screenshot(WWT_URL + fov)

    # get largest object
    contours = get_contours(image)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]

    # print centre co-ordinates of object
    coord = get_contour_centroid(contours[0])
    print "Co-ordinates: {}".format(coord)

    # print centre colour of object
    colour = image[coord[1], coord[0]]
    print "Colour: {}".format(colour)

You’ll see that we have some constants. There is a url which targets Microsoft’s WorldWide Telescope HTML5 web application. And there are a bunch of Field Of View values, which will allow us to zoom past Venus.

We have a grab_screenshot function, which takes a screenshot of WorldWide Telescope in our browser. The exact astronomical spot will depend on the url passed to the function.

Our get_contours function uses OpenCV Canny Edge Detection and Contours to detect objects in the screenshot of space.

The get_contour_centroid function gets the centre coordinates of the object it is passed, using OpenCV Moments.

Okay, here’s the meat and two veg of the code. We loop through each Field Of View value (of which there are three) and grab a screenshot of the night sky. We detect the largest object in the sky. Finally, we print the coordinates and colour of the object. But is the detected object Venus? Let’s find out!

Here’s the first screenshot, which has a Field Of View of “0.18572”:


Yep, there’s Venus the Roman goddess just right of centre. What happens when we apply OpenCV Canny Edge Detection?


That’s a lot of objects detected in the sky! But which object has the largest contour?


Smashing! Venus has been successfully detected in space.

Let’s take a look at how the other two Field Of View values got on. Here’s “0.10970”:




Top-notch. Venus is in our grasp. Now for Field Of View value “0.05391”:




Oh, it gives me a pleasant tingling sensation in my groin. As Venus zooms by our field of view, we are able to track its coordinates. We also sample its colour.


We can match the X Y coordinates with our screenshots. As Venus zooms past us towards bottom right, the X and Y coordinates get bigger.

But what about the colour values? Do they match Venus’ reddish brown colour? Let’s put OpenCV’s Blue|Green|Red image format into Gimp and find out:




Yes. Every colour we have sampled has turned out to be reddish brown. We are definitely tracking the fiery Roman goddess.

So there you have it. We have used Python to detect the planet Venus.

Arkwood is outside, reading The Inscrutable Diaries Of Rodger Saltwash to the trees. And here comes a policeman to arrest him for indecent exposure. Love’s ways are very strange indeed.


I have taken screenshots of space using Microsoft’s WorldWide Telescope HTML5 web application. Did you know that you can also embed the application in your very own web page? Check out my post Worldwide Telescope and Python for detail.

The Python code was run on my Windows 7 PC using Python Tools for Visual Studio.

Note: I used the following code to draw the contour on the screenshot:

cv2.drawContours(image, [contours[0]], -1, (0, 255, 0), 3)