How can I adjust contrast in OpenCV in C?

爱⌒轻易说出口 提交于 2019-11-27 20:30:33

问题


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:

Modified image:

Thanks in advance!


回答1:


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:

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 :

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:




回答2:


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

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




回答3:


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



来源:https://stackoverflow.com/questions/10549245/how-can-i-adjust-contrast-in-opencv-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!