How do I equalize contrast & brightness of images using opencv?

前端 未结 5 770
花落未央
花落未央 2020-12-13 10:53

I\'ve got an image that I\'ve scanned, but the white paper is not white on the screen. Is there a way to equalize the contract/brightness to make the background whiter?

5条回答
  •  甜味超标
    2020-12-13 11:35

    To change brightness and contrast, you can multiply your pixel values and then add some constant to them. (More info on Changing the contrast and brightness of an image, in OpenCV docs.)

    Using python and numpy:

    import cv2 as cv
    import numpy as np
    
    img = cv.imread('b.jpg',0) # loads in grayscale
    
    alpha = 1
    beta = 0
    res = cv.multiply(img, alpha)
    res = cv.add(res, beta)
    

    You can also just use:

    res = cv.convertScaleAbs(img, alpha = alpha, beta = beta)
    

    In your image, you can check in histogram that the maximum values are around 170 (actually, it is 172, if you use img.max()). So, you can multiply your image by 255/172 = 1.48 to increase brightness.

    See the results below:

    And the histograms, respectively:

提交回复
热议问题