首先确定特征空间,这里选取了空间坐标x,y和 {MOD}彩空间r,g,b,在实际计算时,是将RGB转换到Lab空间。然后对图像中的每一个点,进行如下操作,对与它位置空间距离小于hr,颜 {MOD}空间距离小于hs的点求平均值,作为新的中心点。 重复这个步骤直到中心点不再移动或者到达最大迭代次数。此时的中心点就作为起始点的聚类中心,将中心点的像素值赋予初始点。
do{
PtPrev = PtCur;// Set the original point and previous one
PtSum =Point5D(0,0,0,0,0);// Initial Sum vector
NumPts =0;// Count number of points that satisfy the bandwidthsfor(int hx = Left; hx < Right; hx++){for(int hy = Top; hy < Bottom; hy++){// Set point in the spatial bandwidth
Pt =Point5D(hx, hy,(float)Img(hx, hy,0),(float)Img(hx, hy,1),(float)Img(hx, hy,2));
Pt.PointLab();// Check it satisfied color bandwidth or notif(Pt.MSPoint5DColorDistance(PtCur)< hr){
PtSum.MSPoint5DAccum(Pt);// Accumulate the point to Sum vector
NumPts++;// Count}}}
PtSum.MSPoint5DScale(1.0/ NumPts);// Scale Sum vector to average vector
PtCur = PtSum;// Get new origin point
step++;// One time end// filter iteration to end}while((PtCur.MSPoint5DColorDistance(PtPrev)> MS_MEAN_SHIFT_TOL_COLOR)&&(PtCur.MSPoint5DSpatialDistance(PtPrev)> MS_MEAN_SHIFT_TOL_SPATIAL)&&(step < MS_MAX_NUM_CONVERGENCE_STEPS));
模点聚类
基本上就是区域生长,从某一点出发,如果和它附近的点8邻域)的颜 {MOD}值相似就合并,同时再从新合并的点出发继续合并下去,直到碰到不相似的点或者该点已经属于另一类了,此时,就退回来,直到退无可退(所有的8邻域搜索空间都已经搜索完毕)。
// Region Growing 8 Neighbours
vector<Point5D> NeighbourPoints;
NeighbourPoints.push_back(PtCur);while(!NeighbourPoints.empty()){
Pt = NeighbourPoints.back();
NeighbourPoints.pop_back();// Get 8 neighboursfor(int k =0; k <8; k++){int hx = Pt.x + dxdy[k][0];int hy = Pt.y + dxdy[k][1];if((hx >0)&&(hy >0)&&(hx < ROWS)&&(hy < COLS)&&(Labels[hx][hy]<0)){
Point5D P(hx, hy,(float)Img(hx, hy,0),(float)Img(hx, hy,1),(float)Img(hx, hy,2));
P.PointLab();// Check the colorif(PtCur.MSPoint5DColorDistance(P)< hr){// Satisfied the color bandwidth
Labels[hx][hy]= label;// Give the same label
NeighbourPoints.push_back(P);// Push it into stack
MemberModeCount[label]++;// This region number plus one// Sum all color in same region
Mode[label *3+0]+= P.l;
Mode[label *3+1]+= P.a;
Mode[label *3+2]+= P.b;}}}}
MemberModeCount[label]++;// Count the point itself
Mode[label *3+0]/= MemberModeCount[label];// Get average color
Mode[label *3+1]/= MemberModeCount[label];
Mode[label *3+2]/= MemberModeCount[label];
阶段效果:(右图对图像二值化)
A4纸矫正
获取角点
对每个像素,遍历它的8邻域,如果全为白 {MOD},就将它变为黑 {MOD},得到边缘图像。
auto res = grayscaled;CImg_3x3(I, uchar);cimg_for3x3(grayscaled, x, y,0,0,I,uchar){if(Ipp==255&&Icp ==255&&Inp ==255&&Ipc ==255&&Icc ==255&&Inc ==255&&Ipn ==255&&Icn ==255&&Inn ==255){res(x, y)=0;}}
利用之前的霍夫变换,可以求出边缘的直线方程,并得到交点坐标。