减去两个CV_64FC3矩阵并保持负值 [英] Subtracting two CV_64FC3 matrices and keeping the negative values

查看:52
本文介绍了减去两个CV_64FC3矩阵并保持负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,减去两个非浮点矩阵会产生预期结果,但是减去两个CV_64FC3(就此而言,和CV_32F)矩阵会将负值裁剪为0.0 ,并且将结果归一化为0.0和1.0.我知道这些限制并阅读了相关文档,但仍然无法减去两个矩阵.

As far as i know, subtracting two non-float matrices yields expected results but subtracting two CV_64FC3 (and CV_32F for that matter) matrices clips the negative values to 0.0 and normalizes the result between 0.0 and 1.0. I know these limitations and read related documentation but still couldn't subtract two matrices.

    Mat lowpass1, lowpass2, mask;
    
    mask.convertTo(mask, CV_32F);
    lowpass1.convertTo(lowpass1, CV_32F);
    lowpass2.convertTo(lowpass2, CV_32F);           
    
    // all other variables (high_b1, high_a0 etc.) are double 
    // and in the range of -1.0 to 1.0 but never 0.0
    // frame and prev_frame are CV_32F matrices

    lowpass1 = (-high_b1 * lowpass1 + high_a0 * frame + high_a1 * prev_frame) / high_b0;
    lowpass2 = (-low_b1 * lowpass2 + low_a0 * frame + low_a1 * prev_frame) / low_b0;

    mask = lowpass1 - lowpass2

虽然lowpass1和lowpass2包含非零正值,但减法始终为0.我假设它是在裁剪和/或舍入到最接近的整数.

Altough the lowpass1 and lowpass2 contains non zero positive values, the subtraction always gives 0. I'm assuming it's clipping and/or rounding up to the nearest integer.

    mask = lowpass2 - lowpass1

这也提供与上述完全相同的结果.

This also gives the exact same result as mentioned above.

最小的工作示例;

#include <iostream>
#include <vector>
#include <list>
#include <opencv2/opencv.hpp>
    
using std::cout;
using std::string;
using std::list;
using std::vector;
using cv::Mat;

int main() {
    cv::VideoCapture cap("small.mp4");
    const int level = 2;
    vector<Mat> *data = new vector<Mat>;
    vector<vector<Mat>> pyramid;
    pyramid.resize(level);
    Mat frame;

    while (true) {
        cap >> frame;
        if (frame.empty())
            break;
        frame.convertTo(frame, CV_32F, 1.0 / 255.0f);
        data->push_back(frame.clone());

        Mat current = frame.clone();
        for (int i = 0; i < level; i++) {
            Mat down, up;
            if (i == (level - 1)) {
                pyramid[i].push_back(current);
                break;
            }
            cv::pyrDown(current, down);
            cv::pyrUp(down, up, current.size());
            pyramid[i].push_back(current - up);
            current = down;
        }
    }

    double low_a0 = 0.04979798;
    double low_a1 = 0.04979798;
    double low_b0 = 1;
    double low_b1 = -0.90040404;
    double high_a0 = 0.13672874;
    double high_a1 = 0.13672874;
    double high_b0 = 1;
    double high_b1 = -0.72654253;
    
    vector<vector<Mat>> filtered;
    filtered.resize(level);

    for (int i = 1; i < pyramid.size(); i++) {
        Mat lowpass1 = pyramid[i][0], lowpass2 = pyramid[i][0];
        for (int j = 1; j < pyramid[i].size(); j++) {       
            lowpass1 = (-high_b1 * lowpass1 + high_a0 * pyramid[i][j] + high_a1 * pyramid[i][j-1]) / high_b0;
            lowpass2 = (-low_b1 * lowpass2 + low_a0 * pyramid[i][j] + low_a1 * pyramid[i][j - 1]) / low_b0;

            filtered[i].push_back(lowpass1 - lowpass2);
        }
    }

}

源视频; http://techslides.com/demos/sample-videos/small.mp4

推荐答案

问题出在这部分;

        lowpass1 = (-high_b1 * lowpass1 + high_a0 * pyramid[i][j] + high_a1 * pyramid[i][j-1]) / high_b0;
        lowpass2 = (-low_b1 * lowpass2 + low_a0 * pyramid[i][j] + low_a1 * pyramid[i][j - 1]) / low_b0;

由于在公式的左侧和代码的左侧使用了lowpass1和lowpass2,因此它弄乱了某些内容,但我不确定它的作用.更改为此之后;

Since lowpass1 and lowpass2 are used in the formula and the left hand side of the code, it messed up something but I'm not sure what it did exactly. After changing it to this;

            Mat lowpass1_, lowpass2_;
            lowpass1_ = ( high_b1 * lowpass1 + high_a0 * pyramid[i][j] + high_a1 * pyramid[i][j - 1]);
            lowpass2_ = ( low_b1 * lowpass2 + low_a0 * pyramid[i][j] + low_a1 * pyramid[i][j - 1]) ;
            

计算是正确的,没有裁剪为零等.感谢所有答复.

the calculations were correct and didn't clip to zero etc. Thanks for all the replies.

这篇关于减去两个CV_64FC3矩阵并保持负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆