回転した四角形を描画する具体的な例を次に示します。アイデアは、 Otsu のしきい値を使用してバイナリ イメージを取得し、を使用して輪郭を見つけることcv2.findContours()
です。を使用して回転した長方形を取得し、 を使用cv2.minAreaRect()
して 4 つの角の頂点を取得できcv2.boxPoints()
ます。cv2.drawContours()
またはを使用して長方形を描画しますcv2.polylines()
。
入力->
出力
コード
import cv2
import numpy as np
# Load image, convert to grayscale, Otsu's threshold for binary image
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find contours, find rotated rectangle, obtain four verticies, and draw
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
rect = cv2.minAreaRect(cnts[0])
box = np.int0(cv2.boxPoints(rect))
cv2.drawContours(image, [box], 0, (36,255,12), 3) # OR
# cv2.polylines(image, [box], True, (36,255,12), 3)
cv2.imshow('image', image)
cv2.waitKey()