Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I adjust contrast in OpenCV in C?

Tags:

c

opencv

contrast

I'm just trying to adjust contrast/ brightness in an image in gray scale to highlight whites in that image with Opencv in C. How can I do that? is there any function that makes this task in opencv?

Original image:

enter image description here

Modified image:

enter image description here

Thanks in advance!

like image 233
edsonlp1 Avatar asked May 11 '12 09:05

edsonlp1


1 Answers

I think you can adjust contrast here in two ways:

1) Histogram Equalization :

But when i tried this with your image, result was not as you expected. Check it below:

enter image description here

2) Thresholding :

Here, i compared each pixel value of input with an arbitrary value ( which i took 127). Below is the logic which has inbuilt function in opencv. But remember, output is Binary image, not grayscale as you did.

If (input pixel value >= 127):
    ouput pixel value = 255
else:
    output pixel value = 0

And below is the result i got :

enter image description here

For this, you can use Threshold function or compare function

3) If you are compulsory to get grayscale image as output, do as follows:

(code is in OpenCV-Python, but for every-function, corresponding C functions are available in opencv.itseez.com)

for each pixel in image:
   if pixel value >= 127: add 'x' to pixel value.
   else : subtract 'x' from pixel value. 

( 'x' is an arbitrary value.) Thus difference between light and dark pixels increases.

img = cv2.imread('brain.jpg',0)

bigmask = cv2.compare(img,np.uint8([127]),cv2.CMP_GE)
smallmask = cv2.bitwise_not(bigmask)

x = np.uint8([90])
big = cv2.add(img,x,mask = bigmask)
small = cv2.subtract(img,x,mask = smallmask)
res = cv2.add(big,small)

And below is the result obtained:

enter image description here

like image 88
Abid Rahman K Avatar answered Oct 24 '22 15:10

Abid Rahman K