実際のラベルを使って輪郭を出してみた。

輪郭: 初めの一歩
タイトル
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
import cv2
 
# 画像ファイルの読み込み
img = cv2.imread('label.png')
 
cv2.imshow('org',img)
cv2.waitKey(0)
 
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
 
cv2.imshow('gray',imgray)
cv2.waitKey(0)
 
 
ret,thresh = cv2.threshold(imgray,127,255,0)
 
cv2.imshow('thresh',thresh)
cv2.waitKey(0)
 
 
#image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
 
print(len(contours))
 
img = cv2.drawContours(img, contours, -1, (0,255,0), 3)
 
# 表示
cv2.imshow("outall", img)
cv2.waitKey(0)
 
#for i in range(len(contours)):
#    print(i)
#    img = cv2.drawContours(img, contours, i, (0,255,0), 3)
#    # 表示
#    cv2.imshow(str(i), img)
#    cv2.waitKey(0)
 
cv2.destroyAllWindows()

注)ラベル本体は手書きで黒塗りにしています。

コメント

このブログの人気の投稿

Python OpenCVとWebカメラでバーコードリーダー

VB.net Dictionaryクラスの初期化