前提・実現したいこと
「pythonで始めるOpenCV3プログラミング」の書籍で画像処理の勉強を行っています。
4.4の透視撮影の分野ですがエラーの原因が分からずつまずいています。
ご回答お願いします。
発生している問題・エラーメッセージ
runfile('C:/Users/Yumi/.spyder-py3/perspective.py', wdir='C:/Users/Yumi/.spyder-py3')
Traceback (most recent call last):
File "C:\Users\Yumi\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-10-27eebc9173fc>", line 1, in <module>
runfile('C:/Users/Yumi/.spyder-py3/perspective.py', wdir='C:/Users/Yumi/.spyder-py3')
File "C:\Users\Yumi\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\Yumi\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Yumi/.spyder-py3/perspective.py", line 34
list_dsts = np.float32([[list_srcs[0], list_srcs[1], [x1-x_margin, y1-y_margin], [x1-x_margin, y0+y_margin]])
^
SyntaxError: invalid syntax
該当のソースコード
import cv2
import numpy as np
try:
img =cv2.imread('D:/temp/Lenna.jpg')
if img is None: print('ファイルを読み込めません') import sys sys.exit() rows, cols = img.shape[:2] x0 = cols/4 x1 = (cols*3)/4 y0 = rows/4 y1 = (rows*3)/4 list_src = np.float32([[x0, y0],[x0, y1],[x1, y1],[x1, y0]]) x_margin = cols/10 y_margin = rows/10 list_dsts = np.float32([[x0+x_margin, y0+y_margin],[x1, y1], list_src[1], list_src[2], [x1-x_margin, y0+y_margin]]) perspective_matrix = cv2.getPerspectiveTransform(list_srcs, list_dsts) dst = cv2.warpPerspective(img, perspective_matrix, (cols, rows)) cv2.imwrite("D:/temp/dst0.jpg", dst) cv2.imshow('dst0', dst) x_margin = cols/8 y_margin = rows/8 list_dsts = np.float32([[list_srcs[0], list_srcs[1], [x1-x_margin, y1-y_margin], [x1-x_margin, y0+y_margin]]) perspective_matrix = cv2.getPerspectiveTransform(list_srcs, list_dsts) dst = cv2.warpPerspective(img, perspective_matrix, (cols, rows)) cv2.imwrite("D:/temp/dst1.jpg", dst) cv2.imshow('dst1', dst) x_margin = cols/6 y_margin = rows/6 list_dsts = np.float32([[x0+x_margin, y0+y_margin], list_src[1], [x1-x_margin, y1-y_margin], list_src[3]]) perspective_matrix = cv2.getPerspectiveTransform(list_srcs, list_dsts) dst = cv2.warpPerspective(img, perspective_matrix, (cols, rows)) cv2.imwrite("D:/temp/dst2.jpg", dst) cv2.imshow('dst2', dst)
except:
import sys
print("Error:", sys.exc_info()[0])
print(sys.exc_info()[1])
import traceback
print(traceback.format_tb(sys.exc_info()[2]))
試したこと
構文のエラーだと思い本に載っているサンプルコードと見比べてみましたが解決しませんでした。