Showing posts with label Image processing with open cv. Show all posts
Showing posts with label Image processing with open cv. Show all posts

Saturday, May 10, 2014

Extract frame from video when user presses a key in Open CV

Capturing the frames from video using Open CV is very simple. Just we need to use cvSaveImage method from Open CV library. But some times we need to capture the images interactively like when the user presses a key. For doing this we can use the function kbhit(). this function returns non zero values when there is something in the keyboard buffer. So by using this we can interactively extract the frames.

By using the fallowing code we can accomplish this.

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <conio.h>

void main()
{
IplImage * img;
CvCapture * v;
v = cvCreateFileCapture("E:/v.mp4");
int k = 0;
char text[10];
while(true)
{
if(_kbhit())
{
sprintf(text,"%s%d%s","E:/Image",k,".jpeg");
cvSaveImage(text,img);
getch();
}
else
{
img= cvQueryFrame(v);
cvWaitKey(0);
k++;
}
}
_getch();
}

we have one while loop which continuously reads the frames from the video. The if block will be executed when the user presses a key from keyboard else else block gets executed. In else block we are extracting the frames from the video we have one delay loop as well else the the computer reads all the frames one by one with in fraction of seconds.

To change the saved image file name every time we are using char buffor. By using sprintf we are changing the file name. cvSaveImage saves the image on the specifed path supplied as first argument and the image to be saved as second argument. getch() is used to clear the buffor else the if block executed continuously.

Saturday, April 26, 2014

Color Image Rotation in C++ and with Open CV

In previous post explains how we can apply rotation to gray scale image that is single channel images. In this post I will explain the same for multy channel images aka color images.

In real time scenarios some times we need to apply the geometric transformation on the images like rotation etc. This process includes rotating the entire image around the center of the image. It is better to map the rotated image on other image like blank image. In digital computers images are treated as the matrices or two dimensional vectors.

Int his process we find some image points falling on the outside of the boundaries. There are lot of procedures to deal with this type of problems. One is leaving those points. Second one is plotting the rotated image on the larger canvas.

In digital computer this process becomes simple matrix multiplication. In geometry as well this process is represented as the matrix operations. In image processing this type of transformations are called affine transforms.

Mathematically whole precess can be represented in two steps.

x2=cos(t)*(x1-x0)-sin(t)*(y1-y0)+x0
y2=sin(t)*(x1-x0)+cos(t)*(y1-y0)+y0

x2 is the new coordinate of the rotated image corresponding to x1 of original image similarly y2.
t is the required angle of rotation.  x0 and y0 or the center coordinates of the image.

The fallowing code snippet written in open cv and c++ does the exactly same.

 #include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <conio.h>
#include <math.h>

using namespace cv;
using namespace std;

#define PI 3.14159265

void main()
{
    int angle;
    Mat img;
    img=imread("E:/test.jpg");
    Mat nimg((img.rows),(img.cols),CV_8UC3,Scalar(0));
    Mat tm(2,2,CV_32SC1);
    Mat nc(2,2,CV_32SC1);
    Mat oc(2,2,CV_32SC1);
    cout<<"Enter the Rotation angle\n";
    cin>>angle;
    float cosine=cos(angle*PI/180.0);
    float sine=sin(angle*PI/180.0);

    float cx=img.cols/2.0;
    float cy=img.rows/2.0;

    for(int i=0;i<img.rows;i++)
    {
        for(int j=0;j<img.cols;j++)
        {
            int nx=(cosine*(i-cx))-(sine*(j-cy))+cx;
            int ny=(sine*(i-cx))+(cosine*(j-cy))+cy;
            if((nx>=0)&&(ny>=0)&&(nx<=nimg.rows)&&(ny<=nimg.cols))
            {
                           for( int c = 0; c < 3; c++ )
                {
                nimg.at<Vec3b>(nx,ny)[c]=img.at<Vec3b>(i,j)[c];
                }
            }
        }
    }
    imshow("Image",nimg);
    waitKey(100);
     _getch();
}

The difference is just we iterate through the three channels. In Open CV we will get the pixel intensities with .at<Vec3b> array and by using index we can get the individual channel intensities.  

Sunday, April 13, 2014

Image Rotation with Open cv and C++



In real time scenarios some times we need to apply the geometric transformation on the images like rotation etc. This process includes rotating the entire image around the center of the image. It is better to map the rotated image on other image like blank image. In digital computers images are treated as the matrices or two dimensional vectors.

Int his process we find some image points falling on the outside of the boundaries. There are lot of procedures to deal with this type of problems. One is leaving those points. Second one is plotting the rotated image on the larger canvas.

In digital computer this process becomes simple matrix multiplication. In geometry as well this process is represented as the matrix operations. In image processing this type of transformations are called affine transforms.

Mathematically whole precess can be represented in two steps.

x2=cos(t)*(x1-x0)-sin(t)*(y1-y0)+x0
y2=sin(t)*(x1-x0)+cos(t)*(y1-y0)+y0

x2 is the new coordinate of the rotated image corresponding to x1 of original image similarly y2.
t is the required angle of rotation.  x0 and y0 or the center coordinates of the image.

The fallowing code snippet written in open cv and c++ does the exactly same.

#include "stdafx.h" 
#include "cv.h"
#include "highgui.h"
#include <conio.h>
#include <math.h>

using namespace cv;
using namespace std;

#define PI 3.14159265

void main()
{
    int angle;
    Mat img;
    img=imread("E:/test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    Mat nimg(img.rows,img.cols,CV_8UC1,Scalar(0));
    Mat tm(2,2,CV_32SC1);
    Mat nc(2,2,CV_32SC1);
    Mat oc(2,2,CV_32SC1);
    cout<<"Enter the Rotation angle\n";
    cin>>angle;
    float cosine=cos(angle*PI/180.0);
    float sine=sin(angle*PI/180.0);

    float cx=img.cols/2.0;
    float cy=img.rows/2.0;


    for(int i=0;i<img.rows;i++)
    {
        for(int j=0;j<img.cols;j++)
        {
            int nx=(cosine*(i-cx))-(sine*(j-cy))+cx;
            int ny=(sine*(i-cx))+(cosine*(j-cy))+cy;
            if((nx>=0)&&(ny>=0)&&(nx<=img.rows)&&(ny<=img.cols))
            {
            nimg.at<uchar>(nx,ny)=img.at<uchar>(i,j);
            }
        }
    }
   imshow("Image",nimg);
   waitKey(100);
    _getch();
}

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...