ColorBlobDetection example. Bug fix. Refactoring.
This commit is contained in:
parent
ef478840af
commit
abe16352d6
@ -1,6 +1,7 @@
|
|||||||
package org.opencv.samples.colorblobdetect;
|
package org.opencv.samples.colorblobdetect;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.opencv.android.Utils;
|
import org.opencv.android.Utils;
|
||||||
@ -8,9 +9,9 @@ import org.opencv.core.Core;
|
|||||||
import org.opencv.core.CvType;
|
import org.opencv.core.CvType;
|
||||||
import org.opencv.core.Mat;
|
import org.opencv.core.Mat;
|
||||||
import org.opencv.core.MatOfPoint;
|
import org.opencv.core.MatOfPoint;
|
||||||
|
import org.opencv.core.Point;
|
||||||
import org.opencv.core.Rect;
|
import org.opencv.core.Rect;
|
||||||
import org.opencv.core.Scalar;
|
import org.opencv.core.Scalar;
|
||||||
import org.opencv.core.Size;
|
|
||||||
import org.opencv.highgui.Highgui;
|
import org.opencv.highgui.Highgui;
|
||||||
import org.opencv.highgui.VideoCapture;
|
import org.opencv.highgui.VideoCapture;
|
||||||
import org.opencv.imgproc.Imgproc;
|
import org.opencv.imgproc.Imgproc;
|
||||||
@ -26,16 +27,27 @@ import android.view.View.OnTouchListener;
|
|||||||
public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchListener {
|
public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchListener {
|
||||||
|
|
||||||
private Mat mRgba;
|
private Mat mRgba;
|
||||||
private static final String TAG = "Example/CollorBlobDetection";
|
|
||||||
|
|
||||||
public boolean mIsColorSelected = false;
|
private boolean mIsColorSelected = false;
|
||||||
public Scalar mSelectedColorRgba = new Scalar(255);
|
private Scalar mSelectedColorRgba = new Scalar(255);
|
||||||
public Scalar mSelectedColorHsv = new Scalar(255);
|
private Scalar mSelectedColorHsv = new Scalar(255);
|
||||||
public Scalar mLowerBound = new Scalar(0);
|
|
||||||
public Scalar mUpperBound = new Scalar(0);
|
// Lower and Upper bounds for range checking in HSV color space
|
||||||
private final int mColorRadius = 25;
|
private Scalar mLowerBound = new Scalar(0);
|
||||||
public Mat mSpectrum = new Mat();
|
private Scalar mUpperBound = new Scalar(0);
|
||||||
public int mSpectrumScale = 2;
|
|
||||||
|
private Mat mSpectrum = new Mat();
|
||||||
|
private int mSpectrumScale = 4;
|
||||||
|
|
||||||
|
// Color radius for range checking in HSV color space
|
||||||
|
private static final Scalar COLOR_RADIUS = new Scalar(25,50,50,0);
|
||||||
|
|
||||||
|
// Minimum contour area in percent for contours filtering
|
||||||
|
private static final double MIN_CONTOUR_AREA = 0.1;
|
||||||
|
|
||||||
|
// Logcat tag
|
||||||
|
private static final String TAG = "Example/CollorBlobDetection";
|
||||||
|
|
||||||
|
|
||||||
public ColorBlobDetectionView(Context context)
|
public ColorBlobDetectionView(Context context)
|
||||||
{
|
{
|
||||||
@ -54,110 +66,81 @@ public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchL
|
|||||||
|
|
||||||
public boolean onTouch(View v, MotionEvent event)
|
public boolean onTouch(View v, MotionEvent event)
|
||||||
{
|
{
|
||||||
// TODO Auto-generated method stub
|
|
||||||
int cols = mRgba.cols();
|
int cols = mRgba.cols();
|
||||||
int rows = mRgba.rows();
|
int rows = mRgba.rows();
|
||||||
|
|
||||||
Log.d(TAG, "Resolition: " + cols + "x" + rows);
|
int xOffset = (getWidth() - cols) / 2;
|
||||||
|
int yOffset = (getHeight() - rows) / 2;
|
||||||
|
|
||||||
int xoffset = (getWidth() - cols) / 2;
|
int x = (int)event.getX() - xOffset;
|
||||||
int yoffset = (getHeight() - rows) / 2;
|
int y = (int)event.getY() - yOffset;
|
||||||
|
|
||||||
Log.d(TAG, "Touch offset " + xoffset + "x" + yoffset);
|
|
||||||
|
|
||||||
Log.d(TAG, "Screen touch coordinates: " + event.getX() + "x" + event.getY());
|
Log.i(TAG, "Touch image coordinates: (" + x + ", " + y + ")");
|
||||||
|
|
||||||
int x = (int)event.getX() - xoffset;
|
if ((x < 0) || (y < 0) || (x > cols) || (y > rows)) return false;
|
||||||
int y = (int)event.getY() - yoffset;
|
|
||||||
|
|
||||||
Log.i(TAG, "Touch coordinates: (" + x + ", " + y + ")");
|
|
||||||
|
|
||||||
if ((x > cols) || (y > rows)) return false;
|
|
||||||
|
|
||||||
Rect TouchedRect = new Rect();
|
Rect touchedRect = new Rect();
|
||||||
|
|
||||||
if (x-4 >= 0)
|
touchedRect.x = (x>4) ? x-4 : 0;
|
||||||
TouchedRect.x = x-4;
|
touchedRect.y = (y>4) ? y-4 : 0;
|
||||||
else
|
|
||||||
TouchedRect.x = 0;
|
|
||||||
|
|
||||||
if (y-4 >= 0)
|
|
||||||
TouchedRect.y = y-4;
|
|
||||||
else
|
|
||||||
TouchedRect.y = 0;
|
|
||||||
|
|
||||||
if (x+4 < mRgba.cols())
|
touchedRect.width = (x+4<mRgba.cols()) ? x + 4 - touchedRect.x : mRgba.width() - touchedRect.x;
|
||||||
TouchedRect.width = x + 4 - TouchedRect.x;
|
touchedRect.height = (y+4 < mRgba.rows()) ? y + 4 - touchedRect.y : mRgba.rows() - touchedRect.y;
|
||||||
else
|
|
||||||
TouchedRect.width = mRgba.width() - TouchedRect.x;
|
|
||||||
|
|
||||||
if (y+4 < mRgba.rows())
|
|
||||||
TouchedRect.height = y+4 - TouchedRect.y;
|
|
||||||
else
|
|
||||||
TouchedRect.height = mRgba.rows() - TouchedRect.y;
|
|
||||||
|
|
||||||
Mat TouchedRegionMat = mRgba.submat(TouchedRect);
|
Mat touchedRegionMatRgba = mRgba.submat(touchedRect);
|
||||||
Mat TouchedRegionMatHsv = new Mat();
|
Mat touchedRegionMatHsv = new Mat();
|
||||||
|
|
||||||
Imgproc.cvtColor(TouchedRegionMat, TouchedRegionMatHsv, Imgproc.COLOR_RGB2HSV_FULL);
|
Imgproc.cvtColor(touchedRegionMatRgba, touchedRegionMatHsv, Imgproc.COLOR_RGB2HSV_FULL);
|
||||||
|
|
||||||
mSelectedColorHsv = Core.sumElems(TouchedRegionMatHsv);
|
mSelectedColorHsv = Core.sumElems(touchedRegionMatHsv);
|
||||||
int DotCount = TouchedRect.width*TouchedRect.height;
|
int pointCount = touchedRect.width*touchedRect.height;
|
||||||
for (int i = 0; i < mSelectedColorHsv.val.length; i++)
|
for (int i = 0; i < mSelectedColorHsv.val.length; i++)
|
||||||
{
|
{
|
||||||
mSelectedColorHsv.val[i] /= DotCount;
|
mSelectedColorHsv.val[i] /= pointCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat rgbaDotMap = new Mat();
|
Mat pointMapRgba = new Mat();
|
||||||
Mat hsvDotMat = new Mat(1, 1, CvType.CV_8UC3);
|
Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3);
|
||||||
|
|
||||||
byte[] buf = {(byte)mSelectedColorHsv.val[0], (byte)mSelectedColorHsv.val[1], (byte)mSelectedColorHsv.val[2]};
|
byte[] buf = {(byte)mSelectedColorHsv.val[0], (byte)mSelectedColorHsv.val[1], (byte)mSelectedColorHsv.val[2]};
|
||||||
|
|
||||||
hsvDotMat.put(0, 0, buf);
|
pointMatHsv.put(0, 0, buf);
|
||||||
Imgproc.cvtColor(hsvDotMat, rgbaDotMap, Imgproc.COLOR_HSV2RGB_FULL, 4);
|
Imgproc.cvtColor(pointMatHsv, pointMapRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);
|
||||||
|
|
||||||
mSelectedColorRgba.val = rgbaDotMap.get(0, 0);
|
mSelectedColorRgba.val = pointMapRgba.get(0, 0);
|
||||||
|
|
||||||
Log.i(TAG, "Touched rgba color: (" + mSelectedColorRgba.val[0] + ", " + mSelectedColorRgba.val[1] +
|
Log.i(TAG, "Touched rgba color: (" + mSelectedColorRgba.val[0] + ", " + mSelectedColorRgba.val[1] +
|
||||||
", " + mSelectedColorRgba.val[2] + ", " + mSelectedColorRgba.val[3] + ")");
|
", " + mSelectedColorRgba.val[2] + ", " + mSelectedColorRgba.val[3] + ")");
|
||||||
|
|
||||||
double MinH = 0;
|
double minH = (mSelectedColorHsv.val[0] >= COLOR_RADIUS.val[0]) ? mSelectedColorHsv.val[0]-COLOR_RADIUS.val[0] : 0;
|
||||||
double MaxH = 255;
|
double maxH = (mSelectedColorHsv.val[0]+COLOR_RADIUS.val[0] <= 255) ? mSelectedColorHsv.val[0]+COLOR_RADIUS.val[0] : 255;
|
||||||
|
|
||||||
if (mSelectedColorHsv.val[0]-mColorRadius >= 0)
|
mLowerBound.val[0] = minH;
|
||||||
MinH = mSelectedColorHsv.val[0]-mColorRadius;
|
mUpperBound.val[0] = maxH;
|
||||||
else
|
|
||||||
MinH = 0;
|
|
||||||
|
|
||||||
if (mSelectedColorHsv.val[0]+mColorRadius <= 255)
|
|
||||||
MaxH = mSelectedColorHsv.val[0]+mColorRadius;
|
|
||||||
else
|
|
||||||
MaxH = 255;
|
|
||||||
|
|
||||||
mLowerBound.val[0] = MinH;
|
|
||||||
mUpperBound.val[0] = MaxH;
|
|
||||||
|
|
||||||
mLowerBound.val[1] = 100;
|
mLowerBound.val[1] = mSelectedColorHsv.val[1] - COLOR_RADIUS.val[1];
|
||||||
mUpperBound.val[1] = 255;
|
mUpperBound.val[1] = mSelectedColorHsv.val[1] + COLOR_RADIUS.val[1];
|
||||||
|
|
||||||
mLowerBound.val[2] = 100;
|
mLowerBound.val[2] = mSelectedColorHsv.val[2] - COLOR_RADIUS.val[2];
|
||||||
mUpperBound.val[2] = 255;
|
mUpperBound.val[2] = mSelectedColorHsv.val[2] + COLOR_RADIUS.val[2];
|
||||||
|
|
||||||
Mat HsvSpectrum = new Mat(32, 2*mColorRadius*mSpectrumScale, CvType.CV_8UC3);
|
Log.d(TAG, "Bounds: " + mLowerBound + "x" + mUpperBound);
|
||||||
|
|
||||||
|
Mat spectrumHsv = new Mat(32, (int)(maxH-minH)*mSpectrumScale, CvType.CV_8UC3);
|
||||||
|
|
||||||
for (int i = 0; i < 32; i++)
|
for (int i = 0; i < 32; i++)
|
||||||
{
|
{
|
||||||
for (int k = 0; k < mSpectrumScale; k++)
|
for (int k = 0; k < mSpectrumScale; k++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < MaxH-MinH; j++)
|
for (int j = 0; j < maxH-minH; j++)
|
||||||
{
|
{
|
||||||
byte[] tmp = {(byte)(MinH+j), (byte)255, (byte)255};
|
byte[] tmp = {(byte)(minH+j), (byte)255, (byte)255};
|
||||||
HsvSpectrum.put(i, j*mSpectrumScale + k, tmp);
|
spectrumHsv.put(i, j*mSpectrumScale + k, tmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Imgproc.cvtColor(HsvSpectrum, mSpectrum, Imgproc.COLOR_HSV2RGB_FULL, 4);
|
Imgproc.cvtColor(spectrumHsv, mSpectrum, Imgproc.COLOR_HSV2RGB_FULL, 4);
|
||||||
|
|
||||||
mIsColorSelected = true;
|
mIsColorSelected = true;
|
||||||
|
|
||||||
@ -166,41 +149,66 @@ public class ColorBlobDetectionView extends SampleCvViewBase implements OnTouchL
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Bitmap processFrame(VideoCapture capture) {
|
protected Bitmap processFrame(VideoCapture capture) {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||||
|
|
||||||
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
|
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
|
||||||
|
|
||||||
if (mIsColorSelected)
|
if (mIsColorSelected)
|
||||||
{
|
{
|
||||||
Mat BlurMat = mRgba; //new Mat();
|
Mat PyrDownMat = new Mat();
|
||||||
//Imgproc.blur(mRgba, BlurMat, new Size(3, 3));
|
|
||||||
|
Imgproc.pyrDown(mRgba, PyrDownMat);
|
||||||
|
Imgproc.pyrDown(PyrDownMat, PyrDownMat);
|
||||||
|
|
||||||
Mat hsvMat = new Mat();
|
Mat hsvMat = new Mat();
|
||||||
Imgproc.cvtColor(BlurMat, hsvMat, Imgproc.COLOR_RGB2HSV_FULL);
|
Imgproc.cvtColor(PyrDownMat, hsvMat, Imgproc.COLOR_RGB2HSV_FULL);
|
||||||
|
|
||||||
Log.d(TAG, "Bounds: " + mLowerBound + "x" + mUpperBound);
|
|
||||||
|
|
||||||
Mat RangedHsvMat = new Mat();
|
Mat rangedHsvMat = new Mat();
|
||||||
Core.inRange(hsvMat, mLowerBound, mUpperBound, RangedHsvMat);
|
Core.inRange(hsvMat, mLowerBound, mUpperBound, rangedHsvMat);
|
||||||
|
|
||||||
Mat DilatedMat = new Mat();
|
Mat dilatedMat = new Mat();
|
||||||
Imgproc.dilate(RangedHsvMat, DilatedMat, new Mat());
|
Imgproc.dilate(rangedHsvMat, dilatedMat, new Mat());
|
||||||
|
|
||||||
Mat CannyRangedMat = new Mat();
|
|
||||||
Imgproc.Canny(RangedHsvMat, CannyRangedMat, 80, 90);
|
|
||||||
|
|
||||||
List<MatOfPoint> contours = new ArrayList<MatOfPoint>(5);
|
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
||||||
Mat hierarchy = new Mat();
|
Mat hierarchy = new Mat();
|
||||||
|
|
||||||
Imgproc.findContours(CannyRangedMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
Imgproc.findContours(dilatedMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
||||||
Imgproc.drawContours(mRgba, contours, -1, new Scalar(255,0,0,255));
|
|
||||||
|
|
||||||
Mat TestColorMat = mRgba.submat(2, 34, 2, 34);
|
// Find max contour area
|
||||||
TestColorMat.setTo(mSelectedColorRgba);
|
double maxArea = 0;
|
||||||
|
Iterator<MatOfPoint> it = contours.iterator();
|
||||||
|
while (it.hasNext())
|
||||||
|
{
|
||||||
|
MatOfPoint wrapper = it.next();
|
||||||
|
double area = Imgproc.contourArea(wrapper);
|
||||||
|
if (area > maxArea)
|
||||||
|
maxArea = area;
|
||||||
|
}
|
||||||
|
|
||||||
Mat TestSpectrumMat = mRgba.submat(2, 34, 38, 38 + mSpectrum.cols());
|
// Filter contours by area and resize to fit the original image size
|
||||||
mSpectrum.copyTo(TestSpectrumMat);
|
List<MatOfPoint> filteredContours = new ArrayList<MatOfPoint>();
|
||||||
|
it = contours.iterator();
|
||||||
|
while (it.hasNext())
|
||||||
|
{
|
||||||
|
MatOfPoint wrapper = it.next();
|
||||||
|
if (Imgproc.contourArea(wrapper) > MIN_CONTOUR_AREA*maxArea);
|
||||||
|
Point[] contour = wrapper.toArray();
|
||||||
|
for (int i = 0; i < contour.length; i++)
|
||||||
|
{
|
||||||
|
// Original image was pyrDown twice
|
||||||
|
contour[i].x *= 4;
|
||||||
|
contour[i].y *= 4;
|
||||||
|
}
|
||||||
|
filteredContours.add(new MatOfPoint(contour));
|
||||||
|
}
|
||||||
|
|
||||||
|
Imgproc.drawContours(mRgba, filteredContours, -1, new Scalar(255,0,0,255));
|
||||||
|
|
||||||
|
Mat testColorMat = mRgba.submat(2, 34, 2, 34);
|
||||||
|
testColorMat.setTo(mSelectedColorRgba);
|
||||||
|
|
||||||
|
Mat testSpectrumMat = mRgba.submat(2, 34, 38, 38 + mSpectrum.cols());
|
||||||
|
mSpectrum.copyTo(testSpectrumMat);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user