Tag Archives: opencv

Adjust HSV of image on Opencv

Opencv provide cvtColor(src,dst,CV_RGB2HSV_FULL);  and cvtColor(img,dst,CV_HSV2RGB_FULL);  methods to convert between RGB color and HSV components. Such that we can adjust HSV of image.

1 2 3

 

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>


using namespace cv;
using namespace std;



/// Global variables
Mat src,img,image;

int elem1 = 255;
int elem2 = 255;
int elem3 = 255;

int const max_elem = 500;


/** Function Headers */

void Hue( int, void* );
void Saturation( int, void* );
void Value( int, void* );
void Processing();

/** @function main */
int main( int argc, char** argv )
{
    /// Load an image
    namedWindow("image");

    createTrackbar( "Huse", "image",&elem1, max_elem,Hue);
    createTrackbar( "Saturation", "image",&elem2, max_elem,Saturation);
    createTrackbar( "Value", "image",&elem3, max_elem,Value);

    src = imread( "C:\\Users\\Hasanaga\\Downloads\\1.jpg" );

    waitKey(0);
    return 0;
}

void Hue(int, void *)
{
    Processing();
}

void Saturation(int, void *)
{
    Processing();
}

void Value(int, void *)
{
    Processing();
}


void Processing()
{
    cvtColor(src,img,CV_RGB2HSV);

    int hue = elem1 - 255;
    int saturation = elem2 - 255;
    int value = elem3 - 255;

    for(int y=0; y<img.cols; y++)
    {
        for(int x=0; x<img.rows; x++)
        {
            int cur1 = img.at<Vec3b>(Point(y,x))[0];
            int cur2 = img.at<Vec3b>(Point(y,x))[1];
            int cur3 = img.at<Vec3b>(Point(y,x))[2];
            cur1 += hue;
            cur2 += saturation;
            cur3 += value;

            if(cur1 < 0) cur1= 0; else if(cur1 > 255) cur1 = 255;
            if(cur2 < 0) cur2= 0; else if(cur2 > 255) cur2 = 255;
            if(cur3 < 0) cur3= 0; else if(cur3 > 255) cur3 = 255;

            img.at<Vec3b>(Point(y,x))[0] = cur1;
            img.at<Vec3b>(Point(y,x))[1] = cur2;
            img.at<Vec3b>(Point(y,x))[2] = cur3;
        }
    }

    cvtColor(img,image,CV_HSV2RGB);
    imshow( "image", image );

}

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Flappy Bird PC version – with QT 4.8 & OpenCV Source code

 

It is a simple game like “Flappy Bird”. Written with OpenCV and QT 4.8 (C++) is only for training.


flappy_bird

 

Continue reading

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Simple AVI player with OpenCV QT 4.8 (Playing video file with slider position)

I use iplimage in here but CV::Mat is always advisable.  We change position during video playback with qSlider.

demo
We need the following tools to create a simple player:

  • Qtimer 
  • CvCapture
  • QSlider
  • QFileDialog
  • PushButton

Continue reading

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

OpenCV – cvHoughLines2 example on QT

#include <QtCore/QCoreApplication>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>


IplImage *image, *image1, *image2;


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);



    image  = cvLoadImage("C:\\Temp\\99.jpg");

    IplImage* image1 = cvCreateImage( cvGetSize(image), 8, 1 );

    IplImage* image2 = cvCreateImage( cvGetSize(image), 8, 3 );

    CvMemStorage* storage = cvCreateMemStorage(0);

    CvSeq* lines = 0;


    cvCanny( image, image1, 50, 200, 3 );

    cvCvtColor( image1, image2, CV_GRAY2BGR );


    lines = cvHoughLines2( image1, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 );

    for( int i = 0; i < lines->total; i++ )
    {
        CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
        cvLine( image2, line[0], line[1], CV_RGB(255,0,0), 3, 8 );
    }




    cvShowImage( "Original", image );
    cvShowImage( "cvHoughLines2", image2 );


    return a.exec();

}


 

3

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

OpenCV – cvFilter2D example on QT

#include <QtCore/QCoreApplication>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>

IplImage *image, *image1, *image2;;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    image  = cvLoadImage("C:\\Temp\\99.jpg");

    image1 = cvCreateImage(cvSize(image->width,image->height),8,1);
    image2 = cvCreateImage(cvSize(image->width,image->height),8,1);

    float kernel_vertical[]  = { -1,  0, 1,
                                   -2,  0, 2,
                                   -1,  0, 1};

    float kernel_horizontal[]  = { 1,  2, 1,
                                 0,  0, 0,
                                -1, -2,-1};

    //Vertical filter

    CvMat kernel_ver=cvMat(3,3,CV_32FC1,kernel_vertical);

    cvConvertImage(image,image1,CV_GRAY2BGR);

    cvFilter2D(image1, image1, &kernel_ver, cvPoint(-1,-1));

    cvShowImage("Vertical",image1);

    //Horizontal filter

    CvMat kernel_hor=cvMat(3,3,CV_32FC1,kernel_horizontal);

    cvConvertImage(image,image2,CV_GRAY2BGR);

    cvFilter2D(image2, image2, &kernel_hor, cvPoint(-1,-1));

    cvShowImage("Horizontal",image2);

    return a.exec();

}

 

1

2

3

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS