私はcv2.findContours
から得られたNumPy配列contours
を持っていて、contours = np.concatenate(contours, axis = 0)
を使って平らにしました。画像からのオブジェクトの輪郭の座標を格納します。ただし、XまたはYのいずれか小さい方の座標を削除する場合は、100、または1000を超えています。最初にcontours = np.delete(contours, 0)
とcontours = np.delete(contours[0], 0)
を使用してすべての項目を削除しようとしましたが、このエラーが発生しました: IndexError: invalid index to scalar variable.
NumPy配列から値のペアを削除します
このような値のペアを削除するにはどうすればよいですか?
print(type(contours))
→ <class 'numpy.ndarray'>
print(contours[0])
→ [[2834 4562]]
print(type(contours[0]))
→ <class 'numpy.ndarray'>
print(contours[0][0])
→ [2834 4562]
print(type(contours[0][0]))
<class 'numpy.ndarray'>
はまた、私はそれが私がcv2.convexHull(contours)
に送信することを必要とする形で正確だから、それ以上のリストを/連結平らにする必要はありません。
はここに私のコードの最小限の作業のサンプルです:ここで
import cv2 # library for processing images
import numpy as np # numerical calculcations for Python
img = cv2.imread("img.png")
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, img_thr = cv2.threshold(img_gray,0,255,cv2.THRESH_OTSU)
img_rev = cv2.bitwise_not(img_thr)
img_cnt, contours, hierarchy = cv2.findContours(img_rev, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = np.concatenate(contours, axis = 0)
hull = cv2.convexHull(contours)
rect = cv2.minAreaRect(np.int0(hull))
box = cv2.boxPoints(rect)
box = np.int0(box)
img_cnt = cv2.drawContours(img, contours, -1, (0,255,0), 3)
img_cnt = cv2.drawContours(img, [box], -1, (0,0,255), 5)
cv2.imwrite("img_out.png", img_cnt)
はサンプルinput imageあり、ここで私のoutput imageです。私は、テキスト選択のための外側の "ノイズ"を無視したい。私はさらなるノイズ削減を使用することはできません。
[最小、完全、および確認可能](http://stackoverflow.com/help/mcve)の例を作成してください。そうすれば、私たちがあなたを助けやすくなります。 –