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

6

19

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?

Update

I've tried the suggested Image._EqualizeHist function from EmguCv:

string file = @"IMG_20120512_055533.jpg";
Image<Bgr, byte> originalColour = new Image<Bgr, byte>(file);

Image<Bgr, byte> improved = originalColour.Clone();
improved._EqualizeHist();

But get an even worse result (also when first gray scaled):

Am I missing other parameters?

Mousse answered 12/5, 2012 at 5:2 Comment(2)
Stack Overflow provides a free image hosting service, just click on the image button in the toolbar and point it to your image hosted locally on your computer on elsewhere on the web. This actually uploads and mirrors a copy of the image, which ensures that it will always be available, even if the external source goes down. I've already shifted over the images you've posted, but just something to keep in mind for the future.Eniwetok
I don't know why, but equalizehist gives more worse result in such cases.. me too have this problem sometimes..Malady
M
24

I have discussed some techniques here : How can I adjust contrast in OpenCV in C?

Please check it. Below are the results i got when i tried last two methods on your image

1) Thresholding:

Thresholding gives a binary image. If that is what you want you can apply threshold function

2) If grayscale image needed :

enter image description here

Additional :

Morphological closing also work good in your case

img = cv2.imread('home.jpg',0)
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
close = cv2.morphologyEx(gray,cv2.MORPH_CLOSE,kernel1)
div = np.float32(gray)/(close)
res = np.uint8(cv2.normalize(div,div,0,255,cv2.NORM_MINMAX))

(code in Python API)

Result Below:

enter image description here

Malady answered 12/5, 2012 at 5:39 Comment(0)
M
7

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:

enter image description here

And the histograms, respectively:

enter image description here

Maxiemaxilla answered 16/9, 2018 at 22:24 Comment(0)
H
6

I suggest using AdaptiveThreshold. It works by doing local neighbourhood thresholding for every pixel in the image (this really is a big deal when there are gradient backgrounds, a bit stronger than in your image). The blockSize parameter is the size of the neighbourhood, and the processed pixels value must be greater than the average neighbourhood value minus param1.

enter image description here

Here is how to do it in python (it should be very easy to convert to c):

import cv
im = cv.LoadImage("9jU1Um.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
cv.AdaptiveThreshold(im, im, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, 
                                  cv.CV_THRESH_BINARY, blockSize=31, param1=15)
cv.ShowImage('image', im)
cv.WaitKey(0)
Hertfordshire answered 12/5, 2012 at 10:11 Comment(0)
M
4

It is called equalizeHist. I do not know its name in emgu, but the result should be exactly what you need - brighter background, and darker text.

EDIT

To extract only the edges (which is very different from the image enhancement techniques) you can simply apply Canny. Select the two thresholds as 20 and 60, for start, and then increase (or decrease them) keeping a ration of 3:1 between them, until you have a good-looking edge image.

Mohandas answered 12/5, 2012 at 5:11 Comment(0)
A
0

This picture shows a variety of morphological operations. By comparison, you will find that morphological operations are very useful and proficient in image edge segmentation and object detection are very effective

Astrophotography answered 4/8 at 2:37 Comment(0)
C
-1

Either way, you can also check every each pixel. Set it to 0 if less then a defined value and set to 255 if exceed the define value.

Catullus answered 13/5, 2012 at 2:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.