Capturing image and segment

asked 2016-08-03 04:38:46 -0500

@boom gravatar image

updated 2016-08-03 04:40:36 -0500

berak gravatar image

Hello There i want to segment image which is capture from cam so here is the code i wrote with help of @berak.

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;
///////////////////////////////////////////image capture and stop camera//////////////////////////////
int main(int, char**)

    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat frame; // keep it out of the loop, so we can save latest img.
    int64 t0 = getTickCount(); // start time
        cap >> frame; // get a new frame from camera
        int64 t1 = getTickCount(); // time now
        double timeRunning = double(t1-t0)/getTickFrequency();
        if (timeRunning >= 1.0) // 2 seconds
    // write last frame to  disk:
    imwrite("/home/sachin/Desktop/a.jpg", frame);
    // the camera will be deinitialized automatically in VideoCapture destructor

////////////////////////////////////Adaptive segmentation /////////////////////////////

Mat src,dst,img;
   img = imread("/home/sachin/Desktop/a.jpg"); //read the image data in the file "MyPic.JPG" and store it in 'img'

if (img.empty()) //check whether the image is loaded or not
cout << "Error : Image cannot be loaded..!!" << endl;
          //system("pause"); //wait for a key press
return -1;
//cvtColor( src, src_gray, CV_BGR2GRAY );
adaptiveThreshold(src,dst,230,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY ,5,2);

namedWindow("Adaptive", CV_WINDOW_NORMAL); //create a window with the name "MyWindow"
imshow("Adaptive", dst); //display the image which is stored in the 'img' in the "MyWindow" window

waitKey(0); //wait infinite time for a keypress

     destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
    return 0;

here camera capture the image and segment the image but camera can't stop. please help me out.

edit retag flag offensive close merge delete


btw, you can mark your code with mouse here, and press "10101" button for nice formatting !

berak gravatar imageberak ( 2016-08-03 04:40:07 -0500 )edit

" but camera can't stop" -- what does it mean , exactly ? (it does stop for me..)

berak gravatar imageberak ( 2016-08-03 04:56:20 -0500 )edit

i does mean that camera is opening and taking picture and segment it and save segment image but camera is running .My camera light is on.

@boom gravatar image@boom ( 2016-08-03 06:37:42 -0500 )edit

you could add

cap.release();  // add this line to release camera
imwrite("/home/sachin/Desktop/a.jpg", frame);
sturkmen gravatar imagesturkmen ( 2016-08-04 08:45:11 -0500 )edit