OpenCV程序的输出在未检测到文本对象时不给出0
Output of OpenCV program doesn't give 0 when not detect text object
我已经从提取文本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;
相关文章:
- 如何从文本文件C++制作对象?
- C++ - 确定 const char* 是指向字符串文本对象还是动态对象
- 遍历对象向量,并找到与从文本文件中提取的对象匹配的变量
- 使用空格分隔符从文本文件将对象读入数组
- 为什么使用 C++ 中的类对象写入文件中的数据以非文本格式存储?
- 从文本文件中读取对象
- 如果给定了文本样式,如何获取特定文本的 QSize 对象
- 有没有办法基于文本文件动态创建对象?
- 如何设计对象以不同方式设置文本格式
- 为什么<string>字符串对象需要 #include 而不是字符串文本?
- 尝试从文本文件读取到对象数组
- 对由文本实例化的对象的类引用
- 无法将对象的值写入文本文件 C++
- 在基于文本的游戏中,如何将对象放置在房间内?
- 如何在文本文件中逐行读取并填充指向对象数组的指针
- 我想从文本文件中获取信息并将该信息分配给类对象
- OpenCV程序的输出在未检测到文本对象时不给出0
- 设计具有变体字段的文本类型类,其中可以存储一个或三个对象
- 如何解析文本文件并使用构造函数中的文件输入来创建对象的容器
- C++ 确定类是否可以使用对象 - 文本 RPG 游戏