Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change saturation values with opencv?

Tags:

opencv

In order to add constant value to each pixel's saturation value, I do this in double loops. I wonder if there is any simpler and faster command achieving this.

like image 980
Tae-Sung Shin Avatar asked Dec 16 '11 14:12

Tae-Sung Shin


People also ask

What is saturation in OpenCV?

Saturation gives the purity of a colour. A pure colour has no gray mixed in it. Greater the amount of gray mixed in a colour, lesser the saturation. The saturation value is usually measured from 0 to 100% but in OpenCV the scale for saturation is from 0 to 255.

What is hue saturation and value in OpenCV?

The HSV or Hue, Saturation and Value of a given object is the color space associated with the object in OpenCV where Hue represents the color, Saturation represents the greyness and Value represents the brightness and it is used to solve the problems related to computer vision because of its better performance when ...


2 Answers

For the experiments I attempted, the alternative method of splitting hsv values, adjusting the individual channels and then doing a merge gave a better performance. Below is what worked for me many times faster as compared to looping through pixels:

(h, s, v) = cv2.split(imghsv)
s = s*satadj
s = np.clip(s,0,255)
imghsv = cv2.merge([h,s,v])

Note that I had converted the values to float32 during BGR2HSV transformation to avoid negative values during saturation transformation to due uint8 (default) overflow:

imghsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV).astype("float32")

And converted it back to default uint8 after my saturation adjustment:

imgrgb = cv2.cvtColor(imghsv.astype("uint8"), cv2.COLOR_HSV2BGR)
like image 130
Punit S Avatar answered Sep 21 '22 07:09

Punit S


Mat img(200, 300, CV_8UC1);

Mat saturated;

double saturation = 10;
double scale = 1;

// what it does here is dst = (uchar) ((double)src*scale+saturation); 
img.convertTo(saturated, CV_8UC1, scale, saturation); 

EDIT

If by saturation, you mean the S channel in a HSV image, you need to separe your image in three channels with split(), apply the saturation correction to the S channel, and then put them together with merge().

like image 35
Sam Avatar answered Sep 19 '22 07:09

Sam