keep-loving-pythonのブログ

Pythonを愛し続けたいです(Pythonが流行っている限りですが。。。)

OpenCVによる撮影画像の連結をいろいろ試してみる【2】

OpenCVによる撮影画像の連結をいろいろ試してみる【2】

基本は、GitHub等のコードを動かすだけ。

Stitch image from drone capture farm area

これも、比較的シンプルなもの

環境

windows10 python3.7 cv2.version.opencv_version 4.6.0.66 <--実行時に確認

↓ pip

opencv-contrib-python             4.5.3.56
opencv-python                     4.6.0.66 <--

URL

https://www.kaggle.com/code/phsophea101/stitch-image-from-drone-capture-farm-area/notebook

結果

image_0041.jpg と 51  と 61の3枚のみとした。

結果の補足

下記で、めちゃくちゃに時間がかった。
041と051のときで、
220秒
(@ LENOVO ideapad 320S)

    # Compute the matches
    matches = flann.knnMatch(descriptors1, descriptors2, k=2)

画像のサイズは、
2720 x 1530
大きいと言えば大きい

自分の関連記事

keep-loving-python.hatenablog.com

keep-loving-python.hatenablog.com

【Python多少深掘り】importどうなってる。(OpenCV編)

importどうなってる。(OpenCV編)

自分の端末で、pip listでみると、以下のようにcontribとそうじゃないのが入っている。

opencv-contrib-python             4.5.3.56
opencv-python                     4.6.0.66

何がimportされたかの調べ方

結局、それほど、わかりませんが、自分がいま到達したレベルで言えば。。。

一時、全然わからないから、Procmon.exeで調べようとしたが。。。

(自分の環境は、Windows10,python3.7です。)
何がimportされた(る)かわからないので、上記のProcmon.exeで、様子を見ようとした時期もありましたが。。。
成果は出ていません。

以下のコードでいかがでしょうか?

import cv2

print("cv2 __file__",cv2.__file__)
print("cv2 __path__",cv2.__path__)

print("cv2 version",cv2.__version__)
print("cv2 version(sono2)",cv2.version.opencv_version)

以下のようなのが出ます。

cv2 __file__ C:\Users\XYZZZ\AppData\Local\Programs\Python\Python37\lib\site-packages\cv2\__init__.py
cv2 __path__ ['C:\\Users\\XYZZZ\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\cv2']
cv2 version 4.6.0
cv2 version(sono2) 4.6.0.66

上記では、あまり、わからないのでは?

はい。上記だけではダメです。
上記のinit.pyに、適当にprint文とか入れて、、、結局、何を読み込んでいるか。。。調べて下さい!!

まず、

__file__  
__path__

などで、検討の入り口を作って下さい!!!

補足

意外と、何も考えずに、printでも、そこそこ、情報が出る??

print("cv2 ",cv2)
print("repr",repr(cv2))

cv2  <module 'cv2' from 'C:\\Users\\XYZZZ\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\cv2\\__init__.py'>
repr <module 'cv2' from 'C:\\Users\\XYZZZ\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\cv2\\__init__.py'>

コメント

あどばいすなどあれば、お願いしますーー。

解決策。cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'imwrite'

解決策。cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'imwrite'

エラー

エラー

cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'imwrite'

エラー詳細

Traceback (most recent call last):
  File "test01.py", line 68, in <module>
    cv2.imwrite("out_desu.jpg",out_desu)
cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'imwrite'
> Overload resolution failed:
>  - img is not a numerical tuple
>  - Expected Ptr<cv::UMat> for argument 'img'

環境

windows10
python 3.7
opencv-contrib-python 4.5.3.56

解決策

これも、ポカに近くて

以下などが、ヒント。imgに変なタプルを渡しているらしい。

>  - img is not a numerical tuple

たとえば、
以下のようなコードを書くと起こせる。 コメントアウト側が正解。

out_desu= alignImages(img1,img2)
##out_desu,_ = alignImages(img1,img2)

解決策。cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'cvtColor'

解決策。cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'cvtColor'

エラー

エラー

cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'cvtColor'

エラー詳細

Traceback (most recent call last):
  File "test01.py", line 65, in <module>
    out_desu,_ = alignImages(img1,img2)
  File "test01.py", line 23, in alignImages
    cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY), None
cv2.error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'cvtColor'
> Overload resolution failed:
>  - src is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'src'

環境

windows10
python 3.7
opencv-contrib-python 4.5.3.56

解決策

これも、ポカに近くて、
ちゃんと、画像データが渡せていない。
例えば、ファイル名を渡しているとか。。。

無理やり間違うとしたら、
該当関数の前の部分で、画像データにするのを忘れるなど。

img1 = "001.jpg"
img2 = "002.jpg"
########img1 = cv2.imread(img1)
########img2 = cv2.imread(img2)

解決策。cv2.error: OpenCV(4.5.3) ..... error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

解決策。cv2.error: OpenCV(4.5.3) ..... error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

これは
、ポカに近いもの。

エラー

エラー

cv2.error: OpenCV(4.5.3) ..... error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

エラー詳細

Traceback (most recent call last):
  File "test01.py", line 62, in <module>
    out_desu,_ = alignImages(img1,img2)
  File "test01.py", line 20, in alignImages
    cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY), None
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-_xlv4eex\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

環境

windows10
python 3.7
opencv-contrib-python 4.5.3.56

解決策

該当関数に、画像データが渡せていない。
それ以前の処理が誤っており、Noneを渡しているなど。

直前に、

    print("None kanaaaaaaaaaaaaaaaaaaaa",img1)

みたいなコードを入れると、

None kanaaaaaaaaaaaaaaaaaaaa None

みたなのが出ると思います。

その間違いかたは、各々だと思うで、誤りを修正して下さい。

解決策。TypeError: only integer scalar arrays can be converted to a scalar index

解決策。TypeError: only integer scalar arrays can be converted to a scalar index

エラー

エラー内容

TypeError: only integer scalar arrays can be converted to a scalar index

↓ 詳細

Traceback (most recent call last):
  File "stitching_detailed.py", line 519, in <module>
    main()
  File "stitching_detailed.py", line 351, in main
    img_names_subset.append(img_names[indices[i]])
TypeError: only integer scalar arrays can be converted to a scalar index

環境

windows10
python 3.7
opencv-contrib-python 4.5.3.56
numpy 1.21.6 (1.19.5, 1.18.0もダメだった)

解決策

解決策の前に。。。

以下の記事によると、このメッセージ自体は、ちょっと、「???」かも。

https://stackoverflow.com/questions/50997928/typeerror-only-integer-scalar-arrays-can-be-converted-to-a-scalar-index-with-1d

引用

Perhaps the error message is somewhat misleading, but

では、解決策。

スカラーが欲しいところを、配列(例:" [3] "とか)になっているだけなので。。。

その1

[0]で、中味にする。

        img_names_subset.append(img_names[indices[i][0]])

その2

int()で囲む。

        img_names_subset.append(img_names[int(indices[i])])

解決策。AttributeError: 'Namespace' object has no attribute 'func'

解決策。AttributeError: 'Namespace' object has no attribute 'func'

AttributeError: 'Namespace' object has no attribute 'xxx'

xxxの部分は、いろいろ。

エラーの内容

エラー

AttributeError: 'Namespace' object has no attribute 'func'

エラー詳細

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    args.func(args) # Error is here
AttributeError: 'Namespace' object has no attribute 'func'

環境

windows10
python3.7

解決策

エラーの出し方

サンプルコード

import argparse

def PrintSuru(args):
    print("Print: Print shimasu")
    exit(0)

parser = argparse.ArgumentParser()
subparser = parser.add_subparsers()
printooo = subparser.add_parser("printooo", help="korekara tsukuru")

printooo.set_defaults(func=PrintSuru)

args = parser.parse_args()
args.func(args) # Error is here

このコードは、以下を参考にしました。

https://gist.github.com/amarao/36327a6f77b86b90c2bca72ba03c9d3a

上記のコードで、
以下のように、引数をつけるとエラーになりません。

>python test.py printooo
Print: Print shimasu

エラーの解説

Namespaceとは

このNamespaceは、argparseの関係で定義されているクラスです。
いわゆる一般用語としてのNamespaceではない。

'Namespace' object has no attribute 'func' とは

これは、引数をつけると、funcというattributeが付加されるということでしょう。
argparse 自体を理解して下さい。

コメント

これは、役立つかも。。。