opencv/samples/python/coherence.py

86 lines
2.2 KiB
Python
Raw Normal View History

2013-03-06 14:41:02 +08:00
#!/usr/bin/env python
2012-10-17 07:18:30 +08:00
'''
Coherence-enhancing filtering example
=====================================
inspired by
Joachim Weickert "Coherence-Enhancing Shock Filters"
http://www.mia.uni-saarland.de/Publications/weickert-dagm03.pdf
'''
# Python 2/3 compatibility
from __future__ import print_function
import sys
PY3 = sys.version_info[0] == 3
if PY3:
xrange = range
2012-10-17 07:18:30 +08:00
import numpy as np
import cv2 as cv
2012-10-17 07:18:30 +08:00
def coherence_filter(img, sigma = 11, str_sigma = 11, blend = 0.5, iter_n = 4):
h, w = img.shape[:2]
for i in xrange(iter_n):
print(i)
2012-10-17 07:18:30 +08:00
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
eigen = cv.cornerEigenValsAndVecs(gray, str_sigma, 3)
2012-10-17 07:18:30 +08:00
eigen = eigen.reshape(h, w, 3, 2) # [[e1, e2], v1, v2]
x, y = eigen[:,:,1,0], eigen[:,:,1,1]
gxx = cv.Sobel(gray, cv.CV_32F, 2, 0, ksize=sigma)
gxy = cv.Sobel(gray, cv.CV_32F, 1, 1, ksize=sigma)
gyy = cv.Sobel(gray, cv.CV_32F, 0, 2, ksize=sigma)
2012-10-17 07:18:30 +08:00
gvv = x*x*gxx + 2*x*y*gxy + y*y*gyy
m = gvv < 0
ero = cv.erode(img, None)
dil = cv.dilate(img, None)
2012-10-17 07:18:30 +08:00
img1 = ero
img1[m] = dil[m]
img = np.uint8(img*(1.0 - blend) + img1*blend)
print('done')
2012-10-17 07:18:30 +08:00
return img
if __name__ == '__main__':
import sys
try:
2013-03-06 14:41:02 +08:00
fn = sys.argv[1]
except:
2014-09-13 22:28:41 +08:00
fn = '../data/baboon.jpg'
2012-10-17 07:18:30 +08:00
src = cv.imread(fn)
2012-10-17 07:18:30 +08:00
def nothing(*argv):
pass
def update():
sigma = cv.getTrackbarPos('sigma', 'control')*2+1
str_sigma = cv.getTrackbarPos('str_sigma', 'control')*2+1
blend = cv.getTrackbarPos('blend', 'control') / 10.0
print('sigma: %d str_sigma: %d blend_coef: %f' % (sigma, str_sigma, blend))
2012-10-17 07:18:30 +08:00
dst = coherence_filter(src, sigma=sigma, str_sigma = str_sigma, blend = blend)
cv.imshow('dst', dst)
2012-10-17 07:18:30 +08:00
cv.namedWindow('control', 0)
cv.createTrackbar('sigma', 'control', 9, 15, nothing)
cv.createTrackbar('blend', 'control', 7, 10, nothing)
cv.createTrackbar('str_sigma', 'control', 9, 15, nothing)
2012-10-17 07:18:30 +08:00
print('Press SPACE to update the image\n')
2012-10-17 07:18:30 +08:00
cv.imshow('src', src)
2012-10-17 07:18:30 +08:00
update()
while True:
ch = cv.waitKey()
2012-10-17 07:18:30 +08:00
if ch == ord(' '):
update()
if ch == 27:
break
cv.destroyAllWindows()