私は、単純な添付画像から数字を読みしようとしています。
このために私は数を保持している五角形を見つけようとしています。しかし、私はそれが正しい値を与えていないOpenCVのfindcontour機能を使用して五角形を見つけようとしていますとき。私はその機能を様々な順列を試してみました。それのどれも働きました。
私はこれまで、次の試してみました:
import cv2 as cv
import numpy as np
im = cv.imread(r'out.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 200, 255, 0)
contours, hierarchy = cv.findContours(thresh, cv.RETR_LIST , cv.CHAIN_APPROX_SIMPLE)
for c in contours:
print(len(c))
出力:1 1 1 1 1 1 1 1 38 36 11 85 87 128 133 55 47 4 4 4 4 4 7
上記の点は、五角形することはできませんので、これはいずれも、5ではありません。
あなたは、私がどんな間違いを犯していた場合、私を助けてくださいことができますか?
You're on the right track. After finding contours, you need to perform contour approximation using cv2.approxPolyDP
+ cv2.arcLength
. You can check the return value from cv2.approxPolyDP
which will give you the polygonal curve shape approximation. If this value is five then you can assume it is a pentagon. Here's a simple approach:
Obtain binary image. Load image, grayscale, bilateral filter, Otsu's threshold
Find contours and perform contour approximation. Find contours with
cv2.findContours
then perfrom contour approximation. If a contour passes this filter we extract the bounding rectangle coordinates withcv2.boundingRect
and extract/save the ROI using Numpy slicing.
The detected ROI's in teal
Extracted/saved ROIs
注:そこ個々の画像として保存された2つのROIはあるが、それらは同じです。
コード
import cv2
import numpy as np
# Load image, grayscale, bilaterial filter, Otsu's threshold
image = cv2.imread('1.jpg')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.bilateralFilter(gray,9,75,75)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find contours, perform contour approximation, and extract ROI
ROI_num = 0
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.04 * peri, True)
# If has 5 then its a pentagon
if len(approx) == 5:
x,y,w,h = cv2.boundingRect(approx)
cv2.rectangle(image, (x, y), (x + w, y + h), (200,255,12), 2)
ROI = original[y:y+h, x:x+w]
cv2.imwrite('ROI_{}.png'.format(ROI_num), ROI)
ROI_num += 1
cv2.imshow('thresh', thresh)
cv2.imshow('ROI', ROI)
cv2.imshow('image', image)
cv2.waitKey()