模糊任何移动的东西

时间:2015-03-01 05:21:40

标签: python opencv

我试图模仿在警察仪表镜头上拍摄的人物。我的策略是模糊任何移动的东西。我发现http://derek.simkowiak.net/motion-tracking-with-python/但它使用旧版本的OpenCV。我无法找到在所使用的OpenCV版本中模糊区域的任何示例。

动作追踪示例中的代码:

for box in trimmed_box_list:
    cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(0,255,0), 2 )

示例代码,用于模糊区域的较新版本的OpenCV:

for (x, y, w, h) in results:

    detected = True
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    sub_face = frame[y:y+h, x:x+w]
    # apply a gaussian blur on this new recangle image
    sub_face = cv2.GaussianBlur(sub_face,(23, 23), 30)
    # merge this blurry rectangle to our final image
    frame[y:y+sub_face.shape[0], x:x+sub_face.shape[1]] = sub_face

如果有人知道如何更改旧版OpenCV的示例,或者如果您有更好的策略来模糊任何移动,请告诉我。

1 个答案:

答案 0 :(得分:1)

您引用的链接对于您的具体想法而言相当复杂;从您所说的内容来看,您不必担心跟踪任何移动的物体,只会模糊任何移动物体。"

为此,我建议简单的背景减法。 Here是一个链接,演示了OpenCV支持的三种不同方法。

以下是该页面的代码:

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

#include < opencv2\opencv.hpp>
#include < opencv2/core/core.hpp>
#include < opencv2/highgui/highgui.hpp>
#include < opencv2/video/background_segm.hpp>


#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing
#pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor
//#pragma comment(lib, "opencv_gpu247d.lib")
//#pragma comment(lib, "opencv_features2d247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#pragma comment(lib, "opencv_ml247d.lib")
//#pragma comment(lib, "opencv_stitching247d.lib");
//#pragma comment(lib, "opencv_nonfree247d.lib");
#pragma comment(lib, "opencv_video247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_objdetect247.lib")
//#pragma comment(lib, "opencv_gpu247.lib")
//#pragma comment(lib, "opencv_features2d247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#pragma comment(lib, "opencv_ml247.lib")
//#pragma comment(lib, "opencv_stitching247.lib");
//#pragma comment(lib, "opencv_nonfree247.lib");
#pragma comment(lib, "opencv_video247d.lib")
#endif 

using namespace cv;
using namespace std;



int main()
{

 //global variables
 Mat frame; //current frame
 Mat resizeF;
 Mat fgMaskMOG; //fg mask generated by MOG method
 Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
 Mat fgMaskGMG; //fg mask fg mask generated by MOG2 method


 Ptr< BackgroundSubtractor> pMOG; //MOG Background subtractor
 Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
 Ptr< BackgroundSubtractorGMG> pGMG; //MOG2 Background subtractor



 pMOG = new BackgroundSubtractorMOG();
 pMOG2 = new BackgroundSubtractorMOG2();
 pGMG = new BackgroundSubtractorGMG();


 char fileName[100] = "C:\\POSCO\\video\\/cctv 2.mov"; //Gate1_175_p1.avi"; //mm2.avi"; //";//_p1.avi";
 VideoCapture stream1(fileName);   //0 is the id of video device.0 if you have only one camera   

 Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1,1) );   

 //unconditional loop   
 while (true) {   
  Mat cameraFrame;   
  if(!(stream1.read(frame))) //get one frame form video   
   break;

  resize(frame, resizeF, Size(frame.size().width/4, frame.size().height/4) );
  pMOG->operator()(resizeF, fgMaskMOG);
  pMOG2->operator()(resizeF, fgMaskMOG2);
  pGMG->operator()(resizeF, fgMaskGMG);
  //morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element); 




  imshow("Origin", resizeF);
  imshow("MOG", fgMaskMOG);
  imshow("MOG2", fgMaskMOG2);
  imshow("GMG", fgMaskGMG);


  if (waitKey(30) >= 0)   
   break;   
 }

}

以此为基础在场景中查找移动物体,然后可以通过几种方式模糊它们。我想到的懒惰方式是模糊图像的副本,然后使用背景减法的结果作为蒙版将模糊图像应用于原始。在每个帧上完成,这将模糊背景减法器认为在逐帧的基础上移动的任何事物。仅模糊背景减法器中足够大的像素组周围的矩形可能是一旦它工作就提高效率的好方法。您甚至可以使用您喜欢的任何噪音预先填充垫子,并使用它而不是模糊的副本。

如果你试图模糊人类,那将会变得更加复杂。人体探测器是完全可行的,但是如果你想确保它们在每一帧都模糊(如果我们在谈论身份保护或其他东西)时会变得复杂,因为探测器可能会错过一些而你会< em>然后想要开始猜测人在帧之间的位置。无论如何,你没有提出要求,但仅供参考。