OpenCV程序的输出在未检测到文本对象时不给出0

Output of OpenCV program doesn't give 0 when not detect text object

本文关键字:对象 文本 程序 输出 检测 OpenCV      更新时间:2023-10-16

我已经从提取文本openCV中修改了代码,以在某些飞机门上找到文本框。该代码可以检测文本框,但是当检测到文本时,我希望输出" 1"并在未检测到" 0"时。当检测到文本框时,我可以获得1,但如果没有检测到0,则不能得到0。这里的代码。

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/ml/ml.hpp>
#include<iostream>
#include<vector>
#include <stdlib.h>
#include<dirent.h>
#include<string.h>
using namespace cv;
using namespace std;
#include<fstream>
#include<iostream>
int main(int argc,char** argv)
{
    IplImage *desimg,*srcimg;   
    string dirName = "C:/Users/TestFeatures/a100/" ;    
    DIR *dir;
    dir = opendir(dirName.c_str());
    string imgName;
    struct dirent *ent; 
    if (dir != NULL) {
    while ((ent = readdir (dir)) != NULL) {
        imgName= ent->d_name;
        if(imgName.compare(".")!= 0 && imgName.compare("..")!= 0)
        {
            string aux;
            aux.append(dirName);
            aux.append(imgName);                            
            Mat image= imread(aux);                          
            waitKey(0);
            Mat rgb,src_gray,small1;
            // downsample and use it for processing
            pyrDown(image, rgb);
            Mat small;
            cv::GaussianBlur(rgb, src_gray, cv::Size(3, 3),2,2,BORDER_DEFAULT);           
            cvtColor(src_gray, small1, CV_BGR2GRAY);
            Canny(small1,small,600,1300,5,true);
            // morphological gradient
            Mat grad;
            Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(8,3));
            morphologyEx(small, grad, MORPH_GRADIENT, morphKernel);
            // binarize
            Mat bw;
            threshold(grad, bw, 255.0, 255.0, THRESH_BINARY | THRESH_OTSU);
            // connect horizontally oriented regions
            Mat connected;
            morphKernel = getStructuringElement(MORPH_CROSS, Size(5, 1));
            morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);
            // find contours
            Mat mask = Mat::zeros(bw.size(), CV_8UC1);
            //std::vector<std::vector<cv::Point> > contours;
            vector<vector<Point> > contours;
            vector<Vec4i> hierarchy;
            findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
            // filter contours     
            for(int idx = 0; idx >= 0; idx = hierarchy[idx][0])
            {
                Rect rect = boundingRect(contours[idx]);
                Mat maskROI(mask, rect);
                maskROI = Scalar(0, 0, 0);
                // fill the contour
                drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
                // ratio of non-zero pixels in the filled region
                double r = (double)countNonZero(maskROI)/(rect.width*rect.height);
                vector<Moments> mu(1);
                vector<Point2f> mc(1);
                mu[0] = moments( contours[idx], false );
                mc[0] = Point2f( mu[0].m10/mu[0].m00 , mu[0].m01/mu[0].m00 );
                if (r > .45 /* assume at least 45% of the area is filled if it contains text */
                && (rect.height >12 && rect.width >12 && rect.height < 100 && rect.width < 100 && rect.width/rect.height>1.03&& rect.width/rect.height<2.1) /* constraints on region size */
                /* these two conditions alone are not very robust. better to use something
                like the number of significant peaks in a horizontal projection as a third condition */)
                {
                    rectangle(rgb, rect, Scalar(0, 255, 0), 2);
                    Moments mu;
                    Point2f mc;
                    mu = moments( contours[idx], false );
                    mc = Point2f( mu.m10/mu.m00 , mu.m01/mu.m00 );
                    circle( rgb, mc, 4, Scalar(255,0,0), -1, 8, 0 );
                    ofstream mycout("C:/Results/moments_text3.txt",ios::app); 
                    cout<<"1"<<endl;
                    cout<<mc<<endl;                          
                }
                else {
                    cout<<"0"<<endl;
                }
            }
            cv::imshow( "rgb.jpg", rgb);
            }
        }
        closedir (dir);
    } else {
        cout<<"not present"<<endl;
    }
}

在此检测到的文本中,输出的输出。它给出了1的1和像素的坐标。

1
[610.473, 135.813]

我可以为未检测到0的帮助如何获得0?

您只需要在对象内找到逻辑代码

bool found=false;
for(int idx = 0; idx >= 0; idx = hierarchy[idx][0])
{
    Rect rect = boundingRect(contours[idx]);
    Mat maskROI(mask, rect);
    maskROI = Scalar(0, 0, 0);
    // fill the contour
    drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);
    // ratio of non-zero pixels in the filled region
    double r = (double)countNonZero(maskROI)/(rect.width*rect.height);
    vector<Moments> mu(1);
    vector<Point2f> mc(1);
    mu[0] = moments( contours[idx], false );
    mc[0] = Point2f( mu[0].m10/mu[0].m00 , mu[0].m01/mu[0].m00 );
    if (r > .45 &&(rect.height >12 && rect.width >12 && rect.height < 100 && rect.width < 100 && rect.width/rect.height>1.03&& rect.width/rect.height<2.1))
    {
        rectangle(rgb, rect, Scalar(0, 255, 0), 2);
        Moments mu;
        Point2f mc;
        mu = moments( contours[idx], false );
        mc = Point2f( mu.m10/mu.m00 , mu.m01/mu.m00 );
        circle( rgb, mc, 4, Scalar(255,0,0), -1, 8, 0 );
        ofstream mycout("C:/Results/moments_text3.txt",ios::app); 
        found=true;
        cout<<"1"<<endl;
        cout<<mc<<endl;                          
    }
}
if(found==false) 
cout<<"0"<<endl;