JavaCV - Color based thresholding in image using OpenCV

JavaCV - Red color based thresholding (RGB-A space) in image using OpenCV : Full working java source code 

Note that the order of colors is BGR-A not RGB-A. 
Read it more from
//static imports
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
//non-static imports
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class ColorDetect {
    //color range of red like color
    static CvScalar min = cvScalar(0, 0, 130, 0);//BGR-A
    static CvScalar max= cvScalar(140, 110, 255, 0);//BGR-A

    public static void main(String[] args) {
        //read image
        IplImage orgImg = cvLoadImage("colordetectimage.jpg");
        //create binary image of original size
        IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);
        //apply thresholding
        cvInRangeS(orgImg, min, max, imgThreshold);
        //smooth filter- median
        cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 13);
        cvSaveImage("threshold.jpg", imgThreshold);


Input image :
Input test image (red color will be detected )

//Red color detected output image
Red like color detected
#you can alter the range of red color (min, max) .


  1. hi... can u develop corrsponding code 4 android???
    n where are we suppose to save the input image

    1. Hello Asha, in my example the image will be saved at project's folder.
      See my answer to Aman's question for your other question.

  2. hi...I am having same problem as Asha can you tell me how to use this in android platform...pls its urgent

    1. hello Aman and Asha , I haven't used JavaCV in android. But AFAIK, you should use native calls OpenCV functions from Android code. Do more research.

      The following resources will certainly help you.

  3. hello mister, i want to ask, what is code "static CvScalar min = cvScalar(0, 0, 130, 0); static CvScalar max= cvScalar(140, 110, 255, 0);" mean ? what is HSV value ,,

    do you have formula or anything,to calc number cvScalar if i want to change color to detection.... e.g (yellow or green)


    1. As my comment suggest they represent : color range of red like color.
      For HSV Scale, refer to :

      I haven't used any formula. Its just game of hit/trial to achieve close range.

    2. ok thanks for ur suggest.... I almost can detect colors (yellow ang green),

      I can conclude that cvScalar parameter's is cvScalar(G, B, R, A);

    3. You are right about (G, B, R, A)

  4. what do you have coding steganografi with java?

    email to:

  5. HI
    DO you know how to track same color objects in different place with independent way. that meant i need to track object 1, object 2, object 3... likewise i want to get 4 red colored object with each coordinates need each one as independent way

  6. OpenCV Error: Bad argument (Array should be CvMat or IplImage) in unknown function, file ..\..\..\src\opencv\modules\core\src\array.cpp, line 1238
    Exception in thread "main" java.lang.RuntimeException: ..\..\..\src\opencv\modules\core\src\array.cpp:1238: error: (-5) Array should be CvMat or IplImage

    at com.googlecode.javacv.cpp.opencv_core.cvGetSize(Native Method)


Your Comment and Question will help to make this blog better...