Color recognition using openCV 2


Related Post:  Color recognition using openCV

Recognising colors in computer vision using openCV is not so complicated as it is expected. Actually, getting the capture image from webcam or some other advanced cameras, converting the color from RGB to HSV or HSI( not sure it is right ), and extracting the specific color(s) from the converted image, that is done. After that, some filters like CVSMOOTH, CANNY and CVTHRESHOLD can be applied to take off the noises in the previous image.

Give some codes here :

IplImage* src = cvCreateImage( sz, 8, 3 );

IplImage* hsv_image = cvCreateImage( sz, 8, 3);

IplImage* hsv_mask = cvCreateImage( sz, 8, 1);

IplImage* hsv_edge = cvCreateImage( sz, 8, 1);

CvScalar  hsv_min = cvScalar(0, 80, 80, 0);

CvScalar  hsv_max = cvScalar(20, 150, 255, 0);


src = cvQueryFrame( capture);

cvCvtColor(src, hsv_image, CV_BGR2HSV);

cvInRangeS (hsv_image, hsv_min, hsv_max, hsv_mask);

cvSmooth( hsv_mask, hsv_mask, CV_MEDIAN, 13, 0, 0, 0 );

cvCanny(hsv_mask, hsv_edge, 1, 3, 5);

Author: Andol Li

A HCI researcher, a digital media lecturer, an information product designer, and a python/php/java coder.

17 Comments On “ Color recognition using openCV 2”

  1. hi,i am so happy to find your blog,there are so many code and method which are just what i want.Thank you for your sharing ! One problem is i conld not download the source code ,could you solve it or tell me why!Waiting for your reply!

  2. hi,ur code was very useful for me ,i need to know the position of this object
    for(int i = 1;i < sz.height; i++)
    for(int j = 1;j <sz.width; j++)
    if (cvGet2D(hsv_mask,i-1,j).val==s.val &&cvGet2D(hsv_mask,i,j).val==s.val&&cvGet2D(hsv_mask,i+1,j).val&&cvGet2D(hsv_mask,i,j-1).val==s.val&&cvGet2D(hsv_mask,i,j).val==s.val&&cvGet2D(hsv_mask,i,j+1).val)


    i trying this code but it fails

  3. hi Andol, i find that this program is very useful for the people who want to start with opencv, but i wonder that if the code in pdf file is full, i debug it, but there is nothing happen, would you mind if i ask you to tell me the way to run this program. I’m a new comer with opencv. For example: which lib we have to add to input to the input (linker) in the project properties… I’ve added the lib from Opencv to C++.(include, Lib already)
    thanks a lot for your help!!!

    • It is the full code, but, some parameters such like the source image and target colour detection need to be adjusted due to different application environments. Have another try, might work with different parameters.

  4. thank you so much Andol, it was my honour when you replied to me when i’ve just asked, actually, i’m taking a thesis with opencv, but i don’t really know much about iit, i am trying to recognise the basic color as you told, but i don’t really know what to do at first in the code, could you help me with the way to solve the problem. i mean the algorithms.
    thank you for your time.

    • @Ardy
      yes the codes are open for in-commercial use – you can use it freely in your project
      generally tracking a white line is similar like tracking a yellow ball, you need to filter, threshold and split the images to highlight the position of your target. You can get the range of colour you want, and using this range to filter out the white line you want.
      By detecting the line, you may/ or not / need the function houghline.

      • Trims Mr Andol for fast reply.
        Actually i am new with opencv. My plan to combine your code for detect rect and estimation angle with white color. For estimation angle, can you tellme how it works. For example, rect object on wall. By use the webcam, i can detect how degre / the gradient from my webcam to that object..

        Hope you can answer sir,.

Leave a comment
Due to technical adjustments, the comment function is shortly closed and will be re-openning soon. Thanks.

Copyrights 2006-2018 © All rights reserved
Theme Tree2, re-designed by Andol Li, powered by WordPress and Bootsrap
WWW.ANDOL.ME | 浙ICP备15040508号-1
公安备案图标 浙公网安备33010602004018号
Back to top