我有一个cv :: Mat,我想从该行的每个像素中减去每第8行的平均值(从0开始,所以是0、8、16等)。
然后我要对列进行相同的操作。
出于某种原因,这意味着第一行中的每个值都是0,但显然不是。
这是我尝试过的:
int N = 8;
//cv::Mat row_mean, col_mean;
//cv::reduce(myMat, row_mean, 0, CV_REDUCE_AVG); - not giving me what I want somehow?
//cv::reduce(myMat, col_mean, 1, CV_REDUCE_AVG);
for(int r = 0; r < myMat.rows; r = r + N)
{
float rowAvg = 0.0;
for(int c = 0; c < myMat.cols; c++)
{
float val = myMat.at<float>(c, r);
if (r == 0) {
std::cout << "Row 0 val at " << c << " is " << val << std::endl;
}
rowAvg += val;
}
rowAvg /= myMat.rows;
std::cout << "Actual mean at r " << r << " is " << rowAvg << std::endl;
myMat.row(r) = myMat.row(r) - rowAvg;
}
for(int c = 0; c < myMat.cols; c = c + N)
{
float colAvg = 0.0;
for(int r = 0; r < myMat.rows; r++)
{
colAvg += myMat.at<float>(c, r);
}
colAvg /= myMat.cols;
std::cout << "Actual mean at c " << c << " is " << colAvg << std::endl;
myMat.col(c) = myMat.col(c) - colAvg;
}
如您所见,我首先尝试使用reduce,但是得到的值似乎不正确。
myMat
只有两个维度,值得澄清。
更新 我得出的结论是,当我使用reduce降低平均值时,我看到的是错误的尺寸。因此,我认为我已修复该问题。所以现在看起来像这样:
cv::Mat row_mean, col_mean;
cv::reduce(myMat, row_mean, 1, CV_REDUCE_AVG);
cv::reduce(myMat, col_mean, 0, CV_REDUCE_AVG);
for(int r = 0; r < myMat.rows; r = r + N)
{
for(int c = 0; c < myMat.cols; c++)
{
myMat.at<cv::Vec3f>(r, c) = myMat.at<cv::Vec3f>(r, c) - row_mean.at<cv::Vec3f>(r, 0);
}
}
for(int c = 0; c < myMat.cols; c = c + N)
{
for(int r = 0; r < myMat.rows; r++)
{
myMat.at<cv::Vec3f>(r, c) = myMat.at<cv::Vec3f>(r, c) - col_mean.at<cv::Vec3f>(0, c);
}
}
当我标准出row_mean Mat时,它开始如下:
[0, 0.020882325, 0;
0, 0.0043014521, 0;
0, 0.0077573429, 0;
0, -0.003039235, 0;
0, 0.0094852885, 0;
...
col_mean看起来像这样:
[0, 0.011529977, 0, 0, 0.017861091, 0, 0, -0.00044186518, 0, 0, 0.0062413695, 0, 0, 0.0060342439, 0, 0, 0.0068696449,...
所以,好像(如Miki所评论的)有三个通道,但是两个通道全为0。
结果似乎仍然不正确。在TIFF容器中查看值时,看起来好像相邻行和列受到了某种影响,尽管这可能是容器尝试呈现它的原因。
我只想确保我现在正在做的事是正确的。
免责声明
我不是c ++程序员!