forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
cmake_minimum_required(VERSION 2.8.12) | ||
|
||
PROJECT(VideoBackgroundRemoval) | ||
SET(CMAKE_CXX_STANDARD 11) | ||
|
||
SET(OpenCV_DIR /home/hp/workfolder/OpenCV-Installation/installation/OpenCV-master/lib/cmake/opencv4) | ||
|
||
######################## EDIT IF REQUIRED #################### | ||
# ###Uncomment the line below and specify the path to OpenCV directory i.e. the path to the OpenCVConfig.cmake file. Check the examples given below. | ||
#SET(OpenCV_DIR Enter-the-path-of-OpenCV-installation-on-your-system) | ||
|
||
|
||
################### OpenCV_DIR Examples ##################### | ||
|
||
### MACOS : /usr/local/Cellar/opencv/3.3.1_1/share/OpenCV/ | ||
|
||
### UBUNTU : /usr/local/share/OpenCV/ | ||
|
||
### WINDOWS : C:\Users\yourname\Documents\opencv-3.3.1\build\install | ||
|
||
############################################################## | ||
|
||
|
||
|
||
|
||
################### ***DO NOT EDIT*** ##################### | ||
|
||
############# Common Instructions for all Users ############ | ||
if(MSVC) | ||
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") | ||
endif() | ||
|
||
find_package( OpenCV REQUIRED ) | ||
|
||
include_directories( ${OpenCV_INCLUDE_DIRS}) | ||
|
||
MACRO(add_example name) | ||
ADD_EXECUTABLE(${name} ${name}.cpp) | ||
TARGET_LINK_LIBRARIES(${name} ${OpenCV_LIBS} ) | ||
ENDMACRO() | ||
|
||
add_example(removeVideoBg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include <opencv2/opencv.hpp> | ||
#include <iostream> | ||
#include <random> | ||
|
||
using namespace std; | ||
using namespace cv; | ||
|
||
int computeMedian(vector<int> elements) { | ||
nth_element(elements.begin(), elements.begin()+elements.size()/2, elements.end()); | ||
//sort(elements.begin(),elements.end()); | ||
|
||
return elements[elements.size()/2]; | ||
} | ||
|
||
cv::Mat compute_median(std::vector<cv::Mat> vec) { | ||
// Note: Expects the image to be CV_8UC3 | ||
cv::Mat medianImg(vec[0].rows, vec[0].cols, CV_8UC3, cv::Scalar(0, 0, 0)); | ||
|
||
for(int row=0; row<vec[0].rows; row++) { | ||
for(int col=0; col<vec[0].cols; col++) { | ||
std::vector<int> elements_B; | ||
std::vector<int> elements_G; | ||
std::vector<int> elements_R; | ||
|
||
for(int imgNumber=0; imgNumber<vec.size(); imgNumber++) { | ||
int B = vec[imgNumber].at<cv::Vec3b>(row, col)[0]; | ||
int G = vec[imgNumber].at<cv::Vec3b>(row, col)[1]; | ||
int R = vec[imgNumber].at<cv::Vec3b>(row, col)[2]; | ||
|
||
elements_B.push_back(B); | ||
elements_G.push_back(G); | ||
elements_R.push_back(R); | ||
} | ||
|
||
medianImg.at<cv::Vec3b>(row, col)[0] = computeMedian(elements_B); | ||
medianImg.at<cv::Vec3b>(row, col)[1] = computeMedian(elements_G); | ||
medianImg.at<cv::Vec3b>(row, col)[2] = computeMedian(elements_R); | ||
} | ||
} | ||
return medianImg; | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
std::string video_file; | ||
// Read video file | ||
if(argc > 1) { | ||
video_file = argv[1]; | ||
} else { | ||
video_file = "video.mp4"; | ||
} | ||
|
||
VideoCapture cap(video_file); | ||
|
||
if(!cap.isOpened()) | ||
cerr << "Error opening video file\n"; | ||
|
||
// Randomly select 25 frames | ||
default_random_engine generator; | ||
uniform_int_distribution<int>distribution(0, cap.get(CAP_PROP_FRAME_COUNT)); | ||
|
||
vector<Mat> frames; | ||
Mat frame; | ||
|
||
for(int i=0; i<25; i++) { | ||
int fid = distribution(generator); | ||
cap.set(CAP_PROP_POS_FRAMES, fid); | ||
Mat frame; | ||
cap >> frame; | ||
if(frame.empty()) | ||
continue; | ||
frames.push_back(frame); | ||
} | ||
|
||
// Calculate the median along the time axis | ||
Mat medianFrame = compute_median(frames); | ||
|
||
// Display median frame | ||
imshow("frame", medianFrame); | ||
waitKey(0); | ||
|
||
// Reset frame number to 0 | ||
cap.set(CAP_PROP_POS_FRAMES, 0); | ||
|
||
// Convert background to grayscale | ||
Mat grayMedianFrame; | ||
cvtColor(medianFrame, grayMedianFrame, COLOR_BGR2GRAY); | ||
|
||
// Loop over all frames | ||
while(1) { | ||
// Read frame | ||
cap >> frame; | ||
|
||
if (frame.empty()) | ||
break; | ||
|
||
// Convert current frame to grayscale | ||
cvtColor(frame, frame, COLOR_BGR2GRAY); | ||
|
||
// Calculate absolute difference of current frame and the median frame | ||
Mat dframe; | ||
absdiff(frame, grayMedianFrame, dframe); | ||
|
||
// Threshold to binarize | ||
threshold(dframe, dframe, 30, 255, THRESH_BINARY); | ||
|
||
// Display Image | ||
imshow("frame", dframe); | ||
waitKey(20); | ||
} | ||
|
||
cap.release(); | ||
return 0; | ||
} |