for (int i = 0; i < binary.rows; i++)
{
for (int j = 0; j < pointcount[i]; j++) //根据每行中黑色点的个数,进行循环
{
painty.at<uchar> (i, j) = 0;
}
}
imshow ("painty", painty);
vector<Mat> result;
int startindex = 0;
int endindex = 0;
bool inblock = false; //是否遍历到字符位置
for (int i = 0; i < painty.rows; i++)
{
if (!inblock&&pointcount[i] != 0) //进入有字符区域
{
inblock = true;
startindex = i;
cout << "startindex:" << startindex << endl;
}
if (inblock&&pointcount[i] == 0) //进入空白区
{
endindex = i;
inblock = false;
Mat roi = binary.rowRange (startindex, endindex+1); //从而记录从开始到结束行的位置,即可进行行切分
result.push_back (roi);
}
}
for (int i = 0; i < result.size(); i++)
{
Mat tmp = result[i];
imshow ("test"+to_string (i), tmp);
}
delete pointcount;
}
int main (int argc, char* argv[])
{
Mat src = cv::imread ("test.jpg");
if (src.empty())
{
cerr << "Problem loading image!!!" << endl;
return -1;
}
imshow("in",src);
Mat gray;
if (src.channels() == 3)
{
cv::cvtColor (src, gray, CV_BGR2GRAY);
}
else
{
gray = src;
}
Mat bw2 = Mat::zeros (gray.size(), CV_8UC1);
thresholdIntegral (gray, bw2);
cv::imshow ("binary integral", bw2);
//picshadowx (bw2);
picshadowy (bw2);
waitKey (0);
return 0;
}
输入图片:
二值图片:
水平投影:
垂直投影:
行切割:
该处理方法,对印刷体有较好的效果,因为印刷体的行列区分明显,因此可以很快的进行行与列的分割。