Frage

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!

War es hilfreich?

Lösung

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

Andere Tipps

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

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top