首页 > 代码库 > OpenCV Tutorials —— Basic Thresholding Operations
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 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