【发布时间】:2016-01-03 21:47:46
【问题描述】:
我正在做一个项目,我需要改变图像的亮度和对比度,它是亮度而不是亮度。 所以我一开始的代码是
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 以设置我需要的 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 更快,或者它的时间问题是可以处理的?
【问题讨论】:
-
迭代整个图像并更改像素值不是一个好主意,并且会花费大量时间,因此我建议您使用内置的 OpenCV 方法,例如
split()来拆分图像进入组成通道并单独在每个通道上工作,还有multiply()、divide()、add()等类型的算术运算,它们几乎实时执行......我敢打赌,在使用内置方法后,你可能会得到 10 倍速度倍增。 -
或者如果你能精确定义你想要详细执行的步骤,那么我可以准确地告诉你所需处理所需的方法。