9
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.

暗い画像を鮮明にするStretch

Last updated at Posted at 2018-10-08

カメラの画像が、夜の蛍光灯の下などでは、暗すぎて、解析に使えないことがあります。

簡易に、より鮮明にするのに、OpenCVのcv2.equalizeHistがありますが、これはイメージへのDamageが大きい。また、Unsharpなどの手法では暗さは克服できません。

「OpenCVやDlibの使い方で失敗した諸点」に書いたように、以下のStretchですと、Damageが少なく、より明るくて鮮明にできます。

def stretch(self, img):
        # input must be gray
        inImg = img.astype('float64')
        maxv = np.amax(inImg)
        minv = np.amin(inImg)
        factor = 255.0 / (maxv - minv)
        out = (inImg - minv) * factor
        out = cv2.blur(out.astype('uint8'), (3, 3))
        return out

さらに、暗い部分をより鮮明にしたいとき、変換式をいじります。明るい部分をより鮮明にしたければ、0..1に正規化した値に対し、math.sqrtでなくてその代わりにmath.powを使います。

    def stretchLow(self, img):
        # input must be gray
        inImg = img.astype('float64')
        maxv = np.amax(inImg)
        minv = np.amin(inImg)
        mean = np.mean(inImg)
        std = np.std(inImg)
        maxv = min(maxv, mean + 2.0 * std) #あまりに明るい部分は切る
        factor = 255.0 / (maxv - minv)
        out = (inImg - minv) * factor
        out = np.sqrt((out / 256))*256 #小さい値の部分をより広い範囲の値へ対応付ける
        out = cv2.blur(out.astype('uint8'), (3, 3))
        return out
9
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
9
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?