0

I want to binarize an image using local thrersholding( localized Otsu, Niblack, Sauvola ). I have implemented global Otsu. See the following code-

#include<iostream>
#include<string>
#include<vector>
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>


using namespace std;
using namespace cv;

int main() 
{

    vector<cv::String> fn;
    glob("G:\\files\\*.jpg", fn, false);
    string out = "F:\\image_out\\";

    Mat image, gray, blur, blackWhite;

    size_t count = fn.size(); //number of png files in images folder
    for (size_t i=0; i<count; i++){
    
        image = imread(fn[i], IMREAD_COLOR);
        cvtColor(image, gray, COLOR_BGR2GRAY);
        GaussianBlur(gray, blur, Size(7, 7), 0, 0);
        threshold(blur, blackWhite, 0, 255, THRESH_BINARY | THRESH_OTSU);
    
        imwrite((out+to_string(i)+".png"), blackWhite);
        imshow( "black white image ", blackWhite); //optional
    }
    waitKey(0);
    destroyAllWindows();
}

Now I want to replace global thresholding with local thresholding.
Note: If the same process can be done without opencv, that will also do. But ofcourse with C++.

  • You mean [adaptive thresholding](https://docs.opencv.org/master/d7/d1b/group__imgproc__misc.html#ga72b913f352e4a1b1b397736707afcde3)? – Prefect Nov 21 '20 at 07:28
  • @lammuratc I don't know whether adoptive thresholding works like local thresholding or not. That's why I have asked – sadikul haque sadi Nov 21 '20 at 07:53
  • Adaptive thresholding binarizes the image by sliding the window on the image. So it is a local thresholding than a global threshold. You can read about it more in the link. Is it what you mean with local thresholding? – Prefect Nov 21 '20 at 10:32
  • I want to have different threshold values for different parts of the image rather than just a single threshold value – sadikul haque sadi Nov 21 '20 at 12:35
  • 1
    Adaptive thresholding would work in cases where you have non-uniform lighting in the image, say, for eg. shadows. In case your image falls in that category adaptive thresholding should yield decently segmented image. You might have to fiddle around with the block size and offset values though. – Knight Forked Nov 21 '20 at 14:01

0 Answers0