3
2

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 3 years have passed since last update.

Pythonで画像のコントラストを調整する方法

Posted at

 学習メモです。

やったこと

 画像のコントラストを調整するPythonスクリプトを書きました。

コード

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('画像.jpg')

# コントラスト
contrast = 128

# コントラスト調整ファクター
factor = (259 *(contrast + 255)) / (255 *(259 - contrast))

# float型に変換
newImage = np.array(img, dtype = 'float64')

# コントラスト調整。(0以下 or 255以上)はクリッピング
newImage = np.clip((newImage[:,:,:] - 128) * factor + 128, 0, 255)

# int型に戻す
newImage = np.array(newImage, dtype = 'uint8')

# 出力
cv2.imwrite('out.png', newImage)

結果

 レナさんの画像で試してみます。

lena.png
オリジナル画像

out.png
コントラスト+128で調整後

 

参考URL

IMAGE PROCESSING ALGORITHMS PART 5: CONTRAST ADJUSTMENT
Algorithms for Adjusting Brightness and Contrast of an Image

3
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?