Back ground substraction image

asked 2016-08-04 06:14:10 -0500

@boom gravatar image

Hello there i am begineer in image processing as well as coding. i am doing background substaction of image for segmentation.so i get code from opencv decs. here it is

/** * @file bg_sub.cpp * @brief Background subtraction tutorial sample code * @author Domenico D. Bloisi */

//opencv #include "opencv2/imgcodecs.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/videoio.hpp" #include <opencv2 highgui.hpp=""> #include <opencv2 video.hpp=""> //C #include <stdio.h> //C++ #include <iostream> #include <sstream>

  using namespace cv;
  using namespace std;

 // Global variables
Mat frame; //current frame
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
int keyboard; //input from keyboard

/** Function Headers */
void help();
void processVideo(char* videoFilename);
void processImages(char* firstFrameFilename);

void help()
{
cout
<< "--------------------------------------------------------------------------" << endl
<< "This program shows how to use background subtraction methods provided by "  << endl
<< " OpenCV. You can process both videos (-vid) and images (-img)."             << endl
                                                                                << endl
<< "Usage:"                                                                     << endl
<< "./bs {-vid <video filename>|-img <image filename>}"                         << endl
<< "for example: ./bs -vid video.avi"                                           << endl
<< "or: ./bs -img /data/images/1.png"                                           << endl
<< "--------------------------------------------------------------------------" << endl
<< endl;

}

/* * @function main */ int main(int argc, char argv[]) { //print help information help();

//check for the input parameter correctness
if(argc != 3) {
    cerr <<"Incorret input list" << endl;
    cerr <<"exiting..." << endl;
    return EXIT_FAILURE;
}

//create GUI windows
namedWindow("Frame");
namedWindow("FG Mask MOG 2");

//create Background Subtractor objects
pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach

if(strcmp(argv[1], "-vid") == 0) {
    //input data coming from a video
    processVideo(argv[2]);
}
else if(strcmp(argv[1], "-img") == 0) {
    //input data coming from a sequence of images
    processImages(argv[2]);
}
else {
    //error in reading input parameters
    cerr <<"Please, check the input parameters." << endl;
    cerr <<"Exiting..." << endl;
    return EXIT_FAILURE;
}
//destroy GUI windows
destroyAllWindows();
return EXIT_SUCCESS;

}

/* * @function processVideo */ void processVideo(char videoFilename) { //create the capture object VideoCapture capture(videoFilename); if(!capture.isOpened()){ //error in opening the video input cerr << "Unable to open video file: " << videoFilename << endl; exit(EXIT_FAILURE); } //read input data. ESC or 'q' for quitting while( (char)keyboard != 'q' && (char)keyboard != 27 ){ //read the current frame if(!capture.read(frame)) { cerr << "Unable to read next frame." << endl; cerr << "Exiting..." << endl; exit(EXIT_FAILURE); } //update the background model pMOG2->apply(frame, fgMaskMOG2); //get the frame number and write it on the current frame stringstream ss; rectangle(frame, cv::Point(10, 2), cv::Point(100,20), cv::Scalar(255,255,255), -1); ss << capture.get(CAP_PROP_POS_FRAMES); string frameNumberString = ss.str(); putText(frame, frameNumberString.c_str(), cv::Point(15, 15), FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0)); //show the current frame and the fg masks imshow("Frame", frame); imshow("FG Mask MOG 2", fgMaskMOG2); //get the input from the keyboard keyboard = waitKey( 30 ); } //delete capture object capture.release(); }

/* * @function processImages */ processImages(char fistFrameFilename) { //read the first file of the sequence frame = imread(fistFrameFilename); if(frame.empty()){ //error in opening the first image cerr << "Unable to open first image frame: " << fistFrameFilename << endl; exit(EXIT_FAILURE); } //current image filename string fn(fistFrameFilename); //read input data. ESC or 'q' for quitting while( (char)keyboard != 'q' && (char)keyboard != 27 ){ //update the background model pMOG2- ... (more)

edit retag flag offensive close merge delete

Comments

if it's the tutorial code, then there's probably no need to repeat it here.

but what's your question ?

berak gravatar imageberak ( 2016-08-04 06:18:13 -0500 )edit

The question is how can i give input while run code as ./bs_a -img p.jpg it shows error. i only want to background sub. of only 1 image.

@boom gravatar image@boom ( 2016-08-05 00:40:36 -0500 )edit

aaand the error iiiis ?

berak gravatar imageberak ( 2016-08-05 00:47:38 -0500 )edit

in general, it does not make any sense, to apply it to a single image, since it needs to build a model over time. the -img options is of an image sequence (the 1st of some numbered images in a folder), not for a single one.

berak gravatar imageberak ( 2016-08-05 00:52:10 -0500 )edit

Sir i have actually cracked round biscuit i want to detect the crack it it. so first do it background substaction of image for segment then thresholding of this image. now use haugh line transform. but i am segment the image here with watershad algo. but results is not coming. Actually i am Newbie in image Processing And i ma from electric engg. this is my Project work. i am learning it.

@boom gravatar image@boom ( 2016-08-05 02:07:37 -0500 )edit

in short: you need a quite long image sequence , to use the BackgroundSubtractor classes. if you don't have that, all you can do is like an absdiff() of a bg and a fg image.

berak gravatar imageberak ( 2016-08-05 02:18:45 -0500 )edit

Can this do the segment of image like background substraction. Mean i have to fix the Background first and take the picture and apply absdiff(bg,fg);because i want to totally black my background because neglating the effect of light while taking the picture with camera

@boom gravatar image@boom ( 2016-08-09 23:34:30 -0500 )edit