車牌字元分割—投影法
阿新 • • 發佈:2019-01-05
車牌字元的分割—投影
1.首先,定義一個數組用來儲存每一列畫素中白色畫素的個數。
int perPixelValue;//每個畫素的值
int* projectValArry = new int[width];//建立一個用於儲存每列白色畫素個數的陣列
memset(projectValArry, 0, width*4);//必須初始化陣列
- 1
- 2
- 3
2.然後,遍歷二值化後的圖片,將每一列中白色的(也就是數字區域)畫素記錄在陣列中。
//遍歷每一列的影象灰度值,查詢每一行255的值
for (int col = 0; col < width; ++col)
{
for (int row = 0; row < height; ++row)
{
perPixelValue = binImg.at<uchar>(row, col);
if (perPixelValue == 255)//如果是黑底白字
{
projectValArry[col]++;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
3.最後,根據數組裡的灰度值畫出投影圖
perPixelValue = 255; //背景設定為白色。
verticalProjectionMat.at<uchar>(i, j) = perPixelValue;
}
}
/*將直方圖的曲線設為黑色*/
for (int i = 0; i < width; i++)
{
for (int j = 0; j < projectValArry[i]; j++)
{
perPixelValue = 0; //直方圖設定為黑色
verticalProjectionMat.at<uchar >(height - 1 - j, i) = perPixelValue;
}
}
imshow("【投影】",verticalProjectionMat);
delete[] projectValArry;//刪除陣列空間
有了投影圖做切割就很容易了,其實最主要的就是那個儲存灰度值的陣列,下面就需要根據這個陣列的內容來找到相鄰字元間的分割點。
vector<Mat> roiList;//用於儲存分割出來的每個字元
int startIndex = 0;//記錄進入字元區的索引
int endIndex = 0;//記錄進入空白區域的索引
bool inBlock = false;//是否遍歷到了字元區內
for (int i = 0; i < srcImg.cols; ++i)
{
if (!inBlock && projectValArry[i] != 0)//進入字元區了
{
inBlock = true;
startIndex = i;
cout << "startIndex is " << startIndex << endl;
}
else if (projectValArry[i] == 0 && inBlock)//進入空白區了
{
endIndex = i;
inBlock = false;
Mat roiImg = srcImg(Range(0,srcImg.rows),Range(startIndex,endIndex+1));
roiList.push_back(roiImg);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20