Java Source Code:
//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.*;
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 int hueLowerR = 160;
static int hueUpperR = 180;
public static void main(String[] args) {
IplImage orgImg = cvLoadImage("finding-red-color-spot.JPG");
cvSaveImage("hsvthreshold.jpg", hsvThreshold(orgImg));
}
static IplImage hsvThreshold(IplImage orgImg) {
// 8-bit, 3- color =(RGB)
IplImage imgHSV = cvCreateImage(cvGetSize(orgImg), 8, 3);
System.out.println(cvGetSize(orgImg));
cvCvtColor(orgImg, imgHSV, CV_BGR2HSV);
// 8-bit 1- color = monochrome
IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);
// cvScalar : ( H , S , V, A)
cvInRangeS(imgHSV, cvScalar(hueLowerR, 100, 100, 0), cvScalar(hueUpperR, 255, 255, 0), imgThreshold);
cvReleaseImage(imgHSV);
cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 13);
// save
return imgThreshold;
}
}
Similar example but on RGB space is given here :
Are you making software based on
ReplyDeleteimage processing ? Then you need to threshold the image
for initial background subtraction, this
task is really cumbersome as it is
dynamic. But no more! With help of this
software you can change the
threshold values dynamically while
camera is capturing live images! In short everything is dynamic and
live. Enjoy!
sourceforge.net/projects/dthresh/
Description
With help of active trackbars you can
change the values of RGB and HSV to
threshold the live image feed from
your camera!
Hi Dhaval, I tried using your app. But i could not run it at my Win7- 64bit machine.
DeleteIts saying -
The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail.
Thanks.
this example is give me a blank image....means a black image.
ReplyDeletewhat is the problem....??
This code uses thresholding operation to detect red color spot on given image. You need to change the color range :
Deletestatic int hueLowerR = YOUR_VALUE_LOWER;
static int hueUpperR = YOUR_VALUE_UPPER;
wht to do to detect all the position of the red spot in the image!!
ReplyDeletei am having a image with the multiple red spot
Hi Ganesh ,
ReplyDeleteNice code thanks for the upload. If you dont mind my asking, in the line
cvInRangeS(imgHSV, cvScalar(hueLowerR, 100, 100, 0), cvScalar(hueUpperR, 255, 255, 0), imgThreshold);
WHy are there four variables ? and what would I need to change to make it detect green ??
thanks
Paddy