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)

 

转载于:https://www.cnblogs.com/liangxw/p/5313326.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值