1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Thu Dec 20 16:05:10 2018
 4 
 5 @author: leizhen.liu
 6 """
 7 
 8 import cv2.cv2 as cv2
 9 import numpy as np
10 
11 data = cv2.imread('anwser.jpg')
12 cv2.imshow('anwser',data)
13 
14 
15 #灰度處理
16 gray = cv2.cvtColor(data,cv2.COLOR_BGR2GRAY)
17 cv2.imshow('gray',gray)
18 
19 
20 kernel = np.ones((5,5),np.uint8) 
21 
22 #膨脹操作
23 pengzhang = cv2.dilate(gray,kernel,iterations =1)
24 cv2.imshow('pengzhang',pengzhang)
25 
26 #腐蝕
27 fushi = cv2.erode(pengzhang,kernel,iterations =1)
28 cv2.imshow('fushi',fushi)
29 
30 
31 #二值化
32 ret ,color2 = cv2.threshold(fushi,127,255,cv2.THRESH_BINARY_INV)
33 cv2.imshow('binary',color2)
34 
35 #轮廓
36 _,contours,hierarchy=cv2.findContours(color2,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)  
37 #newimg=np.zeros_like(color2)   
38 cv2.drawContours(data, contours, -1, (0,0,255),1)
39 cv2.imshow('lunkuo',data)
40 
41 cv2.waitKey(0)

注意点:1、cv2.waitKey() 要写否则图片不能显示。

posted on 2018-12-20 17:36  卡贝天师  阅读(1709)  评论(0编辑  收藏  举报