--- /dev/null
+'''\r
+ Coherence-enhancing filtering example\r
+\r
+ inspired by \r
+ Joachim Weickert "Coherence-Enhancing Shock Filters"\r
+ http://www.mia.uni-saarland.de/Publications/weickert-dagm03.pdf\r
+'''\r
+\r
+import numpy as np\r
+import cv, cv2\r
+\r
+\r
+def coherence_filter(img, sigma = 11, str_sigma = 11, blend = 0.5, iter_n = 4):\r
+ h, w = img.shape[:2]\r
+\r
+ for i in xrange(iter_n):\r
+ print i,\r
+ \r
+ gray = cv2.cvtColor(img, cv.CV_BGR2GRAY)\r
+ eigen = cv2.cornerEigenValsAndVecs(gray, str_sigma, 3)\r
+ eigen = eigen.reshape(h, w, 3, 2) # [[e1, e2], v1, v2]\r
+ x, y = eigen[:,:,1,0], eigen[:,:,1,1]\r
+ \r
+ gxx = cv2.sobel(gray, cv2.CV_32F, 2, 0, ksize=sigma)\r
+ gxy = cv2.sobel(gray, cv2.CV_32F, 1, 1, ksize=sigma)\r
+ gyy = cv2.sobel(gray, cv2.CV_32F, 0, 2, ksize=sigma)\r
+ gvv = x*x*gxx + 2*x*y*gxy + y*y*gyy\r
+ m = gvv < 0\r
+\r
+ ero = cv2.erode(img, None)\r
+ dil = cv2.dilate(img, None)\r
+ img1 = ero\r
+ img1[m] = dil[m]\r
+ img = np.uint8(img*(1.0 - blend) + img1*blend)\r
+ print 'done'\r
+ return img\r
+\r
+ \r
+if __name__ == '__main__':\r
+ import sys\r
+ try: fn = sys.argv[1]\r
+ except: fn = '../cpp/baboon.jpg'\r
+\r
+ src = cv2.imread(fn)\r
+\r
+ def nothing(*argv):\r
+ pass\r
+\r
+ def update():\r
+ sigma = cv2.getTrackbarPos('sigma', 'control')*2+1\r
+ str_sigma = cv2.getTrackbarPos('str_sigma', 'control')*2+1\r
+ blend = cv2.getTrackbarPos('blend', 'control') / 10.0\r
+ print 'sigma: %d str_sigma: %d blend_coef: %f' % (sigma, str_sigma, blend)\r
+ dst = coherence_filter(src, sigma=sigma, str_sigma = str_sigma, blend = blend)\r
+ cv2.imshow('dst', dst)\r
+\r
+ cv2.namedWindow('control', 0)\r
+ cv.CreateTrackbar('sigma', 'control', 9, 15, nothing)\r
+ cv.CreateTrackbar('blend', 'control', 7, 10, nothing)\r
+ cv.CreateTrackbar('str_sigma', 'control', 9, 15, nothing)\r
+\r
+\r
+ print 'Press SPACE to update the image\n'\r
+\r
+ cv2.imshow('src', src)\r
+ update()\r
+ while True:\r
+ ch = cv2.waitKey()\r
+ if ch == ord(' '):\r
+ update()\r
+ if ch == 27:\r
+ break\r