ISP模块之色彩增强算法--HSV空间Saturation通道调整

    色彩增强不同于彩色图像增强,图像增强的一般处理方式为直方图均衡化等,目的是为了增强图像局部以及整体对比度。而色彩增强的目的是为了使的原有的不饱和的色彩信息变得饱和、丰富起来。对应于Photoshop里面的“色相/饱和度”调节选项里面对饱和度的操作。色彩增强的过程,并不改变原有彩色图像的颜色以及亮度信息。

    在我的色彩增强算法模块里面,始终只针对色彩饱和度(Saturation)信息做研究,调整。这样的话,那就不得不介绍HSV颜色空间了,H代表Hue(色彩),S代表Saturation(饱和度),V代表Value,也可用B表示(Brightness,明度),HSV空间也可称作HSB空间。

    HSV空间在wikipedia上的介绍,https://en.wikipedia.org/wiki/HSL_and_HSV 

    下面根据自己的理解介绍一下HSV空间,以及其各通道在Matlab和OpenCV中的不同。

    HSV的圆柱模型

    ISP模块之色彩增强算法--HSV空间Saturation通道调整_第1张图片

    HSV的圆锥模型

    ISP模块之色彩增强算法--HSV空间Saturation通道调整_第2张图片

    从上图可以看出,在HSV空间中,Hue通道的取值从0-360°变化时,颜色从红->黄->绿->青->蓝逐步变化。Saturation从0->1变化时,色彩逐渐加深变成纯色(pure)。Value值从0->1变化时,图像整体亮度增加,V值为0时,图像为全黑,V值为1时,图像为全白

    Matlab RGB色彩空间向HSV转换,采用函数rgb2hsv,转换后的hsv各通道的元素取值范围为[0,1];OpenCV中彩色图像向HSV空间中转换,cvtColor(src,srcHsv,CV_BGR2HSV),转换后H的取值范围为[0,180],S,V的取值范围为[0,255].

   下面介绍自己的算法处理思路,后面会给出完整的Matlab代码: 

   步骤一、给出一张原图src,用PS进行饱和度(Saturation)+40处理后另存为src_40;

   步骤二、将以上两张图像分别转换到hsv空间,提取出饱和度信息,分别为S,S_40;

   步骤三、统计饱和度增加40后,原色彩饱和度与饱和度增量之间的对应关系,即S -- (S_40-S);

   步骤四、对关系S -- (S_40-S)进行二次多项式曲线拟合,得到二次曲线f(x) = p1*x^2 + p2*x + p3;

   为什么是二次?1.对应关系呈现出抛物线形状;2.更高次曲线并没有明显改善拟合性能,且计算消耗会变高。

   步骤五、任意给定输出图像input,根据其色彩饱和度信息,即可进行色彩增强40处理,新的饱和度信息可以表示为S'(x) = S(x) + f(x),得到增强后的色彩信息后返回RGB图像输出;

   步骤六、分别对原图+20,+40,+60后进行饱和度信息统计,并得到相应拟合参数,设置为色彩增强的低、中、高三挡,在实际处理过程中,根据输入图像input自身色彩饱和度信息(均值)自适应选取相应参数进行色彩增强;

   步骤七、按需对某一单独颜色通道进行色彩增强处理,例如绿色范围为105°-135°,在对该范围进行增强的同时,还需对75°-105°,135°-165°进行一半强度的增强,这样才会保证色彩的连续性,不会出现色斑;

   步骤八、按需对色彩(Hue)进行转换;

   代码部分:第一部分用作估计拟合参数,在Curve fitting tool里面对X,Y进行拟合,得到曲线参数。

% Color Enhancement
clc,clear,close all
src1 = imread('src.bmp');
src2 = imread('src_40.bmp');

src1_hsv = rgb2hsv(src1);
src2_hsv = rgb2hsv(src2);

h1 = src1_hsv(:,:,1);
s1 = src1_hsv(:,:,2);
v1 = src1_hsv(:,:,3);

h2 = src2_hsv(:,:,1);
s2 = src2_hsv(:,:,2);
v2 = src2_hsv(:,:,3);
%
meanS1 = mean(s1();
varS1 = std2(s1);
%
meanS2 = mean(s2();
varS2 = std2(s2);
%
deltaS = s2 - s1;
deltaV = v2 - v1;

%% test1 : 观测“原饱和度-饱和度调整增量”的关系 saturation and delta saturation
figure;
oriS = zeros(101,2);
s3 = s1;
j = 1;
for i = 0: 0.01 : 1
oriS(j,1) = i + 0.01;
oriS(j,2) = mean(deltaS(find(s1 > i & s1< i + 0.01)));
j = j + 1;
end
X = oriS(:,1);
Y = oriS(:,2);
XX = oriS(:,1) * 255;
YY = oriS(:,2) * 255;
plot(XX,YY)

   第二部分,对输入图像进行高、中、低三级自适应增强处理


   
   
   
   
  1. %% Color Enhancement Module -- Authored by HuangDao,08/17/2015
  2. % functions: input a image of type BMP or PNG, the program will decide to
  3. % do the Color Enhancement choice for you.There are four types of Enhanced
  4. % intensity - 20,40,60,80.The larger number stands for stronger
  5. % enhancement.
  6. % And we can also choose the simple color channel(eg.R,G,B) to do the
  7. % enhancement.There are also four different types of enhanced intensity.
  8. %
  9. % parameters table
  10. % ------------------------------------------------------------------------
  11. % | Enhanced | MATLAB params | OpenCV params |
  12. % | intensity |p1 p2 p3 | p1 p2 p3 |
  13. % | 20 |-0.1661 0.2639 -0.003626 |-0.0006512 0.2639 -0.9246|
  14. % | 40 |-0.4025 0.6238 -0.0005937 |0.001578 0.6238 -0.1514|
  15. % | 60 |1.332 1.473 -0.01155 |-0.005222 1.473 -2.946 |
  16. % | 80 |-4.813 3.459 -0.004568 |-0.01887 3.459 -1.165 |
  17. % ------------------------------------------------------------------------
  18. clc; clear ;close all
  19. % 载入文件夹
  20. pathName = '.\';
  21. fileType = '*.bmp';
  22. files = dir([pathName fileType]);
  23. len = length(files);
  24. for pic = 5%1:1:len
  25. srcName = files(pic).name;
  26. srcImg = imread(srcName);
  27. srcHSV = rgb2hsv(srcImg);
  28. srcH = srcHSV(:,:,1);
  29. srcS = srcHSV(:,:,2);
  30. srcV = srcHSV(:,:,3);
  31. meanS = mean(srcS(:));
  32. varS = std2(srcS);
  33. %图像整体进行色彩增强处理
  34. if (meanS >= 0.5)
  35. p1 = 0;p2 = 0;p3 = 0;
  36. else if (meanS >= 0.35 && meanS < 0.5)
  37. p1 = -0.1661;p2 = 0.2639;p3 = -0.003626;
  38. else if ( meanS >=0.2 && meanS <0.35)
  39. p1 = -0.4025;p2 = 0.6238;p3 = -0.0005937;
  40. else
  41. p1 = 1.332;p2 = 1.473;p3 = -0.01155;
  42. end
  43. end
  44. end
  45. dstS = srcS + p1*srcS.*srcS + p2*srcS + p3 ;
  46. dstHSV = srcHSV;
  47. dstHSV(:,:,2) = dstS;
  48. dstImg = hsv2rgb(dstHSV);
  49. figure;imshow(srcImg);
  50. figure;imshow(dstImg);
  51. %指定R,G,B通道进行色彩增强处理,红色范围([225-255]),绿色范围(75-[105-135]-165),蓝色范围([-15-15])
  52. p11 = -0.4025;p21 = 0.6238;p31 = -0.0005937;%周边杂色调整系数,40
  53. p12 = 1.332; p22 = 1.473; p32 = -0.01155; %纯色区域调整系数,60
  54. compHue = srcH;
  55. GcompS = dstS;
  56. RcompS = dstS;
  57. BcompS = dstS;
  58. channel = 'B';
  59. switch channel
  60. case 'G'
  61. I1 = find(compHue > 0.2083 & compHue <0.2917);
  62. GcompS( I1) = dstS(I1) + dstS( I1) .* dstS( I1)* p11 + dstS( I1)* p21 + p31;
  63. I2 = find(compHue >= 0.2917 & compHue <= 0.3750);
  64. GcompS( I2) = dstS(I2) + dstS( I2) .* dstS( I2)* p12 + dstS( I2)* p22 + p32;
  65. I3 = find(compHue > 0.3750 & compHue <0.4583);
  66. GcompS( I3) = dstS(I3) + dstS( I3) .* dstS( I3)* p11 + dstS( I3)* p21 + p31;
  67. compHSV = dstHSV;
  68. compHSV( :, :, 2) = GcompS;
  69. dstImgG = hsv2rgb(compHSV);
  70. figure; imshow( dstImgG);
  71. case ' R'
  72. I1 = find(compHue > 0.875 & compHue <0.9583);
  73. RcompS( I1) = dstS(I1) + dstS( I1) .* dstS( I1)* p11 + dstS( I1)* p21 + p31;
  74. I2 = find(compHue >= 0.9583 | compHue <= 0.0417);
  75. RcompS( I2) = dstS(I2) + dstS( I2) .* dstS( I2)* p12 + dstS( I2)* p22 + p32;
  76. I3 = find(compHue > 0.0417 & compHue <0.125);
  77. RcompS( I3) = dstS(I3) + dstS( I3) .* dstS( I3)* p11 + dstS( I3)* p21 + p31;
  78. compHSV = dstHSV;
  79. compHSV( :, :, 2) = RcompS;
  80. dstImgR = hsv2rgb(compHSV);
  81. figure; imshow( dstImgR);
  82. case ' B'
  83. I1 = find(compHue > 0.5417 & compHue <0.625);
  84. BcompS( I1) = dstS(I1) + dstS( I1) .* dstS( I1)* p11 + dstS( I1)* p21 + p31;
  85. I2 = find(compHue >= 0.625 & compHue <= 0.7083);
  86. BcompS( I2) = dstS(I2) + dstS( I2) .* dstS( I2)* p12 + dstS( I2)* p22 + p32;
  87. I3 = find(compHue > 0.7083 & compHue <0.7917);
  88. BcompS( I3) = dstS(I3) + dstS( I3) .* dstS( I3)* p11 + dstS( I3)* p21 + p31;
  89. compHSV = dstHSV;
  90. compHSV( :, :, 2) = BcompS;
  91. dstImgB = hsv2rgb(compHSV);
  92. figure; imshow( dstImgB);
  93. end
  94. %进行 R, G, B通道之间的互换
  95. convH = zeros(size(srcH,1),size(srcH,2)); % convert
  96. deltaHue = 240;
  97. switch deltaHue
  98. case 120
  99. disp(' R -> G')
  100. convH = srcH + 1/3;
  101. convH(find(convH >= 1)) = convH(find(convH >= 1)) - 1;
  102. case 240
  103. disp('R -> B')
  104. convH = srcH + 2/3;
  105. convH(find(convH >= 1)) = convH(find(convH >= 1)) - 1;
  106. end
  107. convHSV = dstHSV;
  108. convHSV(:,:,1) = convH;
  109. convImg = hsv2rgb(convHSV);
  110. figure;imshow(convImg)
  111. pause();
  112. end


   添加OpenCV代码段:


   
   
   
   
  1. Mat srcHSV,sat,satAdj,dstMerge,dst; //sat - saturation饱和度分量
  2. Mat imageAwb = imread( "m_ImageAwb.bmp");
  3. vector channels,channels1;
  4. double p1,p2,p3;
  5. cvtColor(imageAwb,srcHSV,CV_BGR2HSV);
  6. split(srcHSV,channels);
  7. split(srcHSV,channels1);
  8. sat = channels.at( 1);
  9. Scalar m = mean(sat);
  10. if (m( 0) <= 51.5)
  11. {p1 = -0.002714 , p2 = 0.9498, p3 = -0.5073; AfxMessageBox( "High Color Enhancement!"); } //高
  12. else if (m( 0) > 38.5 && m( 0) <= 89.5)
  13. {p1 = -0.001578 , p2 = 0.6238, p3 = -0.1514;AfxMessageBox( "Middle Color Enhancement!"); } //中
  14. else if (m( 0) > 89.5 && m( 0) <= 127.5)
  15. {p1 = -0.0006512, p2 = 0.2639, p3 = -0.9246;AfxMessageBox( "Low Color Enhancement!");} //低
  16. else
  17. {p1 = 0,p2 = 0,p3 = 0;AfxMessageBox( "No Color Enhancement!");}
  18. satAdj = sat;
  19. for ( int i = 0 ; i < sat.rows;i ++)
  20. {
  21. for ( int j = 0;j < sat.cols;j ++)
  22. {
  23. uchar val = sat.at(i,j);
  24. satAdj.at(i,j) = (val + p1 * val * val + p2 * val + p3) ;
  25. }
  26. }
  27. channels1.at( 1) = satAdj;
  28. merge(channels1,dstMerge);
  29. cvtColor(dstMerge,dst,CV_HSV2BGR);
  30. imwrite( "m_ImageCE.bmp",dst);


   最后给出算法效果图:

Group1.原图->增强后

ISP模块之色彩增强算法--HSV空间Saturation通道调整_第3张图片ISP模块之色彩增强算法--HSV空间Saturation通道调整_第4张图片

Group2.原图->R通道增强->颜色通道改变R2B

ISP模块之色彩增强算法--HSV空间Saturation通道调整_第5张图片ISP模块之色彩增强算法--HSV空间Saturation通道调整_第6张图片ISP模块之色彩增强算法--HSV空间Saturation通道调整_第7张图片

Group3.原图->增强后->颜色通道改变R2B

ISP模块之色彩增强算法--HSV空间Saturation通道调整_第8张图片ISP模块之色彩增强算法--HSV空间Saturation通道调整_第9张图片ISP模块之色彩增强算法--HSV空间Saturation通道调整_第10张图片

完!下篇讲Local Tone Mapping。

你可能感兴趣的:(机器学习)