2016-12-24 2 views

答えて

0

イメージ内の各黒いピクセルの長さを計算するように頼んだら、ポインタでイメージをスキャンして長さを計算できます。画像へのポインタを使うと、ポインタは最初の行を指しています。ここにコードがあります

Mat image; //this is the image that contain the above image; 
int numberOfRow=image.rows;//number of rows in the image 
int length[4]; 
int forTheFirstTime=0; 
int index=0; 
int a=0; 
for(int j=0;j<numberOfRow;j++) 
{ 
    uchar* data=image.ptr<uchar>(j);//the pointer to each row in the image 
    if(data[0]==0) //if the pixel at the first is black 
    { 
    a++; 
    } 
    if(data[0]==1) 
    { 
    if(forTheFirstTime==0)//if the white pixel is for the first time 
    { 
     length[index]=a; 
     a=0; 
     index++ 
    } 
    else 
    { 
     //do nothing 
    } 
    } 
} 

もっと検索Googleポインターで画像をスキャンしています。

+0

'image.ptr'について知ってくれてありがとう。 –

0

imgは、問題の画像です。

このように記述しました。

Mat img_inv; 
bitwise_not(img, img_inv); 

int totalrows = img_inv.rows; 
int totalcols = img_inv.cols; 

int hist[totalrows]; 
int blackspacecount = 0; 
bool isbackground = true; 

for(int i=0; i<totalrows; i++) 
{ 
    hist[i] = countNonZero(img_inv(Rect(0,i,totalcols,1))); 

    if(hist[i] > 0) 
    { 
     if(isbackground == true) 
     { 
      blackspacecount++; 
      isbackground = false; 
     } 
    } 
    else 
    { 
     isbackground = true; 
    } 
} 



int blackspaces[blackspacecount][2]; 

int p = 0; 

for(int i=1; i<totalrows; i++) 
{ 
    if (hist[i] > 0 && hist[i-1] == 0) 
    { 
     blackspaces[p][0] = i; // Begin 
    } 
    if (hist[i] == 0 && hist[i-1] > 0) 
    { 
     blackspaces[p][1] = i-1; // End 
     p++; 
    } 
} 

他の方法はありますか?

関連する問題