An algorithm used to draw the skyline from an existing image.
The goal is to achieve an effect similar to this (image found online, please contact me regarding copyright issues):
- Read the image and extract the RGB values into an array.
- Extract the blue channel values and plot a histogram.
- Determine the threshold based on the histogram.
- Classify pixels:
- Blue channel values above the threshold are painted white (sky).
- Blue channel values below the threshold are painted black (land).
- Traverse the image from top to bottom and keep the first black point for each column. This forms a preliminary skyline.
- Connect discontinuous points in the skyline to form a complete curve.
Each pixel has an RGB value, e.g., (10, 124, 255). The third value (255) is the blue channel, which ranges from 0 to 255.
- By analyzing the blue channel values of all pixels, we can create a histogram.
- This histogram will likely show two peaks:
- One peak represents the sky (high blue values).
- The other represents the land (lower blue values).
- The minimum value between the two peaks is used as the threshold.
After applying the threshold, the image looks like this:
- Identify the two peaks in the histogram.
- The minimum value between these peaks is the threshold.
- If this step is skipped, the threshold may be set outside the peaks, leading to inaccurate results.
At this stage, the preliminary skyline might look like this:
- Some x-coordinates appear to have no black points, but they do exist at unusual positions.
- These invalid points are ignored.
- The remaining valid points form a discrete point graph.
- To connect the points:
- Start from a point.
- Greedily search the surrounding 8 points to find the shortest distance to the next point.
- Connect all points sequentially to form a continuous skyline.
And that’s it!


