android - 如何使用 OpenCV 从图像中检测(计数)头发?

标签 android opencv opencv4android opencv3.1

我已经使用 OpenCV 函数 cvtColorCannyHoughLinesP 尝试了下面的代码,但无法获得准确的结果在某些情况下结果或不起作用。

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;
}

以下是计算毛发的示例图像:

enter image description here

enter image description here

最佳答案

我想你会发现这篇文章很有趣:

http://www.cs.ubc.ca/~lowe/papers/aij87.pdf

他们采用 2D 位图,应用精明的边缘检测器,然后根据不同边缘的 fragment 属于同一对象的可能性(在本例中为头发)重新组合不同边缘的 fragment (并给出此类重新组合的标准)。

我认为您可以使用它来了解图像上有多少对象,如果图像仅包含头发,那么您可以计算头发数量。

关于android - 如何使用 OpenCV 从图像中检测(计数)头发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39975618/

相关文章:

android - 如何在 android 中修剪视频(没有 ffmpeg)?

android - 使用同一个键在 Android 应用程序首选项中存储多个值

java - 为 JavaCameraView 使用自定义类会使应用程序崩溃

将 Mat 转换为 Android 位图时出现 Android OpenCV 错误

opencv - 通过积累图片的夜视

java - Android:需要用户交互/输入的通知

android - 无法更改从 Firebase 控制台发送的 Firebase 云消息传递通知的默认图标

python - 使用 OpenCV 从图像中提取多边形给定坐标

python - Heroku上带有OpenCV的Python应用程序

opencv - 2 个 USB 摄像头不能与 opencv 一起工作