1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

numpyのshape:(h,w,c)から(w,h)を作るシンプルな書き方

Posted at

tl;dr

a.shape[1::-1] # == (a.shape[1], a.shape[0])

問題

opencvなどで扱う画像の実体はnumpy.ndarrayだが、そのshapeはnumpy仕様に準じて(height, width, channel)になる。
しかし、opencv関数の引数に画像サイズを渡すときは(width, height)であることが多い。

なので愚直にnumpy.ndarrayの画像サイズをopnecv関数などに渡す場合、

(a.shape[1], a.shape[0])

みたいに表現せざるを得ず、少し冗長になってしまっていた。

解決策

pythonのスライス書式を使って、

a.shape[1::-1] # == (a.shape[1], a.shape[0])

とすれば(a.shape[1], a.shape[0])と等価な値を得られることに気づいた。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?