8

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!

edsonlp1
  • 453
  • 2
  • 9
  • 22

3 Answers3

12

You could also check out the OpenCV CLAHE algorithm. Instead of equalizing the histogram globally, it splits up the image into tiles and equalizes those locally, then stitches them together. This can give a much better result.

With your image in OpenCV 3.0.0:

import cv2
inp = cv2.imread('inp.jpg',0)
clahe = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8,8))
res = clahe.apply(inp)
cv2.imwrite('res.jpg', res)

Gives something pretty nice

After CLAHE

Read more about it here, though it's not super helpful: http://docs.opencv.org/3.1.0/d5/daf/tutorial_py_histogram_equalization.html#gsc.tab=0

Nick Crews
  • 837
  • 10
  • 13
11

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

Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
  • 12
    Non of these address the question i.e. how to adjust contrast – Max Jaderberg Jun 19 '13 at 11:00
  • 3
    it is true that question title is 'how to adjust contrast'. But what he wants to do in question is to highlight the white part in image. So i answered for that with whatever knowledge i have. And ofcourse, in comment, OP told one method works perfect for him. So i think it addressed problem. Taking only title into consideration, you may be right. :) – Abid Rahman K Jun 19 '13 at 18:58
  • 4
    We should change OP's question to avoid searches leading to this. – user391339 May 27 '15 at 05:20
  • @MaxJaderberg Histogram equalization is a form of contrast enhancement, but thresholding just creates a binary image i.e. adjusts the brightness. So this answer definitely answers OP's question, maybe not the title, but the actual question itself was answered here – smac89 Jan 21 '21 at 22:53
1

Although this post is a bit aged: What about using "cvAddWeighted( )" ?

What it does is:

             dst = src1*alpha + src2*beta + gamma

What I understand from applying brightness and contrast is, that one wants to do:

             dst = src*contrast + brightness;

so if

             src1  = input image
             src2  = any image of same type as src1
             alpha = contrast value
             beta  = 0.0
             gamma = brightness value
             dst   = resulting Image (must be of same type as src1)

One should be pretty much done with the task, no?

This approch works for me using CvMat* images

P M
  • 11
  • 3
  • Question was asked in C – Moia Nov 16 '18 at 11:37
  • 1
    `C: void cvAddWeighted(const CvArr* src1, double alpha, const CvArr* src2, double beta, double gamma, CvArr* dst)`...not to be confused with: `C++ addWeighted()` – P M Nov 17 '18 at 18:44