Reputation: 677
I have looked quite a bit everywhere and cannot find an answer to my problem. I tried to replicate a text detection software form this thread (Extracting text OpenCV) but at the end of the code there is a message error saying there is no match for the rectangle even though i have drawn one just above and we enter the loop. I have tested all the values i could think of and everything seems correct.
here is the complete code ;
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
namedWindow("source_window2",WINDOW_AUTOSIZE);
namedWindow("source_window3",WINDOW_AUTOSIZE);
Mat input = imread(argv[1], CV_LOAD_IMAGE_COLOR);
Mat in_gray = imread(argv[1],CV_LOAD_IMAGE_GRAYSCALE);
Mat gradient;
Mat Kernelellipse = getStructuringElement(MORPH_ELLIPSE, Size(3,3));
morphologyEx(in_gray, gradient, MORPH_GRADIENT, Kernelellipse);
Mat thresh;
//on convertit en binaire
threshold(gradient, thresh, 0.0, 255.0, THRESH_BINARY | THRESH_OTSU);
rectangle(input,Point(0,0),Point(50,50),Scalar(255,255,255),2);
Mat Kernelrectangle = getStructuringElement(MORPH_RECT, Size(9,1));
Mat fermee;
morphologyEx(thresh, fermee, MORPH_CLOSE, Kernelrectangle);
imshow("source_window3", fermee);
Mat noire = Mat::zeros(thresh.size(), CV_8UC1);
//on cheche les contours
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(fermee, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
for (int i = 0; i < contours.size(); ++i)
{
Rect rectangle = boundingRect(contours[i]);
Mat noirerectangle(noire, rectangle);
noirerectangle = Scalar(0, 0, 0);
//on les dessine
drawContours(noire, contours, i, Scalar(255, 255, 255), CV_FILLED);
double proportion_de_blanc = (double)countNonZero(noirerectangle)/(rectangle.width*rectangle.height);
if (proportion_de_blanc > 0.45 && (rectangle.height > 8 && rectangle.width > 8))
{
rectangle(input,rectangle.tl(),rectangle.br(),Scalar(0,255,0),2);
}
}
imshow("source_window2",input);
waitKey(0);
return(0);
}
My issue is within the last loop :
if (proportion_de_blanc > 0.45 && (rectangle.height > 8 && rectangle.width > 8))
{
rectangle(input,rectangle.tl(),rectangle.br(),Scalar(0,255,0),2);
}
Upvotes: 1
Views: 6320
Reputation: 180415
You have
Rect rectangle = boundingRect(contours[i]);
which is creating a variable of type Rect
named rectangle
. Then in your for loop you call what I assume is the rectangle()
function
rectangle(input,rectangle.tl(),rectangle.br(),Scalar(0,255,0),2);
In the scope where that call is made the compiler is treating rectangle
as the variable and not the function. To fix this you either need to qualify rectangle
with cv::
or you can change the name of your variable.
Upvotes: 1
Reputation: 41765
You defined one of your rects as:
Rect rectangle = boundingRect(contours[i]);
The name rectangle collides with the rectangle
drawing function. So either:
rename rectangle with another name, such as Rectangle rect = boundingRect(contours[i]);
call rectangle drawing method as cv::rectangle(input,rectangle.tl(),rectangle.br(),Scalar(0,255,0),2);
Upvotes: 2