11
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Python3.6]OpenCVで画像を4分割

Posted at

OpenCVを使って画像を4分割するやり方の紹介です。
ちらっと検索した限りPython3.6、OpenCVを使って画像を4分割するコードを見つけることが出来なかったので投稿
(Python2.7で動くのは見つけたのですが、)

4分割のイメージ
これが
元画像

こんな感じに
2018-05-21 16_16_07-result.png

環境

-Windows10
-Python 3.6.4
-OpenCV 3.4.1

画像4分割コード

import cv2

def main():
    img = cv2.imread("./test.png")
    height, width, channels = img.shape

    clp = img[0:height//2, 0:width//2]     
    cv2.imwrite("./test-tl.jpg", clp)   

    clp = img[0:height//2, width//2:width]     
    cv2.imwrite("./test-tr.jpg", clp)   

    clp = img[height//2:height, 0:width//2]     
    cv2.imwrite("./test-ul.jpg", clp)   

    clp = img[height//2:height, width//2:width]     
    cv2.imwrite("./test-ur.jpg", clp)

if __name__ == "__main__":
    main()

【おまけ】OpenCV導入方法(Windows)

1.whlファイルをダウンロード
ここから
https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv

Python3.6、64bitのPCにインストールする場合は「opencv_python‑3.4.1‑cp36‑cp36m‑win_amd64.whl」をダウンロード

image.png

2.pip installでwhlファイルをインストール
ダウンロードしたwhlファイルがあるディレクトリまで移動して
pip install opencv_python-3.4.1-cp36-cp36m-win_amd64.whl
を実行

3.動作確認

以下のスクリプトを実行してOpenCVがインストールできたか確認

import cv2
print(cv2.__version__)

3.4.1
と出てくればインストール出来ています。

参考サイト

https://qiita.com/satoshicano/items/bba9594a1203e24e2a31
https://teratail.com/questions/76964

11
7
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
11
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?