java - 使用 OpenCV 检测图像上的矩形人像

标签 java opencv face-detection image-recognition vision

我有许多带有人物肖像的年鉴图像,我正在尝试构建一个算法来检测这些肖像。至少,检测正确的矩形肖像。 Example 1 Example 2

我正在尝试调查三个方向:

  1. 人脸检测
  2. 暗矩形检测(因为人像通常是较亮背景上较暗的形状)
  3. 从 OCR 文本中提取人名

通过结合以上三种算法的结果,我希望得到一些适用于许多不同年鉴页面的方法。

如果您对矩形检测有任何帮助,我将不胜感激。 我从 Java 和 OpenCV 3 开始。

这是我申请的代码 an image :

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat source = Imgcodecs.imread("Path/to/image", Imgcodecs.CV_LOAD_IMAGE_ANYCOLOR);
Mat destination = new Mat(source.rows(), source.cols(), source.type());

Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY);
Imgproc.GaussianBlur(destination, destination, new Size(5, 5), 0, 0, Core.BORDER_DEFAULT);

int threshold = 100;
Imgproc.Canny(destination, destination, 50, 100);
Imgproc.Canny(destination, destination, threshold, threshold*3);

在这一点上,我有这样的结果: enter image description here

试图从上面的边缘找到轮廓:

    List<MatOfPoint> contourDetections = new ArrayList<>();
    Mat hierarchy = new Mat();

    // Find contours
    Imgproc.findContours(destination, contourDetections, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    // Draw contours 
    Imgproc.drawContours(source, contours, -1, new Scalar(255,0,0), 2);

得到这个结果: enter image description here

但不确定如何从这些轮廓中提取矩形,因为许多线是不完整的。

回到边缘并尝试使用 HoughLinesP 找到垂直线和水平线:

    Mat lines = new Mat();
    int thre = 50;
    int minLineSize = 250;
    int lineGap = 80;

    int ignoreLinesShorter = 300;

    Imgproc.HoughLinesP(destination, lines, 1, Math.PI/180, thre, minLineSize, lineGap);

    for(int c = 0; c < lines.rows(); c++) {

        double[] vec = lines.get(c, 0);

        double  x1 = vec[0],
                y1 = vec[1],
                x2 = vec[2],
                y2 = vec[3];

        // Filtering only verticat and horizontal lines
        if(x1 == x2 || y1 == y2) {

            // Filtering out short lines
            if(Math.abs(x1 - x2) > ignoreLinesShorter || Math.abs(y1 - y2) > ignoreLinesShorter) {

              Point start = new Point(x1, y1);
              Point end = new Point(x2, y2);

              // Draw line
              Imgproc.line(source, start, end, new Scalar(0,0,255), 2);
            }
        }
    }

结果:

enter image description here

与轮廓一样,我仍然没有看到可以检测到的正确矩形。你能帮我一个正确的方向吗?也许有更简单的方法来执行此任务?

最佳答案

这不是一个完整的答案,但可能有用。

我用下面的代码得到了下面的图片。

要了解代码,您可以引用我在 http://answers.opencv.org/question/85884 的旧答案

如果它看起来很有希望,我们将尝试共同改进它。

enter image description here

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;

int main(int argc, char** argv)
{
    Mat img = imread("e:/test/twHVm.jpg");
    if (img.empty())
        return -1;

    Mat resized, gray, reduced_h, reduced_w;
    resize(img, resized, Size(), 1, 1);

    cvtColor(resized, gray, CV_BGR2GRAY);

    reduce(gray, reduced_h, 0, REDUCE_AVG);
    reduce(gray, reduced_w, 1, REDUCE_AVG);


    for (int i = 0; i < img.cols; i++)
    {
        if (reduced_h.at<uchar>(0, i) > 200) // this is experimental value
        line(resized, Point(i, 0), Point(i, img.rows), Scalar(0, 255, 0), 1);
    }

    for (int i = 0; i < img.rows; i++)
    {
        if (reduced_w.at<uchar>(i, 0) > 225) // this is experimental value
        line(resized, Point(0, i), Point(img.cols, i), Scalar(0, 255, 0), 1);
    }

    imshow("result", resized);
    waitKey(0);
    return 0;
}

关于java - 使用 OpenCV 检测图像上的矩形人像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45191975/

相关文章:

java - 用 Java 创建 Zip 文件系统提供程序

java.lang.NumberFormatException : Invalid int: "" 异常

ios - OpenCV iOS - 预期标识符(MACRO)

opencv - 大多数 cv2 元组参数在 python 3.5 (windows 7) 中不起作用

python - 如何限制 haar 级联检测到的面孔数量

java - Selenium 我无法在字段中发送文本。发送 key 不起作用

java - Java swing 中图形的标记

c++ - 使用 opencv 和 c++ 在图像中绘制 ROI

android - 为什么 Android 需要 OpenCV Manager 和 neon?

android - 如何使用 openCV 中的 HoughCircles 检测感兴趣区域中的圆圈?