2値化後に輪郭抽出して、座標を自動的に決めるのがよいと思います。流れについては以下のサンプルコードを参考にしてください。
サンプルコード
OpenCV、numpy、画像処理に関する知識がある前提で書きます。
参考リンク
python
1import cv2
2import numpy as np
3from IPython.display import Image, display
4
5def imshow(img):
6 ret, img = cv2.imencode('.png', img)
7 display(Image(img))
8
9# 画像を読み込む。
10img = cv2.imread('test.png')
11assert img is not None, 'Failed to load image'
12
13# bgr -> grayscale
14gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
15
16# 2値化
17ret, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
18
19# 輪郭抽出
20# OpenCV 3 の場合
21# contours = cv2.findContours(
22# binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
23contours = cv2.findContours(
24 binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
25cnt = contours[0] # 対象の輪郭 (1つしか検出されない前提)
26
27# 輪郭を近似する。
28arclen = cv2.arcLength(cnt, closed=True)
29cnt = cv2.approxPolyDP(cnt, epsilon=0.05 * arclen, closed=False)
30
31# 輪郭に外接する長方形を計算する。
32x, y, w, h = cv2.boundingRect(cnt)
33
34# (NumPoints, 1, 2) -> (NumPoints, 2) -> (2, NumPoints)
35xs, ys = cnt.squeeze(axis=1).T
36
37# V の部分の x 座標
38cx = int(x + w / 2)
39# cx に対応する y 座標を探す。
40cy = np.asscalar(ys[np.abs(xs - cx) < 3])
41
42# 描画
43cv2.rectangle(img, (x, y), (cx, cy), color=(255, 0, 0), thickness=2) # 青
44cv2.rectangle(img, (cx, y), (x + w, cy), color=(0, 255, 0), thickness=2) # 緑
45cv2.rectangle(img, (x, cy), (x + w, y + h), color=(0, 0, 255), thickness=2) # 赤
46
47imshow(img)


2値化

輪郭抽出

求めたあと