mirror of https://github.com/opencv/opencv.git
parent
0bd287a8f2
commit
50c2443691
11 changed files with 382 additions and 295 deletions
@ -0,0 +1,119 @@ |
||||
package org.opencv.samples.colorblobdetect; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
|
||||
import org.opencv.core.Core; |
||||
import org.opencv.core.CvType; |
||||
import org.opencv.core.Mat; |
||||
import org.opencv.core.MatOfPoint; |
||||
import org.opencv.core.Scalar; |
||||
import org.opencv.imgproc.Imgproc; |
||||
|
||||
public class ColorBlobDetector |
||||
{ |
||||
public void setColorRadius(Scalar radius) |
||||
{ |
||||
mColorRadius = radius; |
||||
} |
||||
|
||||
public void setHsvColor(Scalar hsvColor) |
||||
{ |
||||
double minH = (hsvColor.val[0] >= mColorRadius.val[0]) ? hsvColor.val[0]-mColorRadius.val[0] : 0; |
||||
double maxH = (hsvColor.val[0]+mColorRadius.val[0] <= 255) ? hsvColor.val[0]+mColorRadius.val[0] : 255; |
||||
|
||||
mLowerBound.val[0] = minH; |
||||
mUpperBound.val[0] = maxH; |
||||
|
||||
mLowerBound.val[1] = hsvColor.val[1] - mColorRadius.val[1]; |
||||
mUpperBound.val[1] = hsvColor.val[1] + mColorRadius.val[1]; |
||||
|
||||
mLowerBound.val[2] = hsvColor.val[2] - mColorRadius.val[2]; |
||||
mUpperBound.val[2] = hsvColor.val[2] + mColorRadius.val[2]; |
||||
|
||||
mLowerBound.val[3] = 0; |
||||
mUpperBound.val[3] = 255; |
||||
|
||||
Mat spectrumHsv = new Mat(1, (int)(maxH-minH), CvType.CV_8UC3); |
||||
|
||||
for (int j = 0; j < maxH-minH; j++) |
||||
{ |
||||
byte[] tmp = {(byte)(minH+j), (byte)255, (byte)255}; |
||||
spectrumHsv.put(0, j, tmp); |
||||
} |
||||
|
||||
Imgproc.cvtColor(spectrumHsv, mSpectrum, Imgproc.COLOR_HSV2RGB_FULL, 4); |
||||
|
||||
} |
||||
|
||||
public Mat getSpectrum() |
||||
{ |
||||
return mSpectrum; |
||||
} |
||||
|
||||
public void setMinContourArea(double area) |
||||
{ |
||||
mMinContourArea = area; |
||||
} |
||||
|
||||
public void process(Mat rgbaImage) |
||||
{ |
||||
Mat pyrDownMat = new Mat(); |
||||
|
||||
Imgproc.pyrDown(rgbaImage, pyrDownMat); |
||||
Imgproc.pyrDown(pyrDownMat, pyrDownMat); |
||||
|
||||
Mat hsvMat = new Mat(); |
||||
Imgproc.cvtColor(pyrDownMat, hsvMat, Imgproc.COLOR_RGB2HSV_FULL); |
||||
|
||||
Mat Mask = new Mat(); |
||||
Core.inRange(hsvMat, mLowerBound, mUpperBound, Mask); |
||||
Mat dilatedMask = new Mat(); |
||||
Imgproc.dilate(Mask, dilatedMask, new Mat()); |
||||
|
||||
List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); |
||||
Mat hierarchy = new Mat(); |
||||
|
||||
Imgproc.findContours(dilatedMask, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); |
||||
|
||||
// Find max contour area
|
||||
double maxArea = 0; |
||||
Iterator<MatOfPoint> each = contours.iterator(); |
||||
while (each.hasNext()) |
||||
{ |
||||
MatOfPoint wrapper = each.next(); |
||||
double area = Imgproc.contourArea(wrapper); |
||||
if (area > maxArea) |
||||
maxArea = area; |
||||
} |
||||
|
||||
// Filter contours by area and resize to fit the original image size
|
||||
mContours.clear(); |
||||
each = contours.iterator(); |
||||
while (each.hasNext()) |
||||
{ |
||||
MatOfPoint contour = each.next(); |
||||
if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) |
||||
{ |
||||
Core.multiply(contour, new Scalar(4,4), contour); |
||||
mContours.add(contour); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public List<MatOfPoint> getContours() |
||||
{ |
||||
return mContours; |
||||
} |
||||
|
||||
// Lower and Upper bounds for range checking in HSV color space
|
||||
private Scalar mLowerBound = new Scalar(0); |
||||
private Scalar mUpperBound = new Scalar(0); |
||||
// Minimum contour area in percent for contours filtering
|
||||
private static double mMinContourArea = 0.1; |
||||
// Color radius for range checking in HSV color space
|
||||
private Scalar mColorRadius = new Scalar(25,50,50,0); |
||||
private Mat mSpectrum = new Mat(); |
||||
private List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();; |
||||
} |
@ -1,61 +0,0 @@ |
||||
/* DO NOT EDIT THIS FILE - it is machine generated */ |
||||
#include <jni.h> |
||||
/* Header for class org_opencv_samples_fd_DetectionBaseTracker */ |
||||
|
||||
#ifndef _Included_org_opencv_samples_fd_DetectionBaseTracker |
||||
#define _Included_org_opencv_samples_fd_DetectionBaseTracker |
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBaseTracker |
||||
* Method: nativeCreateObject |
||||
* Signature: (Ljava/lang/String;F)J |
||||
*/ |
||||
JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeCreateObject |
||||
(JNIEnv *, jclass, jstring, jint); |
||||
|
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBaseTracker |
||||
* Method: nativeDestroyObject |
||||
* Signature: (J)V |
||||
*/ |
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeDestroyObject |
||||
(JNIEnv *, jclass, jlong); |
||||
|
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBaseTracker |
||||
* Method: nativeStart |
||||
* Signature: (J)V |
||||
*/ |
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeStart |
||||
(JNIEnv *, jclass, jlong); |
||||
|
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBaseTracker |
||||
* Method: nativeStop |
||||
* Signature: (J)V |
||||
*/ |
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeStop |
||||
(JNIEnv *, jclass, jlong); |
||||
|
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBaseTracker |
||||
* Method: nativeSetFaceSize |
||||
* Signature: (JI)V |
||||
*/ |
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeSetFaceSize |
||||
(JNIEnv *, jclass, jlong, jint); |
||||
|
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBaseTracker |
||||
* Method: nativeDetect |
||||
* Signature: (JJJ)V |
||||
*/ |
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeDetect |
||||
(JNIEnv *, jclass, jlong, jlong, jlong); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
#endif |
@ -0,0 +1,61 @@ |
||||
/* DO NOT EDIT THIS FILE - it is machine generated */ |
||||
#include <jni.h> |
||||
/* Header for class org_opencv_samples_fd_DetectionBasedTracker */ |
||||
|
||||
#ifndef _Included_org_opencv_samples_fd_DetectionBasedTracker |
||||
#define _Included_org_opencv_samples_fd_DetectionBasedTracker |
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBasedTracker |
||||
* Method: nativeCreateObject |
||||
* Signature: (Ljava/lang/String;F)J |
||||
*/ |
||||
JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeCreateObject |
||||
(JNIEnv *, jclass, jstring, jint); |
||||
|
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBasedTracker |
||||
* Method: nativeDestroyObject |
||||
* Signature: (J)V |
||||
*/ |
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeDestroyObject |
||||
(JNIEnv *, jclass, jlong); |
||||
|
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBasedTracker |
||||
* Method: nativeStart |
||||
* Signature: (J)V |
||||
*/ |
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeStart |
||||
(JNIEnv *, jclass, jlong); |
||||
|
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBasedTracker |
||||
* Method: nativeStop |
||||
* Signature: (J)V |
||||
*/ |
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeStop |
||||
(JNIEnv *, jclass, jlong); |
||||
|
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBasedTracker |
||||
* Method: nativeSetFaceSize |
||||
* Signature: (JI)V |
||||
*/ |
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeSetFaceSize |
||||
(JNIEnv *, jclass, jlong, jint); |
||||
|
||||
/*
|
||||
* Class: org_opencv_samples_fd_DetectionBasedTracker |
||||
* Method: nativeDetect |
||||
* Signature: (JJJ)V |
||||
*/ |
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeDetect |
||||
(JNIEnv *, jclass, jlong, jlong, jlong); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
#endif |
@ -1,52 +0,0 @@ |
||||
package org.opencv.samples.fd; |
||||
|
||||
import org.opencv.core.Mat; |
||||
import org.opencv.core.MatOfRect; |
||||
|
||||
public class DetectionBaseTracker |
||||
{ |
||||
public DetectionBaseTracker(String filename, int faceSize) |
||||
{ |
||||
mNativeObj = nativeCreateObject(filename, faceSize); |
||||
} |
||||
|
||||
public void start() |
||||
{ |
||||
nativeStart(mNativeObj); |
||||
} |
||||
|
||||
public void stop() |
||||
{ |
||||
nativeStop(mNativeObj); |
||||
} |
||||
|
||||
public void setMinFaceSize(int faceSize) |
||||
{ |
||||
nativeSetFaceSize(mNativeObj, faceSize); |
||||
} |
||||
|
||||
public void detect(Mat imageGray, MatOfRect faces) |
||||
{ |
||||
nativeDetect(mNativeObj, imageGray.getNativeObjAddr(), faces.getNativeObjAddr()); |
||||
} |
||||
|
||||
public void release() |
||||
{ |
||||
nativeDestroyObject(mNativeObj); |
||||
mNativeObj = 0; |
||||
} |
||||
|
||||
protected long mNativeObj = 0; |
||||
|
||||
protected static native long nativeCreateObject(String filename, int faceSize); |
||||
protected static native void nativeDestroyObject(long thiz); |
||||
protected static native void nativeStart(long thiz); |
||||
protected static native void nativeStop(long thiz); |
||||
protected static native void nativeSetFaceSize(long thiz, int faceSize); |
||||
protected static native void nativeDetect(long thiz, long inputImage, long resultMat); |
||||
|
||||
static |
||||
{ |
||||
System.loadLibrary("detection_base_tacker"); |
||||
} |
||||
} |
@ -0,0 +1,52 @@ |
||||
package org.opencv.samples.fd; |
||||
|
||||
import org.opencv.core.Mat; |
||||
import org.opencv.core.MatOfRect; |
||||
|
||||
public class DetectionBasedTracker |
||||
{ |
||||
public DetectionBasedTracker(String cascadeName, int minFaceSize) |
||||
{ |
||||
mNativeObj = nativeCreateObject(cascadeName, minFaceSize); |
||||
} |
||||
|
||||
public void start() |
||||
{ |
||||
nativeStart(mNativeObj); |
||||
} |
||||
|
||||
public void stop() |
||||
{ |
||||
nativeStop(mNativeObj); |
||||
} |
||||
|
||||
public void setMinFaceSize(int size) |
||||
{ |
||||
nativeSetFaceSize(mNativeObj, size); |
||||
} |
||||
|
||||
public void detect(Mat imageGray, MatOfRect faces) |
||||
{ |
||||
nativeDetect(mNativeObj, imageGray.getNativeObjAddr(), faces.getNativeObjAddr()); |
||||
} |
||||
|
||||
public void release() |
||||
{ |
||||
nativeDestroyObject(mNativeObj); |
||||
mNativeObj = 0; |
||||
} |
||||
|
||||
private long mNativeObj = 0; |
||||
|
||||
private static native long nativeCreateObject(String cascadeName, int minFaceSize); |
||||
private static native void nativeDestroyObject(long thiz); |
||||
private static native void nativeStart(long thiz); |
||||
private static native void nativeStop(long thiz); |
||||
private static native void nativeSetFaceSize(long thiz, int size); |
||||
private static native void nativeDetect(long thiz, long inputImage, long faces); |
||||
|
||||
static |
||||
{ |
||||
System.loadLibrary("detection_based_tacker"); |
||||
} |
||||
} |
Loading…
Reference in new issue