H极大值变换

对于某一连通区域S(共n个点),点Xi ( i = 1 : n ) 在S中,S的最大值为max(Xi)=M。

H极大值变换:若Xi ≥ M - H,则Xi = M - H;否则,Xi不变。

扩展极大值变换 :若Xi ≥ M - H,则Xi = 1;否则,Xi = 0。

区域极大值:若Xi = M,则Xi = 1;否则,Xi = 0。

 1 import numpy as np
 2 import cv2 
 3 
 4 
 5 def HMax(src, dst, h, kernel):
 6     '''
 7     HMax H-maxima transform.
 8     '''
 9     msk = np.zeros(src.shape, np.uint8)
10     temp1 = np.zeros(src.shape, np.uint8)
11     temp2 = np.zeros(src.shape, np.uint8)
12     
13     cv2.subtract(src, np.zeros(src.shape, np.uint8)+h, msk)#msk=src-h
14     cv2.min(src, msk, dst)
15     cv2.dilate(dst, kernel, dst)
16     cv2.min(src, dst, dst)
17     
18     while True:
19         temp1=np.copy(dst)
20         cv2.dilate(dst, kernel, dst)
21         cv2.min(src, dst, dst)
22         #if temp1(i)==dst(i), than temp2(i)=0; else temp(i)=255
23         cv2.compare(temp1, dst, cv2.CMP_NE, temp2)
24         #for all i, if temp1(i)==dst(i), than break
25         if cv2.sumElems(temp2)[0]==0:
26             break
27     
28     return dst
29     
30 
31 def ExtendedHMax(src, dst, h, kernel):
32     '''
33     ExtendedHMax computes the extended-maxima transform, which
34     is the regional maxima of the H-maxima transform.
35     '''
36     src_hmax_0 = np.zeros(src.shape, np.uint8)
37     src_hmax_1 = np.zeros(src.shape, np.uint8)
38     
39     
40     HMax(src, src_hmax_0,   h, kernel)
41     HMax(src, src_hmax_1, h+1, kernel)
42     
43     cv2.subtract(src_hmax_0, src_hmax_1, dst)#dst=src_hmax_0-src_hmax_1
44         
45     return dst
46 
47 def RegionalMax(src, dst, kernel):
48     '''
49     computes the regional maxima of src.
50     '''
51     ExtendedHMax(src, dst, 1, kernel)
52     return dst
1 #src: an array read from a picture
2 
3 kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
4 
5 eMax = np.zeros(src.shape, np.uint8)
6 rMax = np.zeros(src.shape, np.uint8)
7 
8 ExtendedHMax(src, eMax, 7, kernel)
9 RegionalMax(src, rMax, kernel)

 

posted @ 2016-03-23 22:01  liangxw  阅读(1531)  评论(0编辑  收藏  举报