OpenCV c ++:absdiff

时间:2017-01-12 11:20:36

标签: c++ opencv

我一直在尝试使用opencv进行速度估算。第一步是灰度,模糊和图像处理,第二步是找到两者之间的绝对差异。 当找到绝对差异时,我得到以下结果:

First scale conversion done:

OpenCV Error: Bad argument (when the input arrays in add/subtract/multiply/divide 
functions have different types, the output array type must be explicitly specified)
in arithm_op, file /opt/opencv/modules/core/src/arithm.cpp, line 683.
terminate called after throwing an instance of 'cv::Exception'

请给我代码:

#include <opencv2/videoio.hpp>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
Mat img,gray, blur, diff, tmp, canny, acc;
char first=0;

namedWindow("gray", CV_WINDOW_AUTOSIZE);
namedWindow("frame", CV_WINDOW_AUTOSIZE);


VideoCapture cap("toy_plane_liftoff.avi");
if(!cap.isOpened())
{
    printf("Error 404: no video found\n");
    return 0;
}
else printf("Capture from file successfull!!\n");

double fps = cap.get(CV_CAP_PROP_FPS);//bonus: frames per second counter
double frames= cap.get(CV_CAP_PROP_FRAME_COUNT);
double currentFrame = cap.get(CAP_PROP_FRAME_COUNT);
cout << "Frame per seconds : " << fps << "\n"<< "Total frames : " << frames << endl;

cap >>img;
acc = Mat::zeros(img.size(), CV_32FC3);

for(;;) {
cap >>img;

if (!cap.grab())
{
    cout << "\n Cannot read the frame from video file" << endl;
    break;
}

Mat floating;
img.copyTo(floating);


cvtColor(floating, gray, CV_RGB2GRAY);
GaussianBlur(gray, blur, Size(3,3), 0, 0, BORDER_DEFAULT );
Canny(blur, canny, 25, 150, 3);

if(first==0)
{
 canny.copyTo(diff);
 canny.copyTo(tmp);
 canny.convertTo(acc,CV_32FC3);
 printf("\n" "First scale conversion done \n \n");
 first=1;
}
else accumulateWeighted(canny, acc, 5, noArray());

acc.copyTo(tmp);
acc.convertTo(tmp,CV_32FC3);
absdiff(canny, tmp, diff);

imshow("gray", blur);
imshow("frame", tmp); //show vid
moveWindow("gray", 30, 100);
moveWindow("frame", 700, 100);

if(waitKey(10) == 27 ||waitKey(10)=='q')
  {
        cout << "press esc to eject!" << endl;
        break;
  }
}
return 0;
}

我认为这可能是第二次没有定义tmp的问题。

编辑:我替换了

acc.copyTo(tmp);
acc.convertTo(tmp,CV_32FC3);

convertScaleAbs(acc,tmp,1,0);

现在一切都很好&#39;

0 个答案:

没有答案