English 中文(简体)
B. 横向化方法、中门槛的双重化
原标题:Binarization methods, middle-threshold binarisation

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

在这方面,你可以看到一个例子。 “entergraph

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.

最佳回答

我从未听说过Otsus方法,但我理解一些基百页,因此我试图简化这一方法。

1 Count how many pixels are at each level of darkness.
2 "Guess" a threshold.
3 Calculate the variance of the counts of darkness less than the threshold
4 Calculate the variance of the counts of darkness greater than the threshold
5 If the variance of the darker side is greater, guess a darker threshold, 
  else guess a higher threshold.
  Do this like a binary search so that it ends.
6 Turn all pixels darker than threshold black, the rest white.

抽水法实际上是“尽量扩大跨层差异”,但我不理解这部分数学。

差异概念是“彼此的数值相距甚远”。 差异不大意味着一切都相似。 差异很大,意味着价值大相径庭。 雨果差异很大,有色。 st流的背景差异是零,因为它完全是白人,没有其他肤色。 差异的计算方式大致如下:

double variance(unsigned int* counts, int size, int threshold, bool above) {
    //this is a quick trick to turn the "upper" into lower, save myself code
    if (above) return variance(counts, size-threshold, size-threshold, false);
    //first we calculate the average value
    unsigned long long atotal=0;
    unsigned long long acount=0;
    for(int i=0; i<threshold; ++i) {
        atotal += counts[i]*i //number of px times value
        acount += counts[i];
    }
    //finish calculating average
    double average = double(atotal)/count;
    //next we calculate the variance
    double vtotal=0;
    for(int i=0; i<threshold; ++i) {
        //to do so we get each values s difference from the average
        double t = std::abs(i-average);
        //and square it (I hate mathmaticians)
        vtotal += counts[i]*t*t;
    }
    //and return the average of those squared values.
    return vtotal/count;
}
问题回答

我将采取另一种做法处理这个问题:

  • Compute the cumulative histogram of greyscaled values of the image.
  • Use as threshold the pixel value in which this cumulative reaches half of the total pixels of the image.

算法如下:

  int bin [256];
  foreach pixel in image
       bin[pixelvalue]++;  
  endfor  // this computes the histogram of the image

  int thresholdCount = ImageWidth * ImageSize / 2;
  int count = 0;
  for int i = 0 to 255
    count = count + bin[i];
    if( count > thresholdCount)
         threshold = i;
         break; // we are done
    endif
  endfor

This algorithm does not compute the cumulative histogram itself but rather uses the image histogram to do what I said earlier.

如果你本人为白人背景做了适当的工作,但因黑人背景而失败,那么你就完全需要发现你有黑色背景,歪曲价值观。 如果你认为背景价值更为普遍,那么你就只能计算结果的1和0;如果0或更多,则不考虑结果。

Instead of using mean of min and max, you should use median of all points as threshold. In general kth percentile (k = what percentage of points you want as black) is more appropriate.

另一个解决办法是将数据分为两组。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签