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();
}


No comments:

Post a Comment

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