OpenCV的在框架中提供該自適應閾值的範例:http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#adaptivethreshold
函數原型看起來像:
void adaptiveThreshold(InputArray src, OutputArray dst,
double maxValue, int adaptiveMethod,
int thresholdType, int blockSize, double C);
前兩個參數是輸入圖像和存儲在輸出閾值的圖像的位置。 maxValue
是分配給輸出像素的閾值,如果它通過條件,adaptiveMethod
是用於自適應閾值處理的方法,thresholdType
是要執行的閾值類型(稍後),blockSize
是要檢查的窗口大小(稍後),並且C
是從每個窗口中減去的常量。我從來沒有真正需要使用這個,我通常將其設置爲0。
的默認方法adaptiveThreshold
是分析blockSize x blockSize
窗口以及該窗口被C
減去內計算平均強度。如果此窗口的中心高於平均強度,則輸出圖像輸出位置中的相應位置設置爲maxValue
,否則相同位置設置爲0.這應該對抗非均勻照明問題,而不是應用圖像的全局閾值,您要對局部像素鄰域執行閾值處理。
你可以閱讀其他方法對其他參數的文件,而是讓你開始,你可以做這樣的事情:
// Include libraries
#include <cv.h>
#include <highgui.h>
// For convenience
using namespace cv;
// Example function to adaptive threshold an image
void threshold()
{
// Load in an image - Change "image.jpg" to whatever your image is called
Mat image;
image = imread("image.jpg", 1);
// Convert image to grayscale and show the image
// Wait for user key before continuing
Mat gray_image;
cvtColor(image, gray_image, CV_BGR2GRAY);
namedWindow("Gray image", CV_WINDOW_AUTOSIZE);
imshow("Gray image", gray_image);
waitKey(0);
// Adaptive threshold the image
int maxValue = 255;
int blockSize = 25;
int C = 0;
adaptiveThreshold(gray_image, gray_image, maxValue,
CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,
blockSize, C);
// Show the thresholded image
// Wait for user key before continuing
namedWindow("Thresholded image", CV_WINDOW_AUTOSIZE);
imshow("Thresholded image", gray_image);
waitKey(0);
}
// Main function - Run the threshold function
int main(int argc, const char** argv)
{
threshold();
}
在上述鏈接**中使用的大多數功能**都可以在OpenCV中使用。 – Miki
是的,試試這個cv :: morphologyEx –