霍夫线变换 - 如何关闭线以形成多边形?

问题描述 投票:2回答:1

我正在开发一种扑克牌检测程序。我正在使用霍夫线变换来检测卡的位置,因为它似乎是最强大的方式。因为它不太依赖于光线和背景等环境条件(我认为),而不是寻找轮廓。

我正在使用此图片进行测试:

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

如您所见,线条不会关闭多边形,我无法对卡片的位置做出任何结论。

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

我用过这段代码:

#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++ opencv computer-vision
1个回答
4
投票

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

  • 将图像转换为灰度
  • 高斯模糊图像
  • 使用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)
© www.soinside.com 2019 - 2024. All rights reserved.