自适应阈值是通过在每个像素点设置周围b*b的区域加权平均然后减去一个常数得到一个阈值,然后进行一些阈值处理,如二值化,截断等。这种阈值处理的方法对有很强照明梯度(光照不均)的图像具有很好的处理效果
// Threshold.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <cv.h> #include <highgui.h> void main() { //以原始通道数读取图片1 IplImage* img1 = cvLoadImage("D://vc6.0//MSDev98//MyProjects//MachineVision//TestPic//PeppersRGB.bmp",CV_LOAD_IMAGE_UNCHANGED); //创建rgb三通道分量 IplImage* r = cvCreateImage(cvGetSize(img1), IPL_DEPTH_8U, 1); IplImage* g = cvCreateImage(cvGetSize(img1), IPL_DEPTH_8U, 1); IplImage* b = cvCreateImage(cvGetSize(img1), IPL_DEPTH_8U, 1); //将图像的三个通道分配到rgb三个子图中 cvSplit(img1, r, g, b, NULL); //临时存储空间 IplImage* s = cvCreateImage(cvGetSize(img1), IPL_DEPTH_8U, 1); IplImage* s2 = cvCreateImage(cvGetSize(img1), IPL_DEPTH_8U, 1); //将3个通道的子图进行加权叠加 cvAddWeighted(r, 1./3, g, 2./3, 0.0, s); cvAddWeighted(s, 1./3, b, 2./3, 0.0, s); //设定阈值为灰度100 cvThreshold(s, s2, 100, 255, CV_THRESH_BINARY); cvAdaptiveThreshold(s, s, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 3, 5); //命名窗口 cvNamedWindow("Origin", CV_WINDOW_AUTOSIZE); cvNamedWindow("Threshold", CV_WINDOW_AUTOSIZE); cvNamedWindow("AdaptiveThreshold", CV_WINDOW_AUTOSIZE); //显示图片 cvShowImage("Origin", img1); cvShowImage("Threshold", s2); cvShowImage("AdaptiveThreshold", s); cvWaitKey(0); cvReleaseImage(&img1); cvReleaseImage(&r); cvReleaseImage(&g); cvReleaseImage(&b); cvDestroyAllWindows(); }
阈值处理
时间: 2024-11-01 12:21:30