问题描述
我尝试使用OpenCV
函数cvtColor
,Canny
和HoughLinesP
编写以下代码,但是在某些情况下无法获得准确的结果或无法正常工作.
I have try below code using OpenCV
functions cvtColor
,Canny
and HoughLinesP
but not able to get accurate result or not work in some cases.
private boolean opencvProcessCount(Uri picFileUri) {
hairCount = 0;
totalC = 0;
//Log.e(">>>>>>>>","count " + picFileUri);
try {
InputStream iStream = getContentResolver().openInputStream(picFileUri);
byte[] im = getBytes(iStream);
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap image = BitmapFactory.decodeByteArray(im, 0, im.length);
Mat mYuv = new Mat();
Utils.bitmapToMat(image, mYuv);
Mat mRgba = new Mat();
Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_RGB2GRAY, 4);
Imgproc.Canny(mRgba, mRgba, 80, 90);
Mat lines = new Mat();
int threshold = 80;
int minLineSize = 30;
int lineGap = 100;
Imgproc.HoughLinesP(mRgba, lines, 1, Math.PI/180, threshold, minLineSize, lineGap);
for (int x = 0; x < lines.rows(); x++)
{
double[] vec = lines.get(x, 0);
double x1 = vec[0],
y1 = vec[1],
x2 = vec[2],
y2 = vec[3];
Point start = new Point(x1, y1);
Point end = new Point(x2, y2);
double dx = x1 - x2;
double dy = y1 - y2;
double dist = Math.sqrt (dx*dx + dy*dy);
totalC ++;
Log.e(">>>>>>>>","dist " + dist);
if(dist>300.d)
{
hairCount ++;
// Log.e(">>>>>>>>","count " + x);
Imgproc.line(mRgba, start, end, new Scalar(0,255, 0, 255),5);// here initimg is the original image.
}// show those lines that have length greater than 300
}
Log.e(">>>>>>>>",totalC+" out hairCount " + hairCount);
// Imgproc.
} catch (Throwable e) {
// Log.e(">>>>>>>>","count " + e.getMessage());
e.printStackTrace();
}
return false;
}
下面是用于计数头发的示例图像:
Below are sample images to count hair :
推荐答案
我认为您会发现本文有趣:
I think you will find this article interesting:
http://www.cs.ubc.ca/~lowe /papers/aij87.pdf
他们获取2D位图,应用Canny边缘检测器,然后根据它们属于同一对象的可能性重新组合不同边缘的线段-在这种情况下为头发(并提供这种重新组合的标准).
They take a 2D bitmap, apply canny edge detector and then regroup segments of the different edges based on how likely they belong to a same object - in this case hair (and give criterias for such regrouping).
我想您可以使用它来了解图像上有多少个对象,如果图像仅包含头发,那么您就可以对头发进行计数.
I think you could use this to know how many objects there are on the image, and if the image contains only hair, then you'd have a count for hair.
这篇关于如何使用OpenCV从图像中检测(计数)头发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!