检测图像中物体的位置 [英] Detect location(s) of objects in an image

查看:89
本文介绍了检测图像中物体的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的输入图像:

请注意,有 6 个带有黑色边框的框.我需要检测每个盒子的位置(左上角编码).通常我会使用类似

解决方案

您可以找到连通组件的边界框.

要找到连接的组件,您可以转换为灰度,并保留所有值为 0 的像素,即矩形的黑色边框.

然后您可以找到每个连接组件的轮廓,并计算其边界框.此处发现红色边界框:

代码:

#include #include <向量>使用命名空间 cv;使用命名空间标准;int main(){//加载图像,作为 BGRMat3b img = imread("path_to_image");//转换为灰度Mat1b 灰色;cvtColor(img, 灰色, COLOR_BGR2GRAY);//获取二进制掩码Mat1b 二进制 = (灰色 == 0);//查找连接组件的轮廓矢量<矢量<点>>轮廓;findContours(binary.clone(), 轮廓, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);//对于每个轮廓for (int i = 0; i 

<小时>

从聊天中发布的图像,此代码生成:

通常,此代码将正确检测卡片以及噪音.您只需要根据某些标准去除噪音.其中包括:框的大小或纵横比、框内的颜色、一些纹理信息.

I have an input image that looks like this:

Notice that there are 6 boxes with black borders. I need to detect the location (upper-left hand corder) of each box. Normally I would use something like template matching but the contents (the colored area inside the black border) of each box is distinct.

Is there a version of template matching that can configured to ignore the inner area of each box? Is the an algorithm better suited to this situation?

Also note, that I have to deal with several different resolutions... thus the actual size of the boxes will be different from image to image. That said, the ratio (length to width) will always be the same.

Real-world example/input image per request:

解决方案

You can do this finding the bounding box of connected components.

To find connected components you can convert to grayscale, and keep all pixels with value 0, i.e. the black border of the rectangles.

Then you can find the contours of each connected component, and compute its bounding box. Here the red bounding boxes found:

Code:

#include <opencv2/opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;

int main()
{
    // Load the image, as BGR
    Mat3b img = imread("path_to_image");

    // Convert to gray scale
    Mat1b gray;
    cvtColor(img, gray, COLOR_BGR2GRAY);

    // Get binary mask
    Mat1b binary = (gray == 0);

    // Find contours of connected components
    vector<vector<Point>> contours;
    findContours(binary.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    // For each contour
    for (int i = 0; i < contours.size(); ++i)
    {
        // Get the bounding box
        Rect box = boundingRect(contours[i]);

        // Draw the box on the original image in red
        rectangle(img, box, Scalar(0, 0, 255), 5);
    }

    // Show result
    imshow("Result", img);
    waitKey();

    return 0;
}


From the image posted in chat, this code produces:

In general, this code will correctly detect the cards, as well as noise. You just need to remove noise according to some criteria. Among others: size or aspect ratio of boxes, colors inside boxes, some texture information.

这篇关于检测图像中物体的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆