【问题标题】:Object detection and segmentation Using Python使用 Python 进行对象检测和分割
【发布时间】:2016-12-21 18:10:07
【问题描述】:

我是一名本科生。我是图像处理和 python 的新手。

我有许多植物样本的图像及其描述(称为标签,贴在样本上),如下图所示。我需要自动分割样本中的那些标签。

我尝试了基于颜色的阈值化,但失败了。你能否建议我一个例子来完成这项任务。我需要一些想法或代码来使其完全自动分割。

如果您是图像处理和 Python 方面的专家,请帮助我,我需要您的帮助才能完成这项任务。

在左上角检测到矩形,但它应该在右下角。你能告诉我我的错误在哪里以及如何纠正它。 我也给出了下面的代码。

【问题讨论】:

    标签: python opencv image-processing ocr image-segmentation


    【解决方案1】:

    您可以尝试使用一个与白色大矩形匹配的模板来识别存储信息的区域。

    http://docs.opencv.org/3.1.0/d4/dc6/tutorial_py_template_matching.html#gsc.tab=0

    完成后,您将能够识别该区域中的字符...您保存一个小子图像,并使用 pytesseract 之类的工具,您将能够读取字符。

    https://pypi.python.org/pypi/pytesseract

    这里还有其他 OCR 示例: https://saxenarajat99.wordpress.com/2014/10/04/optical-character-recognition-in-python/

    祝你好运!

    【讨论】:

    • 嗨@FrECM,我对模板匹配进行了更改。但我不知道我哪里出错了。
    【解决方案2】:

    为什么要使用颜色阈值?我用 ImageJ 试过这个并得到了很好的结果。我刚刚使用固定阈值(在本例中为 166)将图像转换为 8 位和 binarise。您可以从图像histogram 中选择最佳阈值。 然后你只需要找到你的白色矩形区域并阅读FrsECM建议的字符。

    这是一个 c++ 示例:

    #include "opencv2/imgproc.hpp"
    #include "opencv2/highgui.hpp"
    #include <stdlib.h>
    #include <stdio.h>
    
    using namespace cv;
    
    /// Global variables
    int threshold_nvalue = 166; 
    const int thresh_increment = 2;
    int threshold_type = THRESH_BINARY;//1
    int const max_value = 255;
    int const morph_size = 3;
    int const  min_blob_size = 1000;
    Mat src, src_resized, src_gray, src_thresh, src_morph;
    
    /**
    * @function main
    */
    int main(int argc, char** argv)
    {
        /// Load an image
        src = imread("C:\\Users\\phili\\Pictures\\blatt.jpg", 1);
        //Resize for displaying it properly
        resize(src, src_resized, Size(600, 968));
        /// Convert the image to Gray
        cvtColor(src_resized, src_gray, COLOR_RGB2GRAY);    
        /// Region of interest
        Rect label_rect;
        //Binarization sing fixed threshold
        threshold(src_gray,src_thresh, thres, max_value, threshold_type);
        //Erase small object using morphologie
        Mat element = getStructuringElement(0, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));
        morphologyEx(src_thresh, src_morph, MORPH_CLOSE, element);  
    
        //find white objects and their contours
        std::vector<std::vector<Point> > contours;
        std::vector<Vec4i> hierarchy;
        findContours(src_morph, contours, CV_RETR_TREE, CV_CHAIN_APPROX_NONE, Point(0, 0));
        for (std::vector<std::vector<Point> >::iterator it = contours.begin(); it != contours.end(); ++it)
        {
    
            //just big blobs
            if (it->size()>min_blob_size)
            {
                //approx contour and check for rectangle
                std::vector<Point> approx;
                approxPolyDP(*it, approx, 0.01*arcLength(*it, true), true);
                if (approx.size() == 4)
                {
                    //just for visualization 
                    drawContours(src_resized, approx, 0, Scalar(0, 255, 255),-1);
                    //bounding rect for ROI
                    label_rect = boundingRect(approx);
                    //exit loop             
                    break;
                }
            }
    
    
        }
        //Region of interest
        Mat label_roi = src_resized(label_rect);
    
        //OCR comes here...
    }
    

    【讨论】:

    • Here 您可以阅读如何检测矩形等简单形状
    • 感谢@PSchn 提供的信息,我已经根据模板匹配更新了结果,但我不知道我哪里出错了。
    • 我认为你混合了两种方法。进行二值化和轮廓查找以检测您的矩形或模板匹配例程不混合。我建议进行二值化,找到你的轮廓,清理它们,大约一个多边形,......然后就可以了。我使用模板匹配,然后忘记二值化。模板是否与您的标签大小相同?
    猜你喜欢
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 2020-02-11
    • 2011-07-21
    • 2021-08-29
    相关资源
    最近更新 更多