-2

I have bit modified the code from Extracting text OpenCV to find text boxes on some airplane door. The code can detect text boxes but I would like to have output "1" when text is detected and output "0" when not. When text box is detected i can get the 1 but not 0 in case nothing detect. Here the code.

#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;
    }
}

Here the output in case of detected text. It gives 1 and the pixels coordinates of the center point of the detected text box.

1
[610.473, 135.813]

Can I have the help how to get 0 in case not detected?

Community
  • 1
  • 1
bob
  • 363
  • 3
  • 8
  • 21

1 Answers1

1

you just have to place a check inside the object finding logic code

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;
Awais Rafique
  • 466
  • 6
  • 17