存储由简单多边形组成的二进制图像的详细信息 [英] Store details of a binary image consisting simple polygons

查看:195
本文介绍了存储由简单多边形组成的二进制图像的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题涉及到一些实践和经验基础的过程。我有一个Mat二进制图像由黑色背景中的简单的白色多边形组成。实际上,这些多边形代表报纸页面中的文章。所以我想要的是在报纸页面内存储文章的位置的详细信息。一个Mat图像中只有一个多边形。一个选项是


  1. 使用纯OpenCV调用将Mat存储到.xml或.yml文件中(



    第一个选项似乎有可能, t知道如何实现第二种方法。如果可能,这将是最有效的,因为我认为,因为那将只有很少的坐标要保存每篇文章。我可以实现一个复杂的过程来找到它的顶点,并在需要时使用那些坐标重绘Mat图像。但我希望有一个简单的过程在opencv这个任务。



    所以我想知道的是哪种方法更好,如果第二种方法更好,如何在opencv与c ++做。我既不是opencv专家也不是c ++专家,所以一个适当的答案将节省我许多小时和程序的效率。

    解决方案

    您可以使用


    This question relates to somewhat practice and experienced based process. I have an Mat binary image which consist of simple white color polygons in a black background. Actually those polygons represent an article in a newspaper page. So what I want is to store the details of the location of the article inside the newspaper page. One Mat image has only one polygon in it. So one option is to

    1. Use pure OpenCV calls to store Mat into a .xml or .yml file (How to write a Float Mat to a file in OpenCV : accepted answer)
    2. Find the coordinates of the polygon where there are vertices and store only those coordinates into the database

    Following is a sample image of a Mat I am going to store.

    The first option seems possible but I don't know how to implement the second approach. If it is possible, that would be most efficient as I think, because then there would be only few coordinates to be saved for each article. I can implement a complex procedure to find the vertices for that and also to redraw the Mat image using those coordinates when needed. But I hope there is a simple process in opencv for this task.

    So what I want to know is which approach is better and if the second approach is better, how to do that in opencv with c++. I am neither an opencv expert nor c++ expert so an appropriate answer would save me many hours and also efficiency of the program.

    解决方案

    You can simply use findContours, with an appropriate contour approximation method. Basically, aside from CV_CHAIN_APPROX_NONE that will store all points, every other method is fine for this example: CV_CHAIN_APPROX_SIMPLE, CV_CHAIN_APPROX_TC89_L1 and CV_CHAIN_APPROX_TC89_KCOS.

    You can store those points in your database. You can then reload those points, and draw original image with fillPoly.

    This simple example show the retrieved contours points with the approximation method, and how to re-draw the image with those points.

    Note that you're image is aliased (you probably saved it in jpeg before png), so you need to remove aliasing for example keeping only points with value equals to 255.

    #include <opencv2\opencv.hpp>
    #include <vector>
    using namespace std;
    using namespace cv;
    
    int main()
    {
        Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
    
        // Removing compression artifacts
        img = img == 255;
    
        vector<vector<Point>> contours;
        findContours(img.clone(), contours, RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    
        if (contours.empty()) {return -1;}
    
        // Save the vertices "contours[0]"
    
        // Result image
        Mat3b res;
        cvtColor(img, res, COLOR_GRAY2BGR);
        for (int i = 0; i < contours[0].size(); ++i)
        {
            circle(res, contours[0][i], 3, Scalar(0,255,0));
        }
    
        // Reconstruct image from contours vertex
    
        // Load the vertices
        vector<vector<Point>> vertices = { contours[0] };
    
        Mat1b rec(img.rows, img.cols, uchar(0));
        fillPoly(rec, vertices, Scalar(255));
    
        imshow("Vertices", res);
        imshow("Reconstructed", rec);
        waitKey();
    
        return 0;
    }
    

    Green vertices with contour approximation method:

    这篇关于存储由简单多边形组成的二进制图像的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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