【发布时间】:2014-06-25 19:35:29
【问题描述】:
我已将图像分成 3 个独立的颜色通道 - 一个蓝色、一个绿色和一个红色。我想通过图像的强度对这些通道中的每一个进行归一化,其中强度 = (red + blue + green)/3。为了清楚起见,我正在尝试制作由三个颜色通道之一组成的图像,除以图像的强度,其中强度由上面的等式描述。 我是 OpenCV 的新手,我认为我做的不正确;显示图像时,所有像素看起来都是黑色的。 我是 OpenCV 的新手(我已经完成了文档附带的教程,但仅此而已) - 关于如何进行这种标准化的任何建议都会非常有帮助。
谢谢!
这是我的尝试:
int main(int argc, char** argv){
Mat sourceImage, I;
const char* redWindow = "Red Color Channel";
const char* greenWindow = "Green Color Channel";
const char* blueWindow = "Blue Color Channel";
if(argc != 2)
{
cout << "Incorrect number of arguments" << endl;
}
/* Load the image */
sourceImage = imread(argv[1], 1);
if(!sourceImage.data)
{
cout << "Image failed to load" << endl;
}
/* First, we have to allocate the new channels */
Mat r(sourceImage.rows, sourceImage.cols, CV_8UC1);
Mat b(sourceImage.rows, sourceImage.cols, CV_8UC1);
Mat g(sourceImage.rows, sourceImage.cols, CV_8UC1);
/* Now we put these into a matrix */
Mat out[] = {b, g, r};
/* Split the image into the three color channels */
split(sourceImage, out);
/* I = (r + b + g)/3 */
add(b, g, I);
add(I, r, I);
I = I/3;
Mat red = r/I;
Mat blue = b/I;
Mat green = g/I;
/* Create the windows */
namedWindow(blueWindow, 0);
namedWindow(greenWindow, 0);
namedWindow(redWindow, 0);
/* Show the images */
imshow(blueWindow, blue);
imshow(greenWindow, green);
imshow(redWindow, red);
waitKey(0);
return 0;
}
【问题讨论】:
-
1.您不需要预先分配 split() 中使用的通道(无论如何它们都会被重新分配/覆盖) 2. r/I 之类的东西会遭受整数除法 3. 不要在 rgb 空间中这样做,转换为 hsv ,拆分,仅操作 h,合并,转换回 rgb
-
谢谢!好的,我摆脱了预分配,我使用了除法函数而不是'/'(这样更好吗?)。另外,您能多解释一下您所说的仅操纵 h 是什么意思吗?
-
除法与/相同。很抱歉我的马虎,-我之前应该问过,你想通过这种标准化来实现什么?
-
别担心——我正在尝试实现一个基本的显着对象检测算法。因此,显着区域将包含 r、g 或 b 颜色通道中的最不可能的值
-
因此对每个通道进行归一化意味着某个区域将在其中一个归一化通道上显得最亮。 (理论上,至少据我了解)
标签: c++ opencv normalization color-channel