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 –