Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Edges detection using Python
________ ________ ___ __ ___
|\_____ \|\ __ \|\ \|\ \ |\ \
\|___/ /\ \ \|\ \ \ \/ /|\ \ \
/ / /\ \ __ \ \ ___ \ \ \
/ /_/__\ \ \ \ \ \ \\ \ \ \ \
|\________\ \__\ \__\ \__\\ \__\ \__\
\|_______|\|__|\|__|\|__| \|__|\|__|
Ignore PyCharm. program should render easily using a notebook.

Edges detection of an image using Numpy, PIL, Math and Requests packages

Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept.

One simple program using Python for detecting edges.
The following gist offers a program scaled in seven parts:

  1. Import Numpy and related packages
  2. Load image remotely and define scaling variables
  3. Create output image
  4. Convert image to grayscale
  5. Compute intensity convolution using a for loop
  6. Draw magnitude in black and white
  7. Print out input and output images

Getting Started

This Python program built in 21 lines helps you render edges from a given image. The below instructions will help you run this Python program on your local machine for development and testing purposes, as well as in third party sites hosted in the cloud.

  • (#) and (''') are used to comment the following gist.

Prerequisites

I am using Jupyter Notebook on localhost (Ubuntu 18.04 bionic).
You can use Python IDEs and code editors on remote servers (see Tips).

Tips

If you are not using Linux/Unix and still want to try this simple Python program:

Basic commands in Jupyter

  • Note that in Jupyter you add new lines by typing "b" from your keyboard whilst the notebook is opened.
  • Avoid runing the entire code in a single cell in order to understand the steps.
  • Use "ctrl + enter" to execute each line if you want to get the output.
  • Use "dd" outside a cell to delete it.
  • Use "a" outside a cell to add a new cell above it.
  • Use "b" outside as cell to add a new cell below it.
  • Running the last cell should execute the permutations as program output.

Running the tests

  • I used Ubuntu (18.04 bionic) to launch Jupyter Notebook on localhost.
  • Localhost instantiates while using $ jupyter notebook in the terminal.
  • Check if Jupyter is correctly installed: $ jupyter --version

Built With

  • Jupyter - An open source software for creating notebooks
  • Numpy - The fundamental package for scientific computing with Python
  • Math - Mathematical functions defined by the C standar
  • PIL - Create images, annotate, retouch existing ones
  • Requests - A non-GMO HTTP library for Python, safe for human consumption

Versioning

I used no vesioning system for this gist, which repos status is flagged as concept because it is intended to be a demo or POC (proof-of-concept).

Author

  • Isaac Arnault - Suggesting a way to render edges for specific images such as: coins, architecture images.

Practicum

Store the below image remotely (on your IDE / PC) and try to detect edges using Python.

parking.png

MIT License
Copyright (c) 2018 Isaac Arnault
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Program

# Edges detection using Python
# 1. Import Numpy and related packages
from PIL import Image, ImageDraw
import numpy as np
from math import sqrt
import requests

# 2. Load image remotely and define scaling variables
input_image = Image.open("/resources/data/coin.jpg")
input_pixels = input_image.load()
width, height = input_image.width, input_image.height

# 3. Create output image
output_image = Image.new("RGB", input_image.size)
draw = ImageDraw.Draw(output_image)

# 4. Convert image to grayscale
intensity = np.zeros((width, height))
for x in range(width):
    for y in range(height):
        intensity[x, y] = sum(input_pixels[x, y]) / 2

# 5. Compute intensity convolution using a for loop

for x in range(1, input_image.width - 1):
    for y in range(1, input_image.height - 1):
        magx = intensity[x + 1, y] - intensity[x - 1, y]
        magy = intensity[x, y + 1] - intensity[x, y - 1]

        # 6. Draw magnitude in black and white
        color = int(sqrt(magx**2 + magy**2))
        draw.point((x, y), (color, color, color))

# 7. Print out input and output images      
display(input_image, output_image)
output_image.save("output_image.png")

Input image

input-image.png

Output image

output-image.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.