二值化函数Threshold
生活随笔
收集整理的這篇文章主要介紹了
二值化函数Threshold
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Threshold函數(shù):
函數(shù)的調(diào)用方式:
void cvThreshold( const CvArr* src, CvArr* dst, double threshold,double max_value, int threshold_type ); 函數(shù)參數(shù)詳解:
src 原始數(shù)組 (單通道 , 8-bit of 32-bit 浮點(diǎn)數(shù)).
?dst 輸出數(shù)組,必須與 src 的類型一致,或者為 8-bit. threshold?
閾值 max_value 使用 CV_THRESH_BINARY 和 CV_THRESH_BINARY_INV 的最大值.
?threshold_type 閾值類型?
函數(shù) cvThreshold 對(duì)單通道數(shù)組應(yīng)用固定閾值操作。該函數(shù)的典型應(yīng)用是對(duì)灰度圖像進(jìn)行閾值操作得到二值圖像。(cvCmpS 也可以達(dá)到此目的) 或者是去掉噪聲,例如過(guò)濾很小或很大象素值的圖像點(diǎn)。本函數(shù)支持的對(duì)圖像取閾值的方法由 threshold_type 確定:
threshold_type=CV_THRESH_BINARY: dst(x,y) = max_value, if src(x,y)>threshold0, otherwisethreshold_type=CV_THRESH_BINARY_INV: dst(x,y) = 0, if src(x,y)>thresholdmax_value, otherwisethreshold_type=CV_THRESH_TRUNC: dst(x,y) = threshold, if src(x,y)>thresholdsrc(x,y), otherwisethreshold_type=CV_THRESH_TOZERO: dst(x,y) = src(x,y), if (x,y)>threshold0, otherwisethreshold_type=CV_THRESH_TOZERO_INV: dst(x,y) = 0, if src(x,y)>thresholdsrc(x,y), otherwise opencv代碼:#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream>using namespace std; using namespace cv;int main() {Mat img = imread("d:6.jpg");Mat dst(img.size(),8,1);cvtColor(img, img, CV_BGR2GRAY);threshold(img, img, 100, 255, CV_THRESH_BINARY);/*dilate(img, dst, NULL, Point(-1, -1), 10, BORDER_DEFAULT, Scalar(0, 0, 255));*/namedWindow("shiyan");imshow("shiyan", img);waitKey(0);return 0; }
或者:
#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <stdlib.h> #include <stdio.h>using namespace cv;/// 全局變量定義及賦值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";/// 自定義函數(shù)聲明 void Threshold_Demo( int, void* );/*** @主函數(shù)*/ int main( int argc, char** argv ) {/// 讀取一副圖片,不改變圖片本身的顏色類型(該讀取方式為DOS運(yùn)行模式)src = imread( argv[1], 1 );/// 將圖片轉(zhuǎn)換成灰度圖片cvtColor( src, src_gray, CV_RGB2GRAY );/// 創(chuàng)建一個(gè)窗口顯示圖片namedWindow( window_name, CV_WINDOW_AUTOSIZE );/// 創(chuàng)建滑動(dòng)條來(lái)控制閾值createTrackbar( trackbar_type,window_name, &threshold_type,max_type, Threshold_Demo );createTrackbar( trackbar_value,window_name, &threshold_value,max_value, Threshold_Demo );/// 初始化自定義的閾值函數(shù)Threshold_Demo( 0, 0 );/// 等待用戶按鍵。如果是ESC健則退出等待過(guò)程。while(true){int c;c = waitKey( 20 );if( (char)c == 27 ){ break; }}}/*** @自定義的閾值函數(shù)*/ void Threshold_Demo( int, void* ) {/* 0: 二進(jìn)制閾值1: 反二進(jìn)制閾值2: 截?cái)嚅撝?: 0閾值4: 反0閾值*/threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );imshow( window_name, dst ); }
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的二值化函数Threshold的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。