/
形态学分割.py
54 lines (44 loc) · 1.55 KB
/
形态学分割.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
"""
Created on Wed May 23 20:38:17 2018
@author: Windows
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from skimage import data,filters,segmentation,measure,morphology,color
#加载并裁剪硬币图片
imgname1 = "009-b.jpg"
imgname2 = "003-b.jpg"
img1 = cv2.imread(imgname1)
img2 = cv2.imread(imgname2)
image = data.coins()[50:-50, 50:-50]
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
image = gray1
thresh =filters.threshold_otsu(image) #阈值分割
bw =morphology.closing(image > thresh, morphology.square(3)) #闭运算
cleared = bw.copy() #复制
segmentation.clear_border(cleared) #清除与边界相连的目标物
label_image =measure.label(cleared) #连通区域标记
borders = np.logical_xor(bw, cleared) #异或
label_image[borders] = -1
image_label_overlay =color.label2rgb(label_image, image=image) #不同标记用不同颜色显示
fig,(ax0,ax1)= plt.subplots(1,2, figsize=(8, 6))
ax0.imshow(cleared,plt.cm.gray)
ax1.imshow(image_label_overlay)
for region in measure.regionprops(label_image): #循环得到每一个连通区域属性集
#忽略小区域
if region.area < 5000:
continue
#绘制外包矩形
minr, minc, maxr, maxc = region.bbox
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
fill=False, edgecolor='red', linewidth=2)
ax1.add_patch(rect)
fig.tight_layout()
plt.show()
#win = cv2.namedWindow('Result', flags=0)
#cv2.imshow('Result', image)
#cv2.waitKey()
#cv2.destroyAllWindows()