Showing posts with label track bar on window. Show all posts
Showing posts with label track bar on window. Show all posts

Saturday, March 8, 2014

Image thresholding using Open CV and C language with track bar

In previous post we have seen the image thresholding opeeeration. In that the user provides the thresholding level before start of the operation after if he wants to change he has to restart the app. In this post I will explain how interactively user can provide the threshold level. Open CV has lot of GUI tools like in built support for windows etc. We can add one track bar for the window to change the threshold value.

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

    int rows,cols,level,i,j,threshold_value = 0;

    IplImage * image;
    IplImage * img;

    char path[20];
    const int max_value=255;
    char* trackbar_value = "Value";
    char* imagedata;
    char * name="Threshold";
   

int main()
{
   
    puts("Enter the path of the image");
    gets(path);

    image=cvLoadImage(path,CV_LOAD_IMAGE_GRAYSCALE);
    rows=image->height;
    cols=image->width;
   
    img=cvLoadImage(path,CV_LOAD_IMAGE_GRAYSCALE);
       
    cvNamedWindow(name,0);
    cvResizeWindow(name,352,288);
    cvCreateTrackbar( trackbar_value,"Threshold", &threshold_value,max_value);
   
    while(true)
    {
    imagedata=(char*)image->imageData;
   
    for(i=0;i<rows*cols;i++)
    {
        img->imageData[i]=imagedata[i]>threshold_value?imagedata[i]:0;
    }
    cvShowImage(name,img);
    int c;
    c = cvWaitKey( 20 );
    if( (char)c == 27 )
    {
        cvReleaseImage(&image);
        cvReleaseImage(&img);
        cvDestroyWindow("Threshold");
          break;
    }
   }

    getch();
    return 0;
}

To add the track bar for the window we can use the cvCreateTrackbar function from the Open CV library. The first argument is the name to be displayed for the bar. Second one is the name of the window on which track must be showed. Third one is thee value we want to change with bar. fourth one is the maximum allowed value on track bar.

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