基于图片坐标的自适应KMeans算法

场景:已知图片OCR结果(字符串内容、字符串坐标),需要对图片进行分类,进而进一步定位每列的属性,对每个字符串x+20做垂直方向投影,统计投影结果,二次处理时排除干扰项

 

int CExtractInfo::getK(cv::Mat &srcImg)
{

	cv::Mat grayImg, binImage;
	if (srcImg.channels() == 1)
		grayImg = srcImg.clone();
	else
		cv::cvtColor(srcImg, grayImg, cv::COLOR_RGB2GRAY);

	cv::threshold(grayImg, binImage, 100, 255, THRESH_OTSU);

	//构造mask
	Mat histogramImage(binImage.rows, binImage.cols, CV_8UC1);
	for (int i = 0; i < binImage.rows; i++)
	{
		for (int j = 0; j < binImage.cols; j++)
		{
			histogramImage.at<uchar>(i, j) = 255;
		}
	}
	Mat histogramImage2 = histogramImage.clone();
	Mat histogramImage3 = histogramImage.clone();
	for (int k = 0; k < OrgRecoglList.size(); k++)
	{
		int startCol = OrgRecoglList[k].Pos[0]; //列
		int startRow = OrgRecoglList[k].Pos[1];	//行
		for (; startRow <= (OrgRecoglList[k].Pos[1] + OrgRecoglList[k].Pos[3]); startRow++)
		{
			for (; startCol <= (OrgRecoglList[k].Pos[0] + OrgRecoglList[k].Pos[2]); startCol++)
			{
				histogramImage.at<uchar>(startRow, startCol) = 0;
			}
			startCol = OrgRecoglList[k].Pos[0];;
		}
	}
	
	

	for (int k = 0; k < OrgRecoglList.size(); k++)
	{
		int startCol = OrgRecoglList[k].Pos[0]; //列
		int startRow = OrgRecoglList[k].Pos[1];	//行
		for (; startRow <= (OrgRecoglList[k].Pos[1] + OrgRecoglList[k].Pos[3]); startRow++)
		{
			for (int p = 1; p < 21; p++)
				histogramImage2.at<uchar>(startRow, startCol + p) = 0;
		}
	}

	vector<int> res(histogramImage2.cols, 0);
	for (int i = 0; i < histogramImage2.cols; i++)
	{
		for (int j = 0; j < histogramImage2.rows; j++)
		{
			if (histogramImage2.at<uchar>(j, i) == 0)
				res[i]++;
		}
	}
	for (int i = 0; i < histogramImage2.cols; i++)
	{
		for (int j = 0; j < res[i]; j++)
		{
			histogramImage3.at<uchar>(histogramImage2.rows - 1 - j, i) = 0;
		}
	}

	vector<vector<int>> slist;//直方图每列累计值->空白行为间隔
	for (int i = 0; i <res.size(); i++)
	{
		vector<int> temp;
		while (res[i] != 0)
		{
			temp.push_back(res[i]);
			i++;
		}
		if (temp.size() != 0)
			slist.push_back(temp);

	}

    //imwrite("c:/histogramImage1.jpg", histogramImage);
	//imwrite("c:/histogramImage2.jpg", histogramImage2);
	//imwrite("c:/histogramImage3.jpg", histogramImage3);
	return slist.size();
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值