Thursday, February 27, 2014

Displaying the video with Open CV and C

In previous posts you have seen how we can load and display images with the Open CV and C language. Open CV can be used to process videos as well. Processing the videos is simple as the images with the Open CV. This is because the videos are made with the sequence of images. These images are called frames. Generally the videos has the frame rate of 24-30 called as fps. fps represents frames per seconds.

By using Open CV we will grab the each frame from the video from starting frame. After getting the frame from the video to memory we display it as a normal image. If we want to process the grabbed frame we can pass that frame to the processing function and we get result back.

The fallowing code snippet helps you to under stand this process.


#include "stdafx.h" 
#include "cv.h"
#include "highgui.h"
#include <string>
#include <dirent.h>
using namespace std;
CvCapture * capture;
    char* filename;
    string fullpath,path;
    DIR *dp;
    struct dirent *ep;
    const char* full_path;
class base
{
public:
virtual void input()=0;
virtual void process()=0;
virtual void play()=0;
virtual void destroy()=0;
};

class localfile:public base
{
public:
    localfile(char* file);
void input();
void process();
void play();
void destroy();
};
void localfile::input()
{
capture = cvCreateFileCapture(filename);
}
void localfile::process()
{
for ( ; ; )
    {
        play();
        cvWaitKey(33);
    }
}
void localfile::play()
{
 IplImage * frame = cvQueryFrame(capture);
 cvShowImage("Video",frame);
}
void localfile::destroy()
{
cvDestroyWindow("Video");
}
localfile::localfile(char* file)
{
filename=file;
}

int main(int argc,char** argv)
{
    char* path;
    printf("Enter the path to the Video");
    gets(path);
    localfile obj1(path);
    obj1.input();
    obj1.process();
    obj1.destroy();
    return 0;
}

When we run this code it asks for the path to the video file. Based on the path to the video the CvCapture structure will be initialized with the function cvCreateFileCapture which is available in Open CV . Now this structure helps us to get the frames as well as lot information called meta data of the video.

Open CV provides simple function called cvQueryFrame(); this function takes the CvCapture type variable as the argument. and returns the frame for each call to this function. It automatically returns the next frame from the video. After getting the frame we can display this frame as described in the previous posts.

Tuesday, February 25, 2014

Display Images as video with Open CV part-2

In previous post we have seen how we can display the sequence of images as video. But in that process we have seen a problem which can break our code. The basic condition we posed on our code is the folder contains only images. It does not have any other files which are not supported by Open CV.

In this post I will try to explain how we can get rid of this problem by using the regular expressions. These regular expressions are available now in c++ as well.To use this feature we need to include the regex header file. and we have to use the name space std::tr1

#include "stdafx.h"
#include "cv.h"
#include <regex>
#include "highgui.h"
#include <string>
#include <dirent.h>

using namespace std;
using namespace std::tr1;

char* filename;
string fullpath,path;
DIR *dp;
struct dirent *ep;
const char* full_path;

class base
{
public:
    virtual void input()=0;
    virtual void process()=0;
    virtual void destroy()=0;
};

class imagetovideo:public base
{
public:
    imagetovideo(char* folderpath);
    void input();
    void process();
    void destroy();
};

void imagetovideo::input()
{
    const char* mpath=path.c_str();
    dp=opendir(mpath);
}

void imagetovideo::process()
{
    std::tr1::regex rgx("(.jpg$)|(.png$)|(.jpeg$)|(.bmp$)|(.gif)");
    cmatch result;
    cvNamedWindow("Video",0);
    cvResizeWindow("Video",640,320);
        while(ep=readdir(dp))
        {
            regex_search(ep->d_name, result, rgx);
            if(!result.empty())   
            {
                puts(ep->d_name);
                fullpath=path+(ep->d_name);
                full_path=fullpath.c_str();
                puts(full_path);
                IplImage* img=cvLoadImage(full_path);
                cvShowImage("Video",img);
                    if(cvWaitKey(1000)==27)
                    {
                        cvDestroyWindow("Video");
                        break;
                    }
                cvReleaseImage(&img);
            }
            }
}

void imagetovideo::destroy()
{
    closedir (dp);
    cvDestroyWindow("Video");
}

imagetovideo::imagetovideo(char* folderpath)
{
    path=folderpath;
}

int main(int argc,char** argv)
{
    char* path;
    printf("Enter the path to the image");
    gets(path);
    imagetovideo obj1(path);
    obj1.input();
    obj1.process();
    obj1.destroy();
    return 0;
}

If you observe the above code nothing changed from the previous post code snippet. Only we have added the few lines of code which can control the flow of file paths to the cvShowImage function. Now the code is robust. This code will process only the image files. The regular expression filters out the other files which are not images.

The input for the path must be like D:/x/ in this D is the local disk drive number and the x is the folder containing the images. If the folder path is not specified in this manner code will not work.

In this code I used regular expressions to identify the file format. If the file is image then only file is passed to the cvShowImage function. I will explain the regular expression code lines used in this post.

std::tr1::regex rgx("(.jpg$)|(.png$)|(.jpeg$)|(.bmp$)|(.gif)");

observe the above line. You can see the extensions for the images as well as they ended with the dollar symbol. and they are separated with the | symbol. Here $ symbol indicates that in the given string search for the .jpg or .png or .jpeg etc at the end of the string.

cmatch result;

here result is the resul coming out from the  regex_search(ep->d_name, result, rgx); expression. This expression processes the string passed in the first argument the third argument is the regular expression and the second one is the cmatch type.

If this method finds the match it will store that in the cmatch type variable. If the match is found the match will be stored here. So if this not empty the file is image file. else the file is not image.

Saturday, February 22, 2014

Display Images as Video with Open CV

Till now we have seen how we can use Open CV with the C and C++ to display images. You may wonder is there any way to display the images continuously. Yes definitely we can do. Just we have to place the image display code in a loop. Is this enough... Think for a while...
How to write a loop we can use for loop or while loop or any other loop. But before that we must have knowledge of number of images as well as names of the images moreover we must know the directory where the images are stored. From this we can infer that we must get the names of the images from the desired directory. for this we can use dirent.h header file. Which hides all the implementation details of the process. It returns the file names with the file format as well. You can refer this link for more details.
The fallowing code is useful for this.

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <string>
#include <dirent.h>
using namespace std;

char* filename;
    string fullpath,path;
    DIR *dp;
    struct dirent *ep;
const char* full_path;
class base
{
public:
virtual void input()=0;
virtual void process()=0;
virtual void destroy()=0;
};

class imagetovideo:public base
{
public:
imagetovideo(char* folderpath);
void input();
void process();
void destroy();
};

void imagetovideo::input()
{
const char* mpath=path.c_str();
dp=opendir(mpath);
}
void imagetovideo::process()
{
cvNamedWindow("Video",0);
cvResizeWindow("Video",640,320);
int c=0;
while(ep=readdir(dp))
{

c++;
if(c>2)
{
puts(ep->d_name);
fullpath=path+(ep->d_name);
full_path=fullpath.c_str();
puts(full_path);
IplImage* img=cvLoadImage(full_path);
cvShowImage("Video",img);
if(cvWaitKey(1000)==27)
{
cvDestroyWindow("Video");
break;
}
cvReleaseImage(&img);
}
}
}

void imagetovideo::destroy()
{
closedir (dp);
cvDestroyWindow("Video");
}
imagetovideo::imagetovideo(char* folderpath)
{
path=folderpath;
}
int main(int argc,char** argv)
{
char* path;
printf("Enter the path to the image");
gets(path);
imagetovideo obj1(path);
obj1.input();
obj1.process();
obj1.destroy();
return 0;
}

Here we have a base class. Which is implemented as abstract class. Our main class which has code is inherited from the base class. This derived class provides the functionality for our program. It contains three methods and one constructor. The constructor initializes the  path to the folder path. init method initializes the variables available in the dirent header file. The process method gets the file names one by one from the folder and displays the corresponding image in the window. The destroy method releases all the resources used by the Open CV as well as the dirent header file.

Caution: If the folder contains files other than the images the code breaks. Because the dirent header file gets all the files irrespective of the file format even hidden files. We are directly displaying the images with out any filtering. we assumed that the folder contains only images. The reason behind code breaking is the cvload image display only images. If we passed files other than the images this function having no knowledge of the processing. So the code breaks.

Wednesday, February 19, 2014

Displaying Image with Open CV and C++

In first three posts of image processing I explained how we can use Open CV along with C language to display the image. I already stated that Open CV is not limited to C language. We can use C++ language as well with Open CV.
In C++ wrapper of Open CV images are treated as the two dimensional arrays. In other words they are treated as matrices. In this post I will explain how we can use C++ with Open CV.

Fallowing is the code snippet to achieve the above mentioned

#include "StdAfx.h"
#include <iostream>
#include "cv.h"
#include "highgui.h"

using namespace cv;
using namespace std;

class ImageProcessing
{
private:

string path;
Mat image;
int choice,color;

public:

ImageProcessing()
{
cout<<"Enter the path for the Image\n";
cin>>path;

cout<<"Choose from the fallowing\n";
cout<<"1. To load as color image\n";
cout<<"2.To load as gray scale image\n";
cout<<"3. To load the image as unchanged\n";
cin>>color;

cout<<"Select the behaviour of window\n";
cout<<"1. For WINDOW_NORMAL\n";
cout<<"2. For WINDOW_AUTOSIZE\n";
cout<<"3. For WINDOW_OPENGL\n";
cin>>choice;
}

void loadImage()
{
switch(color)
{
case 1:
image=imread(path,CV_LOAD_IMAGE_COLOR);
break;
case 2:
image=imread(path,CV_LOAD_IMAGE_GRAYSCALE);
break;
case 3:
image=imread(path,CV_LOAD_IMAGE_UNCHANGED);
break;
default:
image=imread(path,CV_LOAD_IMAGE_UNCHANGED);
}
}

void displayImage()
{
switch(choice)
{
case 1:
namedWindow("Image",WINDOW_NORMAL);
break;
case 2:
namedWindow("Image",WINDOW_AUTOSIZE);
break;
case 3:
namedWindow("Image",WINDOW_OPENGL);
break;
default :
namedWindow("Image",WINDOW_NORMAL);
}

imshow("Image",image);
cout<<"Press ESC key to close the window";
if(cvWaitKey(1000)==27)
{
destroyWindow("Image");
}
cout<<"Press any key to exit";
}
};

int main()
{
ImageProcessing *ip=new ImageProcessing();
ip->loadImage();
ip->displayImage();
return 0;
}

The code starts with name spaces. We imported two name spaces they are cv and std. cv is name space containing all the declarations for the Open CV. std contains all declarations for the C++. Next we have class having two methods and constructor. Constructor is invoked when the object for the class is  created .
The code inside the constructor gets executed. This initiates the variables of the class with the values provided by the user.
The subsequent methods are useful for loading the image and displaying it. Now closely observe the loadimage method. It has one function from the Open CV imread. Imread is used for loading the image from the memory typically from the secondary memory it initialises the Mat type variables with the images the second parameter is to control the colour of the image.
It's turn for the displayimage method. The code is almost same as the C code the only change is we left the cv term before the method names. But all these methods are defined in the cv namespace.
If you ommit the using namespace cv; command the compiler generates the error saying that I am unable to locate the method.

                                                                                                                                         

Tuesday, February 18, 2014

Displaying Image based on user input

In previous post I explained how to display the image with the help of Open CV and C. In this post I will explain how we can give the user control so that he can control the output based on his input. To do this we need to provide the user a choice to select the window size as well as the path to the image to be displayed.
Fallowing is the code snippet:

void main()
{
char* path;
int choice;
printf("Enter the path to the image");
gets(path);
IplImage* image=cvLoadImage(path);
printf("Select the behaviour of window\n");
printf("1. For WINDOW_NORMAL\n");
printf("2. For WINDOW_AUTOSIZE\n");
printf("3. For WINDOW_OPENGL\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
cvNamedWindow("Image",WINDOW_NORMAL);
break;
case 2:
cvNamedWindow("Image",WINDOW_AUTOSIZE);
break;
case 3:
cvNamedWindow("Image",WINDOW_OPENGL);
break;
default :
cvNamedWindow("Image",WINDOW_NORMAL);
}

cvShowImage("Image",image);
printf("Press ESC key to close the window");
if(cvWaitKey(1000)==27)
{
cvDestroyWindow("Image");
}
printf("Press any key to exit");
getch();
}

I will explain the code line by line. We need two variables to hold the path to the image to be displayed and second one is for to hold the user choice for window behavior. char * path is for holding the path for the image int choice for holding the user choice for holding the window behavior. The rest of the code explanation is same as for the previous post code snippet.
We can add one more functionality to the previous code snippet. that is we can add one more condition that user can use to load the image as color or gray scale image. In first post I explained how to specify the second argument for the cvLoadImage function. which felicitates us to control the behavior of function.

CV_LOAD_IMAGE_COLOR the loaded image is forced to be a 3-channel color image
CV_LOAD_IMAGE_GRAYSCALE the loaded image is forced to be grayscale
CV_LOAD_IMAGE_UNCHANGED the loaded image will be loaded as is

In the fallowing snippet I added the previous condition.
void main()
{
char* path;
int choice,color;
IplImage* image;
printf("Enter the path to the image");
gets(path);
printf("Choose from the fallowing\n");
printf("1. To load as color image\n");
printf("2.To load as gray scale image\n");
printf("3. To load the image as unchanged\n");
scanf("%d",&color);
switch(color)
{
case 1:
image=cvLoadImage(path,CV_LOAD_IMAGE_COLOR);
break;
case 2:
image=cvLoadImage(path,CV_LOAD_IMAGE_GRAYSCALE);
break;
case 3:
image=cvLoadImage(path,CV_LOAD_IMAGE_UNCHANGED);
break;
default:
image=cvLoadImage(path,CV_LOAD_IMAGE_UNCHANGED);
}
printf("Select the behaviour of window\n");
printf("1. For WINDOW_NORMAL\n");
printf("2. For WINDOW_AUTOSIZE\n");
printf("3. For WINDOW_OPENGL\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
cvNamedWindow("Image",WINDOW_NORMAL);
break;
case 2:
cvNamedWindow("Image",WINDOW_AUTOSIZE);
break;
case 3:
cvNamedWindow("Image",WINDOW_OPENGL);
break;
default :
cvNamedWindow("Image",WINDOW_NORMAL);
}

cvShowImage("Image",image);
printf("Press ESC key to close the window");
if(cvWaitKey(1000)==27)
{
cvDestroyWindow("Image");
}
printf("Press any key to exit");
getch();
}


Thursday, February 13, 2014

Displaying the Image with Open CV and C

This is the second post in Image processing. In previous post I explained the very basic code written with Open CV in C language which is useful for loading the image into the program and displaying it.

In this post I will elaborate the steps involved in displaying the image with Open CV and C.

void main()
{
IplImage* image=cvLoadImage("E:\\Capture.jpg");
cvNamedWindow("Image",WINDOW_AUTOSIZE);
cvShowImage("Image",image);
if(cvWaitKey(1000)==27)
{
cvDestroyWindow("Image");
}
}

This is same as the program in previous post. only change is you explicitly created the window to show the image. and you changed the integer passed to the cvWaitKey function. I guess you know that the ASCII value of the escape is 27. To close the window we compared the ASCII value of the escape key with the ASCII value returned by the waitkey function. If those are equal you are destroying the window so image no longer visible to you.

The cvNamedWindow function takes the two arguments first one is the name of the window and second is the parameter which controls the behavior of the window.

WINDOW_NORMAL If this is set, the user can resize the window (no constraint).
WINDOW_AUTOSIZE If this is set, the window size is automatically adjusted to fit the displayed image (see imshow() ), and you cannot change the window size manually.
WINDOW_OPENGL If this is set, the window will be created with OpenGL support.

In next post I will explain the enhanced version of the code.

                                                                                                                                         

Loading Images into the Program


   In real life we are dealing with lot of images. Especially after advancement of computers and processing capabilities of computers image processing become more easy. For  easy processing of images we can use lot of image processing libraries like Open CV, Emgu CV or well known MATLAB. In this tutorial I will demonstrate  how to use Open CV for image processing.

Software requirements:

1. Open CV
2. Visual studio

To process any image first we need to load it into the computer memory. This is very easy. Open CV provides lot of functions among those we have one function which helps us to load the image into the program. Basically Open CV supports C,C++,Java as well as Python. we can use any programming language along with Open CV based on your comfort.

I will use C and C++ to demonstrate the loading of image into the program.

The basic program to display the image using Open CV is

void main()
{
IplImage* image=cvLoadImage("E:\\Capture.jpg");
cvShowImage("Image",image);
cvWaitKey(1);
getch();
}


IplImage* is a structure holding the image data. It points to the memory location of the image. cvLoadImage function is useful for loading the image. Generally it takes to arguments first one is the path to the image to be loaded second one specifies weather the image to be loaded as color image or gray scale image or unchanged. 
CV_LOAD_IMAGE_COLOR the loaded image is forced to be a 3-channel color image
CV_LOAD_IMAGE_GRAYSCALE the loaded image is forced to be grayscale
CV_LOAD_IMAGE_UNCHANGED the loaded image will be loaded as is.

cvShowImage is useful for displaying the image on a window. the first argument takes the name of the window. and second argument takes the variable of type iplImage*. We can generate the image explicitly and we can pass the name of that window to the function other wise the function generates it for us implicitly.

cvWaitKey is the delay function which gives the time for the processor to load the image on to the window.
if forget to specify this function image can not be displayed. it takes the integer as the argument and returns the ASCII key value of the pressed key.

In next post i will try to explain the process in detail with more detailed code and its explanation and examples.

Thursday, July 18, 2013

Variable number of arguments in the function of c language

                  Generally if we want to add the variable number of integers or multiply the variable number of floats/integers we need separate functions for each number of arguments. Fortunately C language provides flexibility for the programmer. It supports the variable number of arguments in the function. The function signature fallows the fallowing syntax:
Returntype functionname(int n,...)

                  This is eclipse notation. The three dots serve for this purpose. To use this facility we have to add the stdarg header file. We need to use va_list this points to the list of the arguments. 
va_start 
   This must be called before using the argument from the list. The syntax of this is va_start(ap,count). First argument is the list and second argument is the number of arguments passed. 

va_arg(ap,datatype):
    This must be called every time to get the value of the argument in a loop. The first parameter is the list and second one is the datatype of the argument.

va_end(ap):
     At the end of program we have to use this to clear the resources used by the list other wise the resources will be there until the program terminates resulting in memory leaks (eating up the RAM).

Consider the fallowing example

#include "stdafx.h"
#include <conio.h>
#include <stdarg.h>
#include <iostream>

using namespace std;

int add(int count,...)
{
int result=0;
va_list ap;
va_start(ap,count);
for(int i=0;i<count;i++)
{
result=result+va_arg(ap,int);
}
va_end(ap);
return result;
}

int main()
{
int r;
r=add(9,1,2,3,4,5,6,7,8,9);
cout<<"\n"<<"the result of summation is :"<<r;
r=add(3,1,2,3);
cout<<"\n"<<"the result of summation is :"<<r;
_getch();
return 0;
}

  The main function calls the two times the add function with the variable number of arguments. Observe that the first parameter is the number of arguments except the first one. The first call to the function has ten arguments the first one is the number of arguments and the renaming arguments are the values to calculate the addition.

Thursday, July 11, 2013

Directory Lister using c language

The command line argument is the folder to be listed. for example if we want ot list hte file names
in the folder images in the c drive we have to specify it as C:\images\.
we need dirent.h file for the code to work. it is open source header file.

#include "stdafx.h"
#include <dirent.h>
#include <conio.h>

int main(int argc,char ** argv)
{
DIR *dp;
struct dirent *ep;
if(argc<2)
{
printf("Insufficinet number of arguments");
return -1;
}
dp = opendir (argv[1]);
while(ep=readdir(dp))
{
puts(ep->d_name);
}
closedir(dp);
puts("Press any key to return");
_getch();
return 0;
}

           The program takes the one argument. that is the folder absolute path to get the file names with the extension. dp is the pointer to the folder or directory to be listed. ep points to the file in the folder. the while loop continuously iterates through the folder or directory. when there is no more files to list then this returns the null then the condition in the while loop becomes void and the loop breaks. ep is the pointer to the currently selected file in the directory. ep->d_name gives the file name currently pointed along with the extension of the file. after completing our task we have to close the directory which is opened in the program.
if we forgot to close then the directory will be closed when we exit the program. based on the need we can modify the code to get the desired files with the specific extension.



Monday, July 8, 2013

Command line arguments in C Language

Command line arguments:
                Generally command line arguments are the parameters taken by the main function of any c language program. The main takes two arguments. First one is the number of arguments available on the command line including the program name and the user specified arguments the second parameter is the character array which originally holds the typed arguments. Characters between the two spaces are interpreted as the argument on the command line. For example on the command line we typed
code.exe visual studio 2010
then the code.exe is the program we wish to execute and the visual is the second command line argument studio is the third and 2010 is the fourth. The main function which takes the command line arguments takes two forms like
                main(int argc, char* argv[])
                main(int argc, char** argv)
In the first form the argument array is specified with the single pointer and the array. In the second one it is specified as the double pointer. Generally when the array is declared the array name acts as the pointer to the first element in the array. In the second case we used double pointer so there is no need of the array.

To access the command line arguments we can fallow the normal array elements accessing rules. But one must remember that the first command line argument is the currently executing program name so if we want to access the user intentionally typed arguments we have to start from the second array element. That is index is 1. The fallowing code snippets help to visualise the command line arguments easily.

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

void main(int argc, char* argv[])
{
int num_arg=argc,i;
printf("Number of arguments on the commandline are %d",num_arg);
for(i=0;i<num_arg;i++)
{
printf("The %d command line argument is %s",i+1,argv[i]);
puts("\n");
}
_getch();
}

the for loop in the code prints the arguments on the command line. one thing we have to remember is the fourth argument 2010 is also considered as the character array so if we want the numerical value we have to convert back to the integer value using some conversion available in the c standard library like "atoi" function. in the output windows we can see the first command line argument is the absolute path of the currently executing program.

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

void main(int argc, char** argv)
{
int num_arg=argc,i;
printf("Number of arguments on the commandline are %d",num_arg);
for(i=0;i<num_arg;i++)
{
printf("The %d command line argument is %s",i+1,argv[i]);
puts("\n");
}
_getch();
}

the second example is another code demonstrating the use of command line arguments. The path for the file to open is passed as the second command line argument of index one and it is used to save the command line arguments in it.

Monday, April 8, 2013

Structure pointer as the function argument

Generally structures are used to store different data types. sometimes we need to pass the structure as the parameter to the function. Arguments are stored in the stack. If the size of the structure is very large then storing that on stack becomes hard and data accessing becomes slow. so we can pass the pointer to the structure as the parameter instead of the structure. This enables the faster data access.

consider the structure

struct data
{
// All the data
};

For this declare a pointer. Structure is also a data type so we can create a pointer to this as the same way we create for the int ,char etc.
for integer we create pointer as int *p=NULL;
p is the pointer of type integer. which always points the integer type variables.
Similarly we can create pointers to the structure.
struct data *dt=NULL;
Here dt is the pointer to the user defined data type data. To access the structure variables we have to use arrow operator ->.

struct var
{
int a;
char c;
};

let p is the pointer to the structure var. To access the integer variable we need statement like var->a.
Generally allocating data to the pointer is good before using it. If we are using the pointer in the same function no need of allotting memory. If we need to use the same pointer f to pass data between functions we need to allot memory using dynamic memory allocation techniques like malloc , calloc in C language and by using new in C++.
We need memory allocation because after returning from functions all the variables initialised and used are deleted from heap except if the variable is used with memory allocation.

var* function1(void)
{
struct var *p=NULL;
p=(struct var*)malloc(sizeof(struct var));;
p->a=10;
p->c="A";
return p;
}

void function2(var* p)
{
int r=p->a;
printf("\n Value of r is %d",r);
}

Here we are using the structure explained above. we are passing structure as pointer from one function to another function. and in the second function we just printing the value of the integer a. We are allotting the memory to pointer dynamically. This is C type memory allocation.
The C++ type allocation is as fallows we need to modify the second statement of the function1.


var* function1(void)
{
struct var *p=NULL;
p=new var;
p->a=10;
p->c="A";
return p;
}

some times we need to hide the type of pointer like we need to pass the structure but the functions should not identify the pointer as pointer to structure. To achieve this we can use void pointers. Just it is casting of pointers.

consider the void pointer void* ptr1=NULL;
strcut var *ptr2=NULL;
ptr2=new var;
Here ptr1 and ptr2 are the two pointers pointing void and structure respectively.

ptr1=(void*)ptr2;


Here just casting operation is done. Now consider the previous example where we are passing pointer to structure as argument. Modify that to pass structure pointer as void pointer.


void* function1(void)
{
struct var *p=NULL;
p=new var;
p->a=10;
p->c="A";
void *ptr=NULL;
ptr=(void*)p;
return ptr;
}
 In the second function we need to convert the void pointer back to pointer to structure. Then second function looks like

void function2(void *vptr)
{
struct var *sptr=NULL;
sptr=(struct var *)vptr;
int r=sptr->a;
printf("\n Value of r is %d",r);
}

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...