CV base (2): OSTU algorithm

1, OTSU algorithm theory

Otsu is proposed by Japanese scholars Otsu (Nobuyuki Otsu) in 1979, it is an adaptive method to determine the threshold of. The algorithm can be assumed that the image pixel threshold value, the background is divided into a [background] and the target [Objects] two portions. Then calculated such that the maximum variance (maximum discrimination) between pixel values ​​of two pixel-based, the pixel value is the division threshold value.

Derivation: M = = total number of pixels referred to a single channel 256 Sum gradation

  • Background pixels than:
  • Foreground pixels than:
  • Average gray value of the background:
  • Prospects mean gray values:
  • Overall gray image accumulated value:
  • Foreground, background inter-class variance:
    Equation 3.4.5 into Equation 6 can be obtained ultimately simplified formula:

2, based on the implementation code OpenCV

Mat base = imread(".//test//1.jpg");
Mat grey;
cvtColor(base, grey, COLOR_RGB2GRAY);
    
Mat seg;
long timeStart = clock();
uint32_t thresh = threshold(grey, seg, 0, 255, THRESH_OTSU);
long timeFinish = clock();
cout << "Calculating time: " << timeFinish - timeStart << endl;

imshow("Origin", base);
imshow("Middle", seg);
imshow("Result", grey);
cout << "The threshold is: " << thresh << endl;
waitKey(0);

Guess you like

Origin www.cnblogs.com/wnwin/p/11205051.html