3
6

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.

[python]opencvあれこれ

Posted at

import

python
import cv2 

画像を読み込む

python
img = cv2.imread('画像のパス')
# グレースケールで読み込み
gray_img = cv2.imread('画像のパス', cv2.IMREAD_GRAYSCALE))

画像を保存する

python
cv2.imwrite('保存先のパスとファイル名', img)

画像を表示する

python
cv2.imshow('window name', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

画像を反転する

python
# 上下反転
flip_img = cv2.flip(img_src, 0)
# 左右反転
flip_img = cv2.flip(img_src, 1)
# 上下左右反転
flip_img = cv2.flip(img_src, -1)

画像をリサイズする

python
resize_img = cv2.resize(img, (width, height))
cv2.imwrite('保存先のパスとファイル名', resize_img)

画像を回転させる

python
size = tuple([img.shape[1], img.shape[0]])
center = tuple([int(size[0]/2), int(size[1]/2)])

# 角度(プラスは右、マイナスは左)
angle = 20.0
scale = 1.0

rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
img_dst = cv2.warpAffine(img, rotation_matrix, size, bg_img,
                         flags=cv2.INTER_LINEAR,
                         borderMode=cv2.BORDER_TRANSPARENT,
                        )

cv2.imwrite('保存先のパスとファイル名' ,img_dst)
3
6
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
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?