我想把一个Mat图像的所有通道加到一个只有一个和通道的Mat图像中。我试过这样做:
// sum up the channels of the image:
// 1 .store initial nr of rows/columns
int initialRows = frameVid1.rows;
int initialCols = frameVid1.cols;
// 2. check if matrix is continous
if (!frameVid1.isContinuous())
{
frameVid1 = frameVid1.clone();
}
// 3. reshape matrix to 3 color vectors
frameVid1 = frameVid1.reshape(3, initialRows*initialCols);
// 4. convert matrix to store bigger values than 255
frameVid1.convertTo(frameVid1, CV_32F);
// 5. sum up the three color vectors
reduce(frameVid1, frameVid1, 1, CV_REDUCE_SUM);
// 6. reshape to initial size
frameVid1 = frameVid1.reshape(1, initialRows);
// 7. convert back to CV_8UC1
frameVid1.convertTo(frameVid1, CV_8U);但不知何故,reduce并不像矩阵维度那样触及颜色通道。有没有其他函数可以对它们进行汇总?
还要说明为什么在步骤4中使用CV_16U。)不工作?(我不得不在里面放一个CV_32F )
提前感谢!
发布于 2019-12-04 01:46:18
可以使用一行对RGB通道求和
cv::transform(frameVid1, frameVidSum, cv::Matx13f(1,1,1))您可能还需要一行,因为在应用转换之前,您应该将图像转换为某种适当的类型,以避免饱和度(我假定为CV_32FC3)。-Output数组的大小和深度与源相同。
一些解释:
cv::transform可以对每个像素的通道值进行操作。对于每个像素[u,v],如果有第三个参数cv::Matx13f(a, b, c),它将执行以下操作:
frameVidSum[u,v] = frameVid1[u,v].B * a + frameVid1[u,v].G * b + frameVid1[u,v].R * c通过使用第三个参数cv::Matx13f(1,0,1),您将只对蓝色和红色通道求和。
cv::transform非常聪明,您甚至可以使用cv::Matx14f,然后将第四个值添加(偏移)到frameVidSum中的每个像素。
发布于 2015-06-05 21:18:48
每三个元素(在RGB中)是一个相似的颜色。如果你抓取每组3个元素(R,G和B),将它们相加,并将其存储在另一个1通道矩阵中,可能会起作用。在存储之前,您应该使用saturate cast,以避免意外的结果。所以,我认为更好的方法是使用饱和型转换,而不是调整矩阵。
发布于 2015-06-06 01:53:49
看一下cv::split()和cv::add()函数。
您可以使用split函数将图像分割为单独的通道,然后使用add函数添加图像。但在使用add时要小心,因为加法可能会导致值饱和。您可能必须先转换类型,然后再添加。看看这里:http://answers.opencv.org/question/13769/adding-matrices-without-saturation/
https://stackoverflow.com/questions/30666224
复制相似问题