前提・実現したいこと
Pythonを用いて写真画像内にある物体外側の輪郭の描画を実現したいと思っております。
実行したところ、画像を保存する行でエラーが発生してしまいます。
どうかご教授願います。
発生している問題・エラーメッセージ
img.save('edited_img.jpg') AttributeError: 'numpy.ndarray' object has no attribute 'save'
該当のソースコード
Python
1# -*- coding: utf-8 -*- 2import matplotlib.pyplot as plt 3from PIL import Image 4import numpy as np 5import cv2 6 7im = cv2.imread('test.jpg') 8imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 9ret,thresh = cv2.threshold(imgray,127,255,cv2.THRESH_OTSU) 10contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) 11 12 13external_contours = np.zeros(im.shape) 14 15for i in range(len(contours)): 16 17 if hierarchy[0][i][3] == -1: 18 19 img = cv2.drawContours(external_contours, contours, i, 255, -1) 20 21img.save('edited_img.jpg')
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/08 15:47