Вы находитесь на странице: 1из 5

https://github.

com/EternityCode/kirsch-edge-detector
kirsch-edge-detector/.gitignore
LIBRERIA 1
gitignore
*.[pP][nN][gG]
*.[jJ][pP[gG]
*.[jJ][pP][eE][gG]
*.[bB][mM][pP]
*.[gG][iI[fF]

LIB 2
LICENSE
The MIT License (MIT)
Copyright (c) 2015 EternityCode
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.

LIB 3
README.md
https://github.com/python-pillow/Pillow

Help Output
usage: kirsch.py [-h] [-s suffix] [-c {mono,sim,fpga}] [-r ratio]
img_files [img_files ...]
Compute a map of edges and their directions from input images using the Kirsch

operator.
positional arguments:
img_files
optional arguments:
-h, --help
show this help message and exit
-s suffix, --suffix suffix
a string to append to the end of the input filename
-c {mono,sim,fpga}, --colour {mono,sim,fpga}
select the output edge colour mapping, 'sim' and
'fpga' are the ECE 327 colour mappings
-r ratio, --resize ratio
scaling factor of each input pixel

LIB 4
kirsch.py

#!/usr/bin/env python3
import argparse
import os
import sys
from PIL import Image
# Input Arguments
def sposint(val):
val = int(val)
if val <= 0:
raise argparse.ArgumentTypeError(
'{} invalid positive int value'.format(val))
return val
aparser = argparse.ArgumentParser(
description='Compute a map of edges and their directions from input ' \
'images using the Kirsch operator.')
aparser.add_argument('-s', '--suffix', dest='img_suffix', metavar='suffix',
default='_edge', type=str,
help='a string to append to the end of the input filename')
aparser.add_argument('-c', '--colour', dest='img_colour_map',
choices=('mono', 'sim', 'fpga'), default='sim',
help='select the output edge colour mapping, \'sim\' and \'fpga\' are ' \
'the ECE 327 colour mappings')
aparser.add_argument('-r', '--resize', dest='img_ratio', metavar='ratio',
default=1, type=sposint, help='scaling factor of each input pixel')
aparser.add_argument('img_files', nargs='+', type=str)
args = aparser.parse_args()
# Colour Mappings
bg_colour = (0, 0, 0)
sim_colours = ((0, 100, 200), (100, 200, 0), (0, 200, 100), (255, 0, 0),
(0, 0, 255), (200, 100, 0), (0, 255, 0), (200, 0, 100))

mono_colour = (255, 127, 0)


fpga_colours = ((0, 0, 255), (255, 0, 0), (0, 255, 0), (255, 255, 0),
(85, 85, 85), (255, 0, 255), (0, 255, 255), (255, 255, 255))
# Function Definitions
def getDerivatives(conv_table):
conv_mask = [5, -3, -3, -3, -3, -3, 5, 5]
rot = lambda l, n: l[-n:] + l[:-n]
derivs = []
for _ in range(8):
derivs.append(sum([a * b for a,b in zip(conv_table, conv_mask)]))
conv_mask = rot(conv_mask, 1)
return derivs
def getEdgeColour(index, colour_map):
if colour_map == 'sim':
return sim_colours[index]
elif colour_map == 'fpga':
return fpga_colours[index]
else:
return mono_colour
# Main Program
for n, file in enumerate(args.img_files):
try:
img_grey = Image.open(args.img_files[0]).convert('L')
except IOError:
msg = 'Fatal Error: Could not open file \'{name}\'.'.format(name=file)
sys.exit(msg)
img_edge = Image.new(
'RGB',
(img_grey.width * args.img_ratio, img_grey.height * args.img_ratio),
bg_colour)
for y in range(1, img_grey.height - 1):
msg = "\r[{:0>3d}/{:0>3d}] Processing File: {} (Row {:0>5d} of {:0>5d})"
sys.stdout.write(
msg.format(n + 1, len(args.img_files),
file, y + 1, img_grey.height - 1)
)
sys.stdout.flush()
for x in range(1, img_grey.width - 1):
derivs = getDerivatives([img_grey.getpixel((x - 1, y - 1)),
img_grey.getpixel((x, y - 1)),
img_grey.getpixel((x + 1, y - 1)),
img_grey.getpixel((x + 1, y)),
img_grey.getpixel((x + 1, y + 1)),
img_grey.getpixel((x, y + 1)),
img_grey.getpixel((x - 1, y + 1)),
img_grey.getpixel((x - 1, y))])
if max(derivs) > 383:
pos = next(pos for pos in range(len(derivs))
if derivs[pos] == max(derivs))
for i_x in range(args.img_ratio):
for i_y in range(args.img_ratio):
img_edge.putpixel(
(x * args.img_ratio + i_x,
y * args.img_ratio + i_y),

getEdgeColour(pos, args.img_colour_map))
try:
img_edge.save('{name}{suff}{ext}'.format(
name=os.path.splitext(file)[0], suff=args.img_suffix,
ext=os.path.splitext(file)[1]))
except IOError:
msg = 'Fatal Error: Could not write to file ' \
'\'{name}\'.'.format(name=file)
sys.exit(msg)
print('')
print('Success.')

..........................................................
OpenCV with Python specific filter not same
http://stackoverflow.com/questions/20169137/opencv-with-python-specific-filter-n
ot-same
I'm trying to make a Kirsch filter in Python with OpenCV. I'm trying to do so fo
llowing this article.
http://www.mathworks.com/matlabcentral/fileexchange/24990-retinal-blood-vessel-e
xtraction/content/Retinal_Blood_Vessel_Extraction/VesselExtract.m
...................................
OPENCV........

import cv2
import numpy as np
np.set_printoptions(threshold=np.nan)
resim = cv2.imread('filter.jpg')
resim = cv2.cvtColor(resim, cv2.COLOR_RGB2GRAY)
h1 = np.array([[5, -3, -3], [-5, 0, -3], [5, -3, -3]], dtype=np.float32)/
h2 = np.array([[-3, -3, 5], [-3, 0, 5], [-3, -3, 5]], dtype=np.float32) /
h3 = np.array([[-3, -3, -3], [5, 0, -3], [5, 5, -3]], dtype=np.float32) /
h4 = np.array([[-3, 5, 5], [-3, 0, 5], [-3, -3, -3]], dtype=np.float32) /
h5 = np.array([[-3, -3, -3], [-3, 0, -3], [5, 5, 5]], dtype=np.float32) /
h6 = np.array([[5, 5, 5], [-3, 0, -3], [-3, -3, -3]], dtype=np.float32) /
h7 = np.array([[-3, -3, -3], [-3, 0, 5], [-3, 5, 5]], dtype=np.float32) /
h8 = np.array([[5, 5, -3], [5, 0, -3], [-3, -3, -3]], dtype=np.float32) /
print(h1)
new_img1 = cv2.filter2D(resim, -1, h1)
new_img2 = cv2.filter2D(resim, -1, h2)
new_img3 = cv2.filter2D(resim, -1, h3)
new_img4 = cv2.filter2D(resim, -1, h4)
new_img5 = cv2.filter2D(resim, -1, h5)
new_img6 = cv2.filter2D(resim, -1, h6
new_img7 = cv2.filter2D(resim, -1, h7)
new_img8 = cv2.filter2D(resim, -1, h8)

15
15
15
15
15
15
15
15

cv2.imshow('color_image', new_img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

h1 = np.array([[5, -3, -3], [5, 0, -3], [5, -3, -3]], dtype=np.float32)/ 15

Вам также может понравиться