2016-01-03 68 views
0

我正在制作一个项目,我需要改变图像的亮度和对比度,亮度不是亮度。 所以我在一开始代码为cvtColor too Slow

for (int y = 0; y < dst.rows; y++) { 
    for (int x = 0; x < dst.cols; x++) { 

     int b = dst.data[dst.channels() * (dst.cols * y + x) + 0]; 
     int g = dst.data[dst.channels() * (dst.cols * y + x) + 1]; 
     int r = dst.data[dst.channels() * (dst.cols * y + x) + 2]; 
... other processing stuff i'm doing 

和它的好,做起来非常快,但是当我试图让HSV到HSL的转换设置,我需要它reaaaaaaally得到缓慢的L值;

我HSL到的代码HSL线

 cvtColor(dst, dst, CV_BGR2HSV); 

     Vec3b pixel = dst.at<cv::Vec3b>(y, x); // read pixel (0,0) 
     double H = pixel.val[0]; 
     double S = pixel.val[1]; 
     double V = pixel.val[2]; 
      h = H; 
      l = (2 - S) * V; 
      s = s * V; 
      s /= (l <= 1) ? (l) : 2 - (l); 
      l /= 2; 

       /* i will further make here the calcs to set the l i want */ 
      H = h; 
      l *= 2; 
      s *= (l <= 1) ? l : 2 - l; 
      V = (l + s)/2; 
      S = (2 * s)/(l + s); 

      pixel.val[0] = H; 
      pixel.val[1] = S; 
      pixel.val[2] = V; 

      cvtColor(dst, dst, CV_HSV2BGR); 

,我跑了,就慢,所以我采取的线路,看看哪一个是使它慢,我弄清楚它是cvtColor(dst, dst, CV_BGR2HSV); 所以有办法让它比使用cvtCOlor更快,或者它的时间问题是可以处理的事情?

+0

遍历整个图像和变化的像素值是不是一个好主意,并会采取大量的时间,所以我建议你使用内置的OpenCV的方法,作为split()将图像分解为组成通道并分别处理每个通道,还有'multiply()'''divide()','add()'等类型的算术运算实时...我使用内置方法打赌后可以获得10倍的速度提升。 – ZdaR

+1

或者,如果您可以精确定义您想要执行的详细步骤,那么我可以准确地告诉您所需处理所需的方法。 – ZdaR

回答

2

我想(我还没有打开文本编辑器,但似乎)你需要生成HSV中的整个图像,然后调用整个图像一次cvtColor。这意味着你应该调用cvtColor一次而不是每个像素一次。这应该会让你的速度显着提升。

你可以这样做:

cvtColor(dst, dst, CV_BGR2HSV); 

    for (int y = 0; y < dst.rows; y++) { 
     for (int x = 0; x < dst.cols; x++) { 


     Vec3b pixel = dst.at<cv::Vec3b>(y, x); // read current pixel 
     double H = pixel.val[0]; 
     double S = pixel.val[1]; 
     double V = pixel.val[2]; 
      h = H; 
      l = (2 - S) * V; 
      s = s * V; 
      s /= (l <= 1) ? (l) : 2 - (l); 
      l /= 2; 

      H = h; 
      l *= 2; 
      s *= (l <= 1) ? l : 2 - l; 
      V = (l + s)/2; 
      S = (2 * s)/(l + s); 

      pixel.val[0] = H; 
      pixel.val[1] = S; 
      pixel.val[2] = V; 
    } 
} 

cvtColor(dst, dst, CV_HSV2BGR); 
+0

我看到我明白你的意思,但我必须为每个像素设置L,我还没有找到如何设置整个图像的亮度,这就是为什么我设置为每个像素。 – schirrel