Pages

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 http://stackoverflow.com/questions/367449/bgr-color-space
//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);
        //save
        cvSaveImage("threshold.jpg", imgThreshold);
    }
}

Results:

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

11 comments:

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

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

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

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

      http://www.stanford.edu/~zxwang/android_opencv.html
      http://opencv.itseez.com/doc/tutorials/introduction/android_binary_package/android_binary_package_using_with_NDK.html

      Delete
  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)

    thanks

    ReplyDelete
    Replies
    1. As my comment suggest they represent : color range of red like color.
      For HSV Scale, refer to :
      http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/javacv-image-thresholding-hsv-color.html

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

      Delete
    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);

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

      Delete
  4. what do you have coding steganografi with java?

    email to: bayuwinarta@yahoo.com

    ReplyDelete
  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

    ReplyDelete
  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)

    ReplyDelete

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