openCV中的Absdiff可以编译但显示黑色图像

时间:2017-06-22 09:45:35

标签: c++ opencv ros

我一直在尝试使用absdiff来查找图像中的动作,但不幸的是它失败了,我是OpenCV的新手。编码应该使用diff1来确定是否有任何动作发生,但输出是diff2motionnext_mframe的黑色。同时,current_mframeprev_mframeresult显示灰度图像。同时, void imageCallback(const sensor_msgs::ImageConstPtr&msg_ptr){ CvPoint center; int radius, posX, posY; cv_bridge::CvImagePtr cv_image; //To parse image_raw from rstp try { cv_image = cv_bridge::toCvCopy(msg_ptr, enc::BGR8); } catch (cv_bridge::Exception& e) { ROS_ERROR("cv_bridge exception: %s", e.what()); return; } frame = new IplImage(cv_image->image); //frame now holding raw_image frame1 = new IplImage(cv_image->image); frame2 = new IplImage(cv_image->image); frame3 = new IplImage(cv_image->image); matriximage = cvarrToMat(frame); cvtColor(matriximage,matriximage,CV_RGB2GRAY); //grayscale prev_mframe = cvarrToMat(frame1); cvtColor(prev_mframe,prev_mframe,CV_RGB2GRAY); //grayscale current_mframe = cvarrToMat(frame2); cvtColor(current_mframe,current_mframe,CV_RGB2GRAY); //grayscale next_mframe = cvarrToMat(frame3); cvtColor(next_mframe,next_mframe,CV_RGB2GRAY); //grayscale // Maximum deviation of the image, the higher the value, the more motion is allowed int max_deviation = 20; result=matriximage; //rellocate image in right order prev_mframe = current_mframe; current_mframe = next_mframe; next_mframe = matriximage; //motion=difflmg(prev_mframe,current_mframe,next_mframe); absdiff(prev_mframe,next_mframe,diff1); //Here should show black and white image absdiff(next_mframe,current_mframe,diff2); bitwise_and(diff1,diff2,motion); threshold(motion,motion,35,255,CV_THRESH_BINARY); erode(motion,motion,kernel_ero); imshow("Motion Detection",result); imshow("diff1",diff1); //I tried to output the image but its all black imshow("diff2",diff2); //same here, I tried to output the image but its all black imshow("diff1",motion); imshow("nextframe",next_mframe); imshow("motion",motion); char c =cvWaitKey(3); } 显示清晰而正常的图像。我用它作为我的参考http://manmade2.com/simple-home-surveillance-with-opencv-c-and-raspberry-pi/。我认为所有的图像存储器都加载了相同的帧并进行比较,这就解释了为什么它的音高为黑色。我想念那里有别的方法吗?我正在使用RTSP将相机 RAW 图像传递给 ROS

<script src="https://cdnjs.cloudflare.com/ajax/libs/json2/20160511/json2.js" type="text/javascript"></script>

1 个答案:

答案 0 :(得分:0)

我将cv_bridge方法更改为VideoCap,它似乎运行良好,即使通过将IplImage更改为Mat格式,cv_bridge也无法保存图像。也许还有其他方法,但就目前而言,我将采用这种方法拳头。

VideoCapture cap(0); 
Tracker(void)
{
    //check if camera worked
    if(!cap.isOpened())
    {
        cout<<"cannot open the Video cam"<<endl;
    }
    cout<<"camera is opening"<<endl;

    cap>>prev_mframe;
    cvtColor(prev_mframe,prev_mframe,CV_RGB2GRAY);  // capture 3 frame and convert to grayscale
    cap>>current_mframe;
    cvtColor(current_mframe,current_mframe,CV_RGB2GRAY);  
    cap>>next_mframe;
    cvtColor(next_mframe,next_mframe,CV_RGB2GRAY); 

    //rellocate image in right order
    current_mframe.copyTo(prev_mframe);
    next_mframe.copyTo(current_mframe);
    matriximage.copyTo(next_mframe);

    motion = diffImg(prev_mframe, current_mframe, next_mframe);
}