I m trying to binarise a picture, firstly of course having it prepared(grayscaling) My method is to find the maximum and minimum values of grayscale, then find the middle value(which is my threshold) and then, iterating over all the pixels I compare the current one with a threshold and if the grayscale is larger than the threshold, I put 0 in a matrix, or for the others I put 1. But now I m facing the problem. In common I m binarising images with white background, so my algorithm is further based on this feature. But when I meet an image with black background everything collapses, but I still can see the number clearly(now 0 s and 1 s switch places) How can i solve this problem, make my program more common? Maybe I d better look for another ways of binarization/
P.S.,我寻求对Otsu门槛值方法作出可以理解的解释,但似乎要么我不为这种困难方式作好准备,要么我每次都找到非常复杂的解释,但我只能用C语书写。 如果有人可以 here,那是 wonder的。
Sorry for not answering the questions, just didn t see them Firstly - the code
for (int y=1;y<Source->Picture->Height;y++)
for (int x=1;x<Source->Picture->Width;x++)
{
unsigned green = GetGValue(Source->Canvas->Pixels[x][y]);
unsigned red = GetRValue(Source->Canvas->Pixels[x][y]);
unsigned blue = GetBValue(Source->Canvas->Pixels[x][y]);
threshold = (0.2125*red+0.7154*green+0.0721*blue);
if (min>threshold)
min=threshold;
if (max<threshold)
max = threshold;
}
middle = (max+min)/2;
然后通过图像进行消化
if (threshold<middle)
{
picture[x][y]=1;
fprintf( fo,"1");
} else {
picture[x][y]=0;
fprintf( fo,"0");
}
}
fprintf( fo,"
");
}
fclose(fo);
因此,我收到了一份档案。
000000000
000001000
000001000
000011000
000101000
000001000
000001000
000001000
000000000
在这方面,你可以看到一个例子。
Then I can interpolate it, or do something else (recognize), depending on zero s and one s. But if I switch the colors, the numbers won t be the same. So the recognition will not work. I wonder if there s an algoritm that can help me out.