Created
May 2, 2019 08:47
-
-
Save MrGussio/c4cdbb7e84c94943edab3f322a7ff2b8 to your computer and use it in GitHub Desktop.
Edgefinding code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Apr 30 11:11:23 2019 | |
@author: s3385159 | |
""" | |
#Exercise 2.1 | |
import numpy as np | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
import matplotlib.image as mpimg | |
#Exercise 2.3 | |
peer = Image.open('peer.jpg') | |
peer_array = np.array(peer) | |
def find_edges(image): | |
image_array = np.array(image) | |
output_array = np.array(image) | |
imgHeight = len(image_array) | |
imgWidth = len(image_array[0]) | |
biggestDiff = 0 | |
for y in range(len(image_array)): #loop door alle rijen pixels | |
for x in range(len(image_array[y])): #loop per pixel in horizontale rij van pixels | |
pixel = image_array[y][x] #hoofdpixel | |
for nX in range(x-1, x+1): | |
for nY in range(y-1, y+1): | |
if(not (nY == y and nX == x)):#hoofdpixel zelf uitsluiten | |
if(y >= 0 and y < imgHeight and x >= 0 and x < imgWidth): #ervoor zorgen dat je niet buiten de afbeeldinggrootte zoekt | |
neighbour = image_array[nY][nX] #een van de naastliggende pixxels | |
diffR = np.abs(pixel[0] - neighbour[0]) | |
diffB = np.abs(pixel[1] - neighbour[1]) | |
diffG = np.abs(pixel[2] - neighbour[2]) | |
totalDiff = np.sqrt(diffR**2 + diffB**2 + diffG**2) | |
if(totalDiff > biggestDiff): | |
biggestDiff = totalDiff | |
print(x, y, diffR, diffB, diffG, totalDiff) | |
if(totalDiff > 441): | |
output_array[nY][nX] = [255, 0, 0] | |
print(biggestDiff) | |
output = Image.fromarray(output_array) | |
output.save('peer_output.jpg') | |
find_edges(peer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment