First of all you should configure JavaCV libraries to your project.
I hope you've already done it.
You can create a threshold image using a specific image that has stored in your hard disk. Or you can use your web cam, take a picture and detect any red, green or blue colors are there.
1st Step : Import these libraries.
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
2nd Step : Initialize min and max BGR ranges of each color (one color at a time). BGR is the default color for capturing from camera (normal 3 channel color)
Red : static CvScalar min = cvScalar(0, 0, 130, 0);
static CvScalar max= cvScalar(140, 110, 255, 0);
Green : static CvScalar min = cvScalar(0, 130, 0, 0);
static CvScalar max= cvScalar(140, 255, 110, 0);
Blue : static CvScalar min = cvScalar(130, 0, 0, 0);
static CvScalar max= cvScalar(255, 110, 140, 0);
3rd Step : Read image and detect color
Here is the code. You can edit this code as you wish.
package detectColors;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
public class ColorsDetect {
static CvScalar min = cvScalar(0, 0, 130, 0);
static CvScalar max= cvScalar(140, 110, 255, 0);
public static void main(String[] args) {
IplImage orgImg = cvLoadImage("rgb.png");
IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);
cvInRangeS(orgImg, min, max, imgThreshold);
cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 13); cvSaveImage("threshold.jpg", imgThreshold);
System.out.print("done");
}
}
********************************* Sample images ***********************************
Hope you enjoyed. Thank you!
No comments:
Post a Comment