我有一张由多行组成的泰文文本图像。我想将图像拆分为单独的文本行,并且可以检查行数。问题是当我拆分子图像时会产生错误。
我的代码
vector<int> point;
//Function Check Line
int checkLine(cv::Mat imgSrc){
int temp=-1;
int line=0;
int CountI[32767], CountJ[32767];
int countRange=0;
int maxBlack = INT_MIN;
point.clear();
for(int y=0;y<imgSrc.size().height;y++)
{
for(int x=0;x<imgSrc.size().width;x++)
{
if(imgSrc.at<uchar>(y,x)==0){
//cout<<y<<":";
if(y-temp>1){
point.push_back(temp);
point.push_back(y);
}
temp=y;
break;
}
}
}
point.push_back(temp);
point.erase(point.begin(), point.begin()+1);
for(vector<int>::size_type i = 0; i<point.size(); i++){
CountI[i]=point[i];
}
for(vector<int>::size_type j = 0; j<point.size()-1; j+=2){
CountJ[countRange++]=point[j+1]-point[j];
}
for(int i=0;i<countRange;i++){
if(CountJ[i]>maxBlack) maxBlack=CountJ[i];
}
for(int i=0;i<countRange;i++){
if(CountJ[i]>(maxBlack*0.5)) line++;
}
return line;
}
//Function Line Rect
vector<cv::Mat> lineRect(cv::Mat imgSrc, vector<int> startPoint, vector<int> stopPoint) {
vector <cv::Mat> img_split;
img_split.clear();
for(vector<int>::size_type k = 0; k<startPoint.size(); k++){
img_split.push_back(imgSrc(cv::Rect(0,startPoint[k], imgSrc.cols,stopPoint[k]-startPoint[k])));
}
return img_split;
}
int main()
{
const string path = "D:\\\exam.jpg";
cv::Mat img_src = imread(path,0);
vector<cv::Mat> img_split;
cv::Mat img_tmp;
vector<int> startPoint;
vector<int> stopPoint;
vector<int> PBlackFont;
vector<int> PBlackBack;
vector<int> pointW;
int line=-1;
int check=-1;
line = checkLine(img_src);
int textLine = line;
for(int k=0;k<textLine;k++){
cout<<line<<endl;
if(check==1){
img_tmp = imread("D:\\buff\\tmp1.jpg",1);
}
else if(check==2){
img_tmp = imread("D:\\buff\\tmp2.jpg", 1);
}
else if(check==-1){
img_tmp = img_src;
}
if(line>1){
PBlackFont.clear();
PBlackBack.clear();
pointW.clear();
for(int i = 1; i<point.size()-1; i+=2){
pointW.push_back((point[i+1]-1)-(point[i]+1));
PBlackFont.push_back(point[i+1]-1);
PBlackBack.push_back(point[i]+1);
}
int max=INT_MIN, index=-1;
for(int j = 0; j<pointW.size(); j++){
if(pointW[j]>max) {
max=pointW[j];
index=j;
}
}
startPoint.clear();
stopPoint.clear();
startPoint.push_back(point[0]);
startPoint.push_back(PBlackFont[index]);
stopPoint.push_back(PBlackBack[index]);
stopPoint.push_back(point[point.size()-1]);
img_split.clear();
img_split = lineRect(img_tmp, startPoint, stopPoint);
if(img_split[0].size().height>img_split[1].size().height){
char buff[3];
std::string nameindex = itoa(1, buff, 10);
std::string name = "crop "+nameindex;
cv::imshow(name, img_split[1]);
cv::imwrite("D:\\Buff\\tmp1.jpg", img_split[0]);
img_tmp = imread("D:\\Buff\\tmp1.jpg", 1);
cv::imshow("Crop1", img_tmp);
line = checkLine(img_tmp);
check=1;
}
else {
char buff[3];
std::string nameindex = itoa(2, buff, 10);
std::string name = "crop "+nameindex;
cv::imshow(name, img_split[0]);
cv::imwrite("D:\\Buff\\tmp2.jpg", img_split[1]);
Mat img_tmp = imread("D:\\Buff\\tmp2.jpg", 1);
cv::imshow("Crop2", img_tmp);
line = checkLine(img_tmp);
check=2;
}
}
else {
startPoint.push_back(point[0]);
stopPoint.push_back(point[point.size()-1]);
char buff[3];
std::string nameindex = itoa(3, buff, 10);
std::string name = "crop "+nameindex;
cv::Mat img_t = img_split[0];
img_split = lineRect(img_t, startPoint, stopPoint);
cv::imshow(name, img_split[0]);
}
}
错误
Unhandled exception at 0x74dd969b in test.exe: Microsoft C++ exception: cv::Exception at memory location 0x0014ed88..
和
在控制台中
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in unknown function,file ..\..\..\src\opencv\modules\core\src\matrix.cpp, line 323