質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

412閲覧

python 画像処理

dqSgZ1JmLUOGOVb

総合スコア9

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/08/25 10:13

前提・実現したいこと
「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]))

試したこと
構文のエラーだと思い本に載っているサンプルコードと見比べてみましたが解決しませんでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

umyu

2018/08/25 10:33

Pythonはインデントが重要な言語です。質問文のソースコード部分をhttps://teratail.storage.googleapis.com/uploads/contributed_images/56957fe805d9d7befa7dba6a98676d2b.gif を参考に編集してくださいな。
guest

回答1

0

エラーメッセージにある 32行目の以下の行、 [] の組合せがあっていますか?

list_dsts = np.float32([[list_srcs[0], list_srcs[1], [x1-x_margin, y1-y_margin], [x1-x_margin, y0+y_margin]])


作業ディレクトリ名とか、出ていますが、問題ありません?

投稿2018/08/25 10:44

pepperleaf

総合スコア6383

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問