# -*- coding: utf-8 -*- #import opencv import cv2 video = cv2.VideoCapture(\'Wal
ID: 3854160 • Letter: #
Question
# -*- coding: utf-8 -*-
#import opencv
import cv2
video = cv2.VideoCapture('Walk1.mpg')
index=1
# extract frames and saved into the current folder
if video.isOpened():
evl , frame = video.read()
else:
evl = False
while evl:
evl, frame = video.read()
cv2.imwrite(str(index) + '.jpg',frame)
index = index + 1
cv2.waitKey(1)
video.release()
Step 1: read all frames (612 frames in total) (this step has been already done in the preliminary code)
Step 2: for each pixel position, calculate standard deviation along 612 frames
Step 3: set a threshold for standard deviation (the mathematical formulation of the standard deviation is in https://en.wikipedia.org/wiki/Standard_deviation)
Step 4: based on threshold, extract the moving objects (remove static background pixels) and draw a rectangle onto the moving objects, save all frames.
Step 5: rebuild 612 frames to a video, in which moving objects are tracked.
Explanation / Answer
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
OR
#pragma once
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
CvCapture* capture;
IplImage* frame;
capture = cvCaptureFromCAM(0);
trackBar1->Maximum = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
cvReleaseCapture(&capture);
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
if(comboBox1->Text == "")
{
MessageBox::Show(this,"Select Capture Method","Error!!!");
}
if(button2->Text == "Start")
{
if (comboBox1->Text == "Capture From Camera")
{
capture = cvCaptureFromCAM(0);
trackBar1->Minimum = 0;
trackBar1->Maximum = 0;
button2->Text = "Stop";
timer1->Start();
}
else if (comboBox1->Text == "Capture From File")
{
openFileDialog1->Filter = "AVI files (*.avi)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 2;
openFileDialog1->RestoreDirectory = true;
openFileDialog1->FileName ="";
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
char *fileName = (char*) Marshal::StringToHGlobalAnsi(openFileDialog1->FileName).ToPointer();
capture = cvCaptureFromFile(fileName);
trackBar1->Minimum = 0;
trackBar1->Maximum = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT);
button2->Text = "Stop";
timer1->Start();
}
}
}
else if(button2->Text == "Stop")
{
cvReleaseCapture(&capture);
button2->Text = "Start";
timer1->Stop();
}
} private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
try
{
frame = cvQueryFrame(capture);
if(frame != NULL)
{
pictureBox1->Image = gcnew System::Drawing::Bitmap(frame->width,frame->height,frame->widthStep,System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) frame->imageData);
pictureBox1->Refresh();
trackBar1->Value = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES);
double codec_double = cvGetCaptureProperty(capture,CV_CAP_PROP_FOURCC);
label6->Text = "Codec: " + System::Text::Encoding::UTF8->GetString(BitConverter::GetBytes((int)codec_double));
label7->Text = "Time: " + (TimeSpan::FromMilliseconds( cvGetCaptureProperty(capture,CV_CAP_PROP_POS_MSEC) ).ToString())->Substring(0, 8);
label8->Text = "Frame No.: " + (int)cvGetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES);
label9->Text = "Video Resolution: " + (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT) + " X " + (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH);
label11->Text = "Video Frame Rate: " + (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
}
}catch(...){}
} private: System::Void trackBar1_Scroll(System::Object^ sender, System::EventArgs^ e)
{
cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES, trackBar1->Value);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.