我是一名生物学家,对于我的实验工作,我想开发一个可以检测载玻片上任何运动的软件。

我想修改以下代码,以便它可以检测到围绕特定xy坐标而不是整个帧的所需半径圆内的运动。您能否建议需要进行哪些更改?

from SimpleCV import *

from SimpleCV import VirtualCamera
#from time import *
vir = VirtualCamera("video.mpg", "video")
vir.getImage().show()
cam = Camera()
threshold = 0.2 # if mean exceeds this amount do something

while True:
        previous = vir.getImage() #grab a frame
        #time.sleep(0.5) #wait for half a second
        current = vir.getImage() #grab another frame
        diff = current - previous
        lines = diff.findLines(threshold=1, minlinelength=1)
        lines.draw(width=2)
        current.addDrawingLayer(diff.dl())
        matrix = diff.getNumpy()
        mean = matrix.mean()
        current.show()

        if mean >= threshold:
                print "Motion Detected"
                print mean

最佳答案

在C++中使用OpenCV:

//Subtract consecutive frames
cv::Mat matN=cv::imread("frame1.jpg");
cv::Mat matM=cv::imread("frame2.jpg");
cv::Mat matDiff=abs(matM-matN);

//Set region of interest (subframe)
int x(10),y(10),width(30),height(40);
cv::Rect myRegionOfInterest(x,y,width,height);

//Define motion
double Threshold=0.1;
double nze=cv::countNonZero( matDiff(myRegionOfInterest) );
double motionFactor=nze/(width*height*C); //C=255 for uchar, C=1 for binary, etc.
if (motionFactor>Threshold)
 std::cout<<"Motion detected at specific ROI";

关于opencv - 检测视频特定区域内的运动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21283269/

10-16 18:08