実際のラベルを使って輪郭を出してみた。
輪郭: 初めの一歩
注)ラベル本体は手書きで黒塗りにしています。
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()
注)ラベル本体は手書きで黒塗りにしています。
コメント
コメントを投稿