首页 > 代码库 > OpenCV Tutorials —— Basic Thresholding Operations

OpenCV Tutorials —— Basic Thresholding Operations

Threshold Binary

 

Threshold Binary

  • This thresholding operation can be expressed as:

    \texttt{dst} (x,y) =  \fork{\texttt{maxVal}}{if $\texttt{src}(x,y) > \texttt{thresh}$}{0}{otherwise}

  • So, if the intensity of the pixel src(x,y) is higher than thresh, then the new pixel intensity is set to a MaxVal. Otherwise, the pixels are set to 0.

    Threshold Binary

二分 ~~ 阈值化最朴素的形式

 

Threshold Binary, Inverted

  • This thresholding operation can be expressed as:

    \texttt{dst} (x,y) =  \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{maxVal}}{otherwise}

  • If the intensity of the pixel src(x,y) is higher than thresh, then the new pixel intensity is set to a 0. Otherwise, it is set to MaxVal.

    Threshold Binary Inverted

反向二分操作

 

Truncate

  • This thresholding operation can be expressed as:

    \texttt{dst} (x,y) =  \fork{\texttt{threshold}}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{src}(x,y)}{otherwise}

  • The maximum intensity value for the pixels is thresh, if src(x,y) is greater, then its value is truncated. See figure below:

    Threshold Truncate

截短 ———— 发现渐渐调整阈值可以做出很有美感的demo :)

 

Threshold to Zero

  • This operation can be expressed as:

    \texttt{dst} (x,y) =  \fork{\texttt{src}(x,y)}{if $\texttt{src}(x,y) > \texttt{thresh}$}{0}{otherwise}

  • If src(x,y) is lower than thresh, the new pixel value will be set to 0.

    Threshold Zero

 

截短操作的逆操作

Threshold to Zero, Inverted

  • This operation can be expressed as:

    \texttt{dst} (x,y) =  \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{src}(x,y)}{otherwise}

  • If src(x,y) is greater than thresh, the new pixel value will be set to 0.

    Threshold Zero Inverted

 

 

#include "stdafx.h"#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"#include <stdlib.h>#include <stdio.h>using namespace cv;/// Global variablesint threshold_value = http://www.mamicode.com/0;"Threshold Demo";char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";char* trackbar_value = "http://www.mamicode.com/Value";/// Function headersvoid Threshold_Demo( int, void* );/** * @function main */int main( int argc, char** argv ){  /// Load an image  src = http://www.mamicode.com/imread("zanxin.jpg", 1 );  /// Convert the image to Gray  cvtColor( src, src_gray, CV_RGB2GRAY );  /// Create a window to display results  namedWindow( window_name, CV_WINDOW_AUTOSIZE );  /// Create Trackbar to choose type of Threshold  createTrackbar( trackbar_type,                  window_name, &threshold_type,                  max_type, Threshold_Demo );  createTrackbar( trackbar_value,                  window_name, &threshold_value,                  max_value, Threshold_Demo );  /// Call the function to initialize  Threshold_Demo( 0, 0 );  /// Wait until user finishes program  while(true)  {    int c;    c = waitKey( 20 );    if( (char)c == 27 )      { break; }   }}/** * @function Threshold_Demo */void Threshold_Demo( int, void* )	// 不需要参数{  /* 0: Binary     1: Binary Inverted     2: Threshold Truncated     3: Threshold to Zero     4: Threshold to Zero Inverted   */  threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );  imshow( window_name, dst );}

OpenCV Tutorials —— Basic Thresholding Operations