我找了很多地方,找不到我的问题的答案。我试图从这个线程(Extracting text OpenCV)复制一个文本检测软件,但是在代码的末尾,有一个消息错误,它说矩形没有匹配,即使我在上面画了一个,我们进入循环。我已经测试了所有我能想到的价值观,一切似乎都是正确的。
这是完整的代码;
#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);}
我的问题在最后一个循环中:
if (proportion_de_blanc > 0.45 && (rectangle.height > 8 && rectangle.width > 8))
{
rectangle(input,rectangle.tl(),rectangle.br(),Scalar(0,255,0),2);
}发布于 2015-08-04 14:35:42
您将其中一个rects定义为:
Rect rectangle = boundingRect(contours[i]);名称矩形与rectangle绘图函数发生冲突。因此,要么是:
Rectangle rect = boundingRect(contours[i]);cv::rectangle(input,rectangle.tl(),rectangle.br(),Scalar(0,255,0),2);发布于 2015-08-04 14:36:13
你有
Rect rectangle = boundingRect(contours[i]);创建一个名为Rect的变量,名为rectangle。然后在for循环中调用我假设的rectangle()函数
rectangle(input,rectangle.tl(),rectangle.br(),Scalar(0,255,0),2);在发出该调用的范围内,编译器将rectangle视为变量而不是函数。要解决这个问题,要么需要用rectangle限定cv::,要么可以更改变量的名称。
https://stackoverflow.com/questions/31812020
复制相似问题