Astronomy, Computer Vision, Dilation, Erosion, Image Analysis, Microsoft, Morphological Transformations, OpenCV, Python, Python Tools for Visual Studio, Rodger Saltwash, Space, Stars, WorldWide Telescope
Arkwood was weeping into his custard this morning.
‘What on earth is the matter?’ I asked him.
‘Earthly matters,’ he replied, wiping his snotty nose on his shirt sleeve. He told me that when he dies no one will remember him.
‘Don’t fret,’ I responded, placing a comforting hand upon his quivering shoulder, ‘I will invest in a wooden bench by the coast, with a plaque bearing your name. People will remember you, as they rest their posteriors.’
‘Fuck that shit!’ Arkwood blasted, ‘I want a star named after me.’
Not wanting to let down my weird Belgian buddy, I began to write some Python code to examine the universe and find a suitable luminous sphere of plasma.
I will use Microsoft’s WorldWide Telescope web client to obtain screenshots of the astronomical sky. Then, using OpenCV image analysis techniques, I will locate various celestial bodies in said screenshots. I’ll discover a new star, and name it “Arkwood”.
Let’s start simple. Let’s use OpenCV Morphological Transformations to remove noise from each screenshot, so that we are left with only the largest stars.
Here’s Microsoft’s WorldWide Telescope in my browser:
And here’s the screenshot, ready for OpenCV analysis:
Note: for now, I’ve acquired the screenshot manually – but going forward it can be obtained from within the code using ImageGrab.
Okay, so let’s load the screenshot into our Python code and use OpenCV to remove the noise from the snap:
import cv2 import numpy as np # load image img = cv2.imread('stars.png',0) # apply morphological transformation kernel = np.ones((5,5),np.uint8) img_morph = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) # save image cv2.imwrite('stars_morph.png', img_morph)
First up, we load our screenshot of the stars in grayscale format.
Next, we use OpenCV Morphological Transformations to remove noise from the screenshot. We are eroding the image to get rid of small celestial objects, and then dilating the image to pump the big celestial objects back up to their original size.
All that’s left to do is save the transformed image.
Here’s the screenshot loaded as grayscale:
And here’s the screenshot after transformation:
Top-notch! OpenCV Morphological Transformations has removed all the noise. We are left with just the biggest celestial bodies, on which to do further analysis.
‘Have you found my twinkle yet?’ Arkwood asked.
‘Not yet,’ I replied. ‘But the adventure has begun. You will soon have your name written in the stars!’
Arkwood huffed and turned another page on the mortal and futile endeavours of Rodger Saltwash.
OpenCV Morphological Transformations are often performed on binary images. We can update our code to convert the screenshot to binary, after it has been loaded:
ret,img = cv2.threshold(img,20,255,cv2.THRESH_BINARY)
Here’s the binary image:
And here’s the binary image after transformation:
Note: I used Python Tools for Visual Studio to run the code on my Windows 7 PC.