mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.3 KiB
86 lines
2.3 KiB
12 years ago
|
#!/usr/bin/env python
|
||
12 years ago
|
|
||
12 years ago
|
'''
|
||
|
Coherence-enhancing filtering example
|
||
|
=====================================
|
||
|
|
||
|
inspired by
|
||
|
Joachim Weickert "Coherence-Enhancing Shock Filters"
|
||
|
http://www.mia.uni-saarland.de/Publications/weickert-dagm03.pdf
|
||
|
'''
|
||
|
|
||
9 years ago
|
# Python 2/3 compatibility
|
||
|
from __future__ import print_function
|
||
|
import sys
|
||
|
PY3 = sys.version_info[0] == 3
|
||
|
|
||
|
if PY3:
|
||
|
xrange = range
|
||
|
|
||
12 years ago
|
import numpy as np
|
||
|
import cv2
|
||
|
|
||
|
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):
|
||
9 years ago
|
print(i)
|
||
12 years ago
|
|
||
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||
|
eigen = cv2.cornerEigenValsAndVecs(gray, str_sigma, 3)
|
||
|
eigen = eigen.reshape(h, w, 3, 2) # [[e1, e2], v1, v2]
|
||
|
x, y = eigen[:,:,1,0], eigen[:,:,1,1]
|
||
|
|
||
|
gxx = cv2.Sobel(gray, cv2.CV_32F, 2, 0, ksize=sigma)
|
||
|
gxy = cv2.Sobel(gray, cv2.CV_32F, 1, 1, ksize=sigma)
|
||
|
gyy = cv2.Sobel(gray, cv2.CV_32F, 0, 2, ksize=sigma)
|
||
|
gvv = x*x*gxx + 2*x*y*gxy + y*y*gyy
|
||
|
m = gvv < 0
|
||
|
|
||
|
ero = cv2.erode(img, None)
|
||
|
dil = cv2.dilate(img, None)
|
||
|
img1 = ero
|
||
|
img1[m] = dil[m]
|
||
|
img = np.uint8(img*(1.0 - blend) + img1*blend)
|
||
9 years ago
|
print('done')
|
||
12 years ago
|
return img
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
import sys
|
||
11 years ago
|
try:
|
||
12 years ago
|
fn = sys.argv[1]
|
||
11 years ago
|
except:
|
||
10 years ago
|
fn = '../data/baboon.jpg'
|
||
12 years ago
|
|
||
|
src = cv2.imread(fn)
|
||
|
|
||
|
def nothing(*argv):
|
||
|
pass
|
||
|
|
||
|
def update():
|
||
|
sigma = cv2.getTrackbarPos('sigma', 'control')*2+1
|
||
|
str_sigma = cv2.getTrackbarPos('str_sigma', 'control')*2+1
|
||
|
blend = cv2.getTrackbarPos('blend', 'control') / 10.0
|
||
9 years ago
|
print('sigma: %d str_sigma: %d blend_coef: %f' % (sigma, str_sigma, blend))
|
||
12 years ago
|
dst = coherence_filter(src, sigma=sigma, str_sigma = str_sigma, blend = blend)
|
||
|
cv2.imshow('dst', dst)
|
||
|
|
||
|
cv2.namedWindow('control', 0)
|
||
|
cv2.createTrackbar('sigma', 'control', 9, 15, nothing)
|
||
|
cv2.createTrackbar('blend', 'control', 7, 10, nothing)
|
||
|
cv2.createTrackbar('str_sigma', 'control', 9, 15, nothing)
|
||
|
|
||
|
|
||
9 years ago
|
print('Press SPACE to update the image\n')
|
||
12 years ago
|
|
||
|
cv2.imshow('src', src)
|
||
|
update()
|
||
|
while True:
|
||
|
ch = 0xFF & cv2.waitKey()
|
||
|
if ch == ord(' '):
|
||
|
update()
|
||
|
if ch == 27:
|
||
|
break
|
||
|
cv2.destroyAllWindows()
|