Simple motion detection with OpenCV - 간단한 움직임 감지
OpenCV 2019. 9. 15. 17:18 |반응형
Detects motions in the vision and saves an image of it as a PNG file. Adjust the sensitivity value if needed.
카메라 영상의 움직임을 감시하고 움직임이 감지된 순간의 영상을 저장한다. sensitivity 값으로 감도를 조절한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main(int argc, char** argv) { VideoCapture cap(0); Mat frameNew; Mat frameOld; Mat frameDiff; double min, max; int sensitivity = 100; int detectionCount = 0; cap >> frameOld; while (true) { cap >> frameNew; if (frameNew.empty()) break; // Calculates the per-element absolute difference // between two arrays or between an array and a scalar. absdiff(frameNew, frameOld, frameDiff); cvtColor(frameDiff, frameDiff, COLOR_BGR2GRAY); minMaxLoc(frameDiff, &min, &max); if (max > sensitivity) { cout << "Motion detected. (Max: " << max << ")" << endl; // For PNG, it can be the compression level from 0 to 9. // A higher value means a smaller size and longer compression time. vector<int> compression_params; compression_params.push_back(IMWRITE_PNG_COMPRESSION); compression_params.push_back(3); if (imwrite(format("detection_%03d.png", detectionCount++), frameNew, compression_params)) cout << "Image saved." << endl; else cout << "Image not saved." << endl; } imshow("Motion Detectoion", frameDiff); frameNew.copyTo(frameOld); if (waitKey(10) == 27) break; } return 0; } | cs |
<frameDiff>
<Detection_XXX.png>
반응형