Binaryzacja

Binaryzacja z zadanym progiem 

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h> 

using namespace std;
using namespace cv;

int main(){
  Mat img = imread("./LENA_512.jpg", 1 );
  cvtColor(img, img, CV_BGR2GRAY);
  for(int i = 1 ; i < 10; i ++){
    Mat binary_img;
    double border = 100+10*i;
    threshold(img, binary_img, border, 255,THRESH_BINARY);
  }
  waitKey(0);
  return 0;  
}

Binaryzacja z zadanym progiem 

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h> 

using namespace std;
using namespace cv;

/// Global Variables
Mat img; 
int border = 100;
/// Function Headers
void binarization(int, void* );
 
int main(){
  /// Read the image
  img = imread( "./LENA_512.jpg", 1 );
  cvtColor(img, img, CV_BGR2GRAY);
 
 
  /// Create Trackbar to enter the number of bins
  char* window_image = "Source image";
  namedWindow( window_image, CV_WINDOW_AUTOSIZE );
  createTrackbar("* Binary: ", window_image, &border, 255, binarization );
  binarization(1, 0);
 
  /// Show the image
  imshow( window_image, img );
 
  /// Wait until user exits the program
  waitKey(0);
  return 0;
}
 
void binarization(int, void* ){
  Mat binary_img;
  threshold(img, binary_img, border, 255,THRESH_BINARY);
  imshow( "Binary", binary_img );
}

Binaryzacja z histogramem  

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h> 

using namespace std;
using namespace cv;

void histogram(const Mat &img);

int main(){
  Mat img = imread("./num.jpg", 1 );
  cvtColor(img, img, CV_BGR2GRAY);
  histogram(img);
  Mat binary_img;
  double border = 116;
  threshold(img, binary_img, border, 255,THRESH_BINARY);
  imshow( "Binary", binary_img );
  waitKey(0);
  return 0;  
}  

void histogram(const Mat &img){
  int histSize = 256;
  /// Set the ranges )
  float range[] = { 0, 256 } ;
  const float* histRange = { range };
  bool uniform = true; 
  bool accumulate = false;
  Mat grey_hist;
  /// Compute the histograms:
  calcHist( &img, 1, 0, Mat(), grey_hist, 1, &histSize, &histRange, uniform, accumulate );
  // Draw the histograms 
  int hist_w = 256; 
  int hist_h = 200;
  int bin_w = cvRound( (double) hist_w/histSize ); 
  Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
  /// Normalize the result to [ 0, histImage.rows ]
  normalize(grey_hist, grey_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
  // Draw for each channel
  for( int i = 1; i < histSize; i++ ){
    line( histImage, Point( bin_w*(i-1), hist_h - cvRound(grey_hist.at<float>(i-1)) ) ,
    Point( bin_w*(i), hist_h - cvRound(grey_hist.at<float>(i)) ),
    Scalar( 120, 120, 120), 2, 8, 0  );
  }
  /// Display
  namedWindow("Hist", CV_WINDOW_AUTOSIZE );
  imshow("Hist", histImage );
};

Binaryzacja metodą Otsu 

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h> 

using namespace std;
using namespace cv;

int main(){
  Mat img = imread("./LENA_512.jpg", 1 );
  cvtColor(img, img, CV_BGR2GRAY);

	Mat binary_img;
  double border = 110;
  threshold(img, binary_img, border, 255,THRESH_BINARY |THRESH_OTSU );

  namedWindow("Binary", WINDOW_AUTOSIZE );
  imshow("Binary", binary_img );
  waitKey(0);
  return 0;  
}  

Bianryzacja lokalna 

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h> 

using namespace std;
using namespace cv;

int main(){
  Mat img = imread("./gazeta.jpg", 1 );
  cvtColor(img, img, CV_BGR2GRAY);

  Mat binary_img;
  int block_size =11;
  double offset =0.1;
  adaptiveThreshold( img, binary_img, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, block_size, offset );
  namedWindow("Binary mean", WINDOW_AUTOSIZE );
  imshow("Binary mean", binary_img );
  adaptiveThreshold( img, binary_img, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, block_size, offset );
  namedWindow("Binary Gauss", WINDOW_AUTOSIZE );
  imshow("Binary Gauss", binary_img );
  waitKey(0);
  return 0;  
}  

Bianryzacja lokalna 

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h> 

using namespace std;
using namespace cv;

int main(){
  Mat img = imread("./LENA_512.jpg", 1 );
  cvtColor(img, img, CV_BGR2GRAY);
  for(int i = 1 ; i < 20; i++){
    Mat binary_img;
    int block_size =11;
    double offset =i;
    adaptiveThreshold( img, binary_img, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, block_size, offset );
    namedWindow( "Binary", WINDOW_AUTOSIZE );
    imshow( "Binary", binary_img );
  }
  waitKey(0);
  return 0;  
}    

Semi binaryzacja 

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h> 

using namespace std;
using namespace cv;

int main(){
  Mat img = imread("./num.jpg", 1 );
  cvtColor(img, img, CV_BGR2GRAY);

  Mat binary_img;
  Mat semi_thresholded_img;
  double border = 115;
  threshold(img, binary_img, border, 255,THRESH_BINARY_INV);
  bitwise_and( img, binary_img, semi_thresholded_img );

  namedWindow("Semi binary", WINDOW_AUTOSIZE );
  imshow("Semi binary", semi_thresholded_img );

  waitKey(0);
  return 0;  
}    

Binaryzacja z podwójnym progiem 

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h> 

using namespace std;
using namespace cv;

int main(){
  Mat img = imread("./num.jpg", 1 );
  cvtColor(img, img, CV_BGR2GRAY);

  Mat binary_img1;
  Mat binary_img2;
  Mat band_thresholded_img;
  int low_threshold = 110;
  int high_threshold = 126;
  threshold( img, binary_img1, low_threshold, 255, THRESH_BINARY );
  threshold( img, binary_img2, high_threshold, 255, THRESH_BINARY_INV );
  bitwise_and( binary_img1, binary_img2, band_thresholded_img );

  namedWindow("Binary", WINDOW_AUTOSIZE );
  imshow("Binary", band_thresholded_img );
  waitKey(0);
  return 0;  
}