Showing posts from November, 2012

OpenCV Saving Image From Video

This example save snapshot form the loaded video with a name of current time. Note that this code uses struct tm and time_t for accessing system time. And the code is written for Linux platform. Below is the code.
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc_c.h" #include <stdio.h>> int main(int argc, char *argv[]) {     int i=0;     char image[1000];     cvNamedWindow("mainWin");     CvCapture* capture = cvCreateFileCapture( argv[1]);     if(argv[1]==NULL){         printf("Cannot laod video\n");         return 0;     }     IplImage* frame,*output;     struct tm *local;     time_t t;     printf("Press 's' to save image 'Esc' to exit\n");     while (1)     {         frame = cvQueryFrame( capture );         if ( !frame ) break;         cvShowImage( "mainWin", frame );         char c = cvWaitKey(33);         if (c=='s')         {             t = time(NULL);           …

OpenCV Find Biggest Contour

Here is an example to find largest contour from the input image. A contour can be considered as a sequence of points that define a specific object in an image. Here I took contours as the brightest object in the image. Contours can be easily extracted  by thresholding the gray scale image.

   After creating the contour images the function to find biggest contour is called where contour scanner used to scan through each contour and find biggest contour.

If you want C++ code, see my new post , OpenCV Find Biggest Contour Using C++

Here is the C code

#include <iostream>   #include "opencv2/highgui/highgui.hpp"   #include "opencv/cv.h"   using namespace cv;   using namespace std;   cv::Mat FC_FindBiggestContours(cv::Mat src);   CvRect R;   int main()   {     cv::Mat src;     cv::Mat src_binary;     src=cv::imread("src.jpeg");     if(src.empty()){       cout<<"Cannot load the image !"<<endl;       return 0; …

Load Modify and Save Image Using OpenCV Highgui

This is simple example for load image, do some image manipulation and save the result.

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

using namespace std;

int main()
    cv::Mat src;
    cv::Mat src_binary;

        cout<<"Cannot load the image !"<<endl;
        return 0;


     cvtColor(src, src_binary, CV_BGR2GRAY); // Convert BGR  to gray
     cv::threshold(src_binary, src_binary, 100, 255, 0); // Threshold the image to convert binary image

    imwrite("/sdcard/result.jpeg",src_binary); // save image

     cvWaitKey(0); // Wait for key press
     return 0;