cvtColor 太慢
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 以设置我需要的 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更快,或者它的时间问题是可以处理的?
我认为(我没有打开文本编辑器,但似乎)您需要在 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);
我正在做一个项目,我需要改变图像的亮度和对比度,它是亮度而不是亮度。 所以我一开始的代码是
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更快,或者它的时间问题是可以处理的?
我认为(我没有打开文本编辑器,但似乎)您需要在 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);