Implementing Stroke Width Estimation

asked 2017-05-16 11:20:00 -0600

Kenny Karnama gravatar image

updated 2017-05-16 11:49:57 -0600

Hi guys, i found an algorithm which is about "stroke width estimation". I have tried to implement it in opencv. And get stuck in the third step. Please i really need help. The algorithm is :

  1. For each column, count the number of black pixels and the transitions between black and white i.e the number of runs
  2. Estimate maxwidth by --> maxwidth = (no. of black pixels / total no. of runs) * 1.5
  3. Repeat step 1 after discarding those black pixels whose run length is greater than maxwidth
  4. The estimated width is the count of black pixels divided by the count of runs.

For the first step, my code :

int numOfBlack = 0;
int numOfRun = 0;

for(int i=0;i<thresholdImg.cols;i++){
  for(int j=0;j<thresholdImg.rows;j++){
    if(thresholdImg.at<uchar>(j,i)==0){
      numOfBlack++;
      if((j+1)<thresholdImg.rows){
        if(thresholdImg.at<uchar>(j+1,i)>0){
          numOfRun++;
        }
      }
    }

  }
}

For the second step, my code :

double maxWidth = ((numOfBlack*1.0)/(numOfRun*1.0))*1.5;

And for the third step, i don't know how to do it, basically i think like this :

int numOfBlack2 = 0;
int numOfRun2 = 0;

for(int i=0;i<thresholdImg.cols;i++){
  int coba = 0;
  int cobaRun = 0;
  for(int j=0;j<thresholdImg.rows;j++){
    if(thresholdImg.at<uchar>(j,i)==0){
      coba++;
      if((j+1)<thresholdImg.rows){
        if(thresholdImg.at<uchar>(j+1,i)>0){
          cobaRun++;
        }
      }
    }

  }

  cout<<cobaRun<<endl;

  if((cobaRun*1.0<=maxWidth)){
    numOfBlack2+=coba;
    numOfRun2+=cobaRun;
  }
}

And for the fourth step :

double strokeWitdth = ((numOfBlack2*1.0)/(numOfRun2*1.0));

Actually, i confuse in the third step. Can anyone help me ? because i am completely stuck in the third step. Thanks

edit retag flag offensive close merge delete