OpenCV Tutorials —— Basic Thresholding Operations

 

Threshold Binary

  • This thresholding operation can be expressed as:

  • So, if the intensity of the pixel is higher than , then the new pixel intensity is set to a . Otherwise, the pixels are set to .

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

 

Threshold Binary, Inverted

  • This thresholding operation can be expressed as:

  • If the intensity of the pixel is higher than , then the new pixel intensity is set to a . Otherwise, it is set to .

反向二分操作

 

Truncate

  • This thresholding operation can be expressed as:

  • The maximum intensity value for the pixels is , if is greater, then its value is truncated. See figure below:

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

 

Threshold to Zero

  • This operation can be expressed as:

  • If is lower than , the new pixel value will be set to .

 

截短操作的逆操作

Threshold to Zero, Inverted

  • This operation can be expressed as:

  • If is greater than , the new pixel value will be set to .

 

 

#include "stdafx.h"

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

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;
char* window_name = "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 = "Value";

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

/**
 * @function main
 */
int main( int argc, char** argv )
{
  /// Load an image
  src = 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 );
}
时间: 2024-08-02 19:22:42

OpenCV Tutorials —— Basic Thresholding Operations的相关文章

OpenCV Tutorials &mdash;&mdash; Basic Drawing

Point It represents a 2D point, specified by its image coordinates and . We can define it as: Point pt;pt.x = 10;pt.y = 8; or Point pt = Point(10, 8); Scalar Represents a 4-element vector. The type Scalar is widely used in OpenCV for passing pixel va

学习opencv tutorials

1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

OpenCV Tutorials &mdash;&mdash; Mask operations on matrices

Mask operations on matrices are quite simple. The idea is that we recalculate each pixels value in an image according to a mask matrix (also known as kernel). This mask holds values that will adjust how much influence neighboring pixels (and the curr

OpenCV Tutorials &mdash;&mdash; Creating a video with OpenCV

写video 需要用到 VideoWriter  视频文件可看作一个容器 视频的类型由视频文件的后缀名来指定   Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you ca

OpenCV Tutorials &mdash;&mdash; Making your own linear filters

kernel A kernel is essentially a fixed size array of numerical coefficeints along with an anchor point in that array, which is tipically located at the center. The value of the convolution is calculated in the following way: 1,Place the kernel anchor

OpenCV Tutorials &mdash;&mdash; Changing the contrast and brightness of an image

Brightness and contrast adjustments Two commonly used point processes are multiplication and addition with a constant: The parameters and are often called the gain and bias parameters; sometimes these parameters are said to control contrast and brigh

OPENCV(2) &mdash;&mdash; Basic Structures(一)

DataType A primitive OpenCV data type is one of unsigned char, bool,signed char, unsigned short, signed short, int, float, double, or a tuple of values of one of these types, where all the values in the tuple have the same type. 类型的命名格式: CV_<bit-dept

OpenCV Tutorials &mdash;&mdash; Scan images

color space reduction divide the color space current value with a new input value to end up with fewer colors. For instance every value between zero and nine takes the new value zero, every value between ten and nineteen the value ten and so on. 减少颜色

OPENCV(2) &mdash;&mdash; Basic Structures(二)

Mat OpenCV C++ n-dimensional dense array class The class Mat represents an n-dimensional dense numerical single-channel or multi-channel array. It can be used to store real or complex-valued vectors and matrices, grayscale or color images, voxel volu