【问题标题】:How to find playing cards in an image with OpenCV?霍夫线变换 - 如何闭合线以形成多边形?
【发布时间】:2019-09-16 08:02:41
【问题描述】:

我目前正在开发一个扑克牌检测程序。我正在使用霍夫线变换来检测卡片的位置,因为这似乎是最可靠的方法。因为它较少依赖环境条件,例如光线和背景(我认为),而不是寻找轮廓。

我用这张图来测试:

在转换之后,我得到了这个结果:

如您所见,线条并没有闭合多边形,我无法对卡片的位置做出任何结论。

我已经考虑过使用一些标准(例如角度等)来对属于同一张卡片的线条进行分组,但我想知道是否有更好更快的方法来找到每张卡片的位置。

我使用了这个代码:

#include <cstdlib>
#include <cstdio>
#include "detector.h"
#include "data_structs.h"
#include <unistd.h>

#define MIN_LINE_LEN 80 
#define MAX_LINE_GAP 10 

//Variáveis globais 
Mat img;
Mat src;

int main( int argc, char** argv ){

    src = imread("img.jpg");
    src.copyTo(img);

    preProcessImg();

    detectCards();

    return 0;
}

//Prepara a img para ser analisada
void preProcessImg(){
    Mat aux_gray;

    cvtColor(img, aux_gray, CV_BGR2GRAY);    // Convert the image to grayscale
    GaussianBlur(aux_gray, img, Size(5,5), 0);
}

void detectCards(){
    vector<Vec4i> lines;

    //Detetar as linhas
    Canny(img, img, 30, 200);
    HoughLinesP(img, lines, 1, CV_PI/180, 80, MIN_LINE_LEN, MAX_LINE_GAP);

}

【问题讨论】:

    标签: c++ image opencv image-processing computer-vision


    【解决方案1】:

    我提出了另一种方法,而不是使用霍夫线变换来检测卡片并且必须闭合线以形成多边形。以下是主要思想:

    • 将图像转换为灰度
    • 高斯模糊图像
    • 使用Canny边缘检测获取卡片位置
    • 寻找轮廓
    • 通过获取每个轮廓的面积来过滤轮廓,以确保它与阈值最小区域匹配

    结果如下:

    Canny 边缘检测(左)检测到的卡片(右)

    注意所有边缘都被检测到,这就是为什么我们必须过滤以去除小颗粒

    检测到轮廓

    检测到的轮廓:7

    我的实现是在 Python 中,但您可以使用相同的策略将其转换为 C++

    import numpy as np
    import cv2
    import imutils
    
    original_image = cv2.imread("1.jpg")
    original_image = imutils.resize(original_image, width=500)
    image = original_image.copy()
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (3, 3), 0)
    canny = cv2.Canny(blurred, 120, 255, 1)
    
    cv2.imshow("canny", canny)
    
    # Find contours in the image
    cnts = cv2.findContours(canny.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    
    contours = []
    
    contour_image = canny.copy()
    threshold_min_area = 400
    for c in cnts:
        area = cv2.contourArea(c)
        if area > threshold_min_area:
            cv2.drawContours(original_image,[c], 0, (0,255,0), 3)
            contours.append(c)
    
    cv2.imshow("detected", original_image) 
    print('contours detected: {}'.format(len(contours)))
    cv2.waitKey(0)
    

    【讨论】:

    • 非常感谢,这实际上似乎是解决此问题的最佳方法!
    猜你喜欢
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2014-01-02
    相关资源
    最近更新 更多