为什么这个运动检测代码不起作用?

时间:2015-05-03 17:18:12

标签: python c++ opencv

我是opencv的新手。我使用opencv 2.4.10 C ++来检测连续帧之间差异的运动。我使用的代码如下。它的问题是即使有运动发生也没有动作。那就是差异窗口总是黑色的。

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
Mat diffImage(Mat t0,Mat t1,Mat t2)
{
  Mat d1,d2,motion;
  absdiff(t2, t1, d1);
  absdiff(t1, t0, d2);
  bitwise_and(d1, d2, motion);
  return motion;
}
int main()
{
    VideoCapture capture(0);
    Mat frame,prev_frame,next_frame,pbwf,bwf,nbwf;
    //capture.open( 0 );
    if ( ! capture.isOpened() ) { printf("--(!)Error opening video capture\n"); return -1; }

    capture.read(prev_frame);
    capture.read(frame);
    capture.read(next_frame);
    cvtColor(prev_frame, pbwf, cv::COLOR_BGR2GRAY);
    cvtColor(frame, bwf, CV_RGB2GRAY);
    cvtColor(next_frame, nbwf, cv::COLOR_BGR2GRAY);
    while(1)
    {
        imshow("diff",diffImage(pbwf,bwf,nbwf));
        imshow("pnwf",pbwf);
        imshow("bnwf",bwf);
        imshow("nbwf",nbwf);
        //prev_frame=frame;
       //frame=next_frame;
       pbwf=bwf;
       bwf=nbwf;
        capture.read(next_frame);
        cvtColor(next_frame, nbwf, cv::COLOR_BGR2GRAY);
        char c=waitKey(5);
        if((char)c==27)
            break;
    } 
    return 0;
}

类似的python脚本运行良好并且符合预期。

import cv2

def diffImg(t0, t1, t2):
  d1 = cv2.absdiff(t2, t1)
  d2 = cv2.absdiff(t1, t0)
  return cv2.bitwise_and(d1, d2)

cam = cv2.VideoCapture(0)

winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)

# Read three images first:
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)

while True:
  cv2.imshow( winName, diffImg(t_minus, t, t_plus) )

  # Read next image
  t_minus = t
  t = t_plus
  t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)

  key = cv2.waitKey(10)
  if key == 27:
    cv2.destroyWindow(winName)
    break

print "Goodbye"

任何人都可以告诉我为什么会这样。感谢

1 个答案:

答案 0 :(得分:0)

一个潜在的问题是,在您的Python代码中,您始终使用COLOR_RGB2GRAY转换,但在C代码中,您可以混合使用COLOR_BGR2GRAYCOLOR_RGB2GRAY。尝试将C代码中的所有颜色转换更改为$USERNAME = 'mygmail@gmail.com'; $PASSWORD = 'mypassword'; $url = "https://accounts.google.com/o/oauth2/auth"; $params = array( "response_type" => "code", "client_id" => "myclient_id", "redirect_uri" => "redirect_uri from setting", "scope" => "https://www.googleapis.com/auth/blogger"); // Request Blogger authentication $request_to = http_build_query($params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POSTFIELDS, $request_to); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie/blogger.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie/blogger.txt'); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $data = curl_exec($ch); // echo $data; $formFields = getFormFields($data); $formFields['Email'] = $USERNAME; $formFields['Passwd'] = $PASSWORD; unset($formFields['PersistentCookie']); $post_string = ''; foreach($formFields as $key => $value) { $post_string .= $key . '=' . urlencode($value) . '&'; } $post_string = substr($post_string, 0, -1); curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); $result = curl_exec($ch); echo $result; function getFormFields($data){ if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) { $inputs = getInputs($matches[1]); return $inputs; } else { die('didnt find login form'); } } function getInputs($form) { $inputs = array(); $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches); if ($elements > 0) { for($i = 0; $i < $elements; $i++) { $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]); if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) { $name = $name[1]; $value = ''; if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) { $value = $value[1]; } $inputs[$name] = $value; } } } return $inputs; } ,看看是否会发生任何变化。

相关问题