Image Processing7(Basic Thresholding Operations)

Goal

这里主要学习图像处理的基本阀值处理。cv::threshold

Cool Theory

理论来源于《Learning OpenCV》

Thresholding?

  • The simplest segmentation method
  • Application example: Separate out regions of an image corresponding to objects which we want to analyze. This separation is based on the variation of intensity between the object pixels and the background pixels.
  • To differentiate the pixels we are interested in from the rest (which will eventually be rejected), we perform a comparison of each pixel intensity value with respect to a threshold (determined according to the problem to solve).
  • Once we have separated properly the important pixels, we can set them with a determined value to identify them (i.e. we can assign them a value of 0 (black), 255 (white) or any value that suits your needs).

最简单的分割方法

应用实例:分割出我们想要分析对象的区域。这种分割时基于对象像素和背景像素之间的变化。

为了区分我们感兴趣区域的像素与其他像素,这里让每个像素与阀值进行比较。

一旦我们分离了出这些像素,我们可以给他们一些特定的值和颜色。

Types of Thresholding

  • OpenCV offers the function cv::threshold to perform thresholding operations.
  • We can effectuate 5 types of Thresholding operations with this function. We will explain them in the following subsections.
  • To illustrate how these thresholding processes work, let's consider that we have a source image with pixels with intensity values src(x,y). The plot below depicts this. The horizontal blue line represents the threshold thresh (fixed).

我们使用cv::shreshold来执行类似的操作。

使用改函数可以实现5种类型的阀值。稍后解释。

为了说明阀值过程如何工作的,假定原图片,并且像素为SRC(x,y).下面的图描述了这些。其中的水平线代表了阀值。

                                                        Image Processing7(Basic Thresholding Operations)

Threshold Binary

  • This thresholding operation can be expressed as:

    dst(x,y)={maxVal0if src(x,y)>threshotherwise
  • 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.

二进制阀值!大于阀值,则为设定的值,否则为零。
                                                        Image Processing7(Basic Thresholding Operations)

Threshold Binary, Inverted

  • This thresholding operation can be expressed as:

    dst(x,y)={0maxValif src(x,y)>threshotherwise
  • 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.

                                                    Image Processing7(Basic Thresholding Operations)
二进制阀值,不过进行反转。大于阀值为零,否则为设定值。

runcate

  • This thresholding operation can be expressed as:

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

                                                      Image Processing7(Basic Thresholding Operations)

截断:大于阀值为阀值,否为不变。

Threshold to Zero

  • This operation can be expressed as:

    dst(x,y)={src(x,y)0if src(x,y)>threshotherwise
  • If src(x,y) is lower than thresh, the new pixel value will be set to 0.

                                                       Image Processing7(Basic Thresholding Operations)

阀值为零,大于阀值不变,否则为零。

Threshold to Zero, Inverted

  • This operation can be expressed as:

    dst(x,y)={0src(x,y)if src(x,y)>threshotherwise
  • If src(x,y) is greater than thresh, the new pixel value will be set to 0.

                                                    Image Processing7(Basic Thresholding Operations)
阀值为零,反转!大于阀值为零,否则不变。

Code

#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;

// Global variables

int threshold_value = 0;
int threshold_type = 3;
int const max_value = 255;
int const max_type = 4;
int const max_BINARY_value = 255;

Mat src, src_gray, dst;
const char* window_name = "Threshold Demo";

const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
const char* trackbar_value = "Value";

// Function headers
void Threshold_Demo( int, void* );

/**
 * @function main
 */

int main( int argc, char** argv )
{
  //! [load]
  String imageName("lena.jpg"); // by default
  if (argc > 1)
  {
      imageName = argv[1];
  }
  src = imread( imageName, IMREAD_COLOR ); // Load an image

  if( src.empty() )
    { return -1; }

  cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to Gray
  //! [load]

  //! [window]

  namedWindow( window_name, WINDOW_AUTOSIZE ); // Create a window to display results
  //! [window]

  //! [trackbar]

  createTrackbar( trackbar_type,
                  window_name, &threshold_type,
                  max_type, Threshold_Demo ); // Create Trackbar to choose type of Threshold

  createTrackbar( trackbar_value,
                  window_name, &threshold_value,
                  max_value, Threshold_Demo ); // Create Trackbar to choose Threshold value
  //! [trackbar]


  Threshold_Demo( 0, 0 ); // Call the function to initialize

  /// Wait until user finishes program
  for(;;)
    {
      char c = (char)waitKey( 20 );
      if( c == 27 )
    { break; }
    }

}

//![Threshold_Demo]
/**
 * @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 );
}

//![Threshold_Demo]

代码较为简单,注释较为清楚,这里不再解释。

CMakeLists.txt文件如下

cmake_minimum_required(VERSION 2.8)

set(CMAKE_CXX_FLAGS "-std=c++11")
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )


install(TARGETS DisplayImage RUNTIME DESTINATION bin)

结果如下:

Image Processing7(Basic Thresholding Operations)