OpenCV中阈值化的5种形式
CVAPI(double) cvThreshold( const CvArr* src, CvArr* dst,
double threshold, double max_value,
int threshold_type );
src源图像,dst输出图像,threshold阈值,max_value,threshold_type 上面贴图的5种阈值化形式
阈值化的图像必须是单通道的,需要先用cvSplit() 和cvAddWeighted()
测试用图:
程序代码:
#include
#include
#include
using namespace std;
int t,Threshold,Max_value;
IplImage *img_in = cvLoadImage("test.jpg");
IplImage *img_blue = cvCreateImage(cvGetSize(img_in),IPL_DEPTH_8U,1);
IplImage *img_green = cvCreateImage(cvGetSize(img_in),IPL_DEPTH_8U,1);
IplImage *img_red = cvCreateImage(cvGetSize(img_in),IPL_DEPTH_8U,1);
IplImage *img_all = cvCreateImage(cvGetSize(img_in),IPL_DEPTH_8U,1);
IplImage *img_out = cvCreateImage(cvGetSize(img_in),IPL_DEPTH_8U,1);
IplImage *img_control = cvCreateImage(cvGetSize(img_in),IPL_DEPTH_8U,1);
void on_trackbar_1(int Threshold_pos)
{
Threshold=Threshold_pos;
cvSplit(img_in,img_blue,img_green,img_red,NULL);
cvAddWeighted(img_blue,1.0/3,img_green,1.0/3,0.0,img_all);
cvAddWeighted(img_all,2.0/3,img_red,1.0/3,0.0,img_all);
cvThreshold(img_all,img_out,Threshold,Max_value,CV_THRESH_BINARY);
cvThreshold(img_all,img_out,Threshold,Max_value,CV_THRESH_TRUNC);
cvNamedWindow("img_out",CV_WINDOW_AUTOSIZE);
cvShowImage("img_out",img_out);
}
void on_trackbar_2(int Max_value_pos)
{
Max_value=Max_value_pos;
cvSplit(img_in,img_blue,img_green,img_red,NULL);
cvAddWeighted(img_blue,1.0/3,img_green,1.0/3,0.0,img_all);
cvAddWeighted(img_all,2.0/3,img_red,1.0/3,0.0,img_all);
cvThreshold(img_all,img_out,Threshold,Max_value,CV_THRESH_BINARY);
cvNamedWindow("img_out",CV_WINDOW_AUTOSIZE);
cvShowImage("img_out",img_out);
}
int main()
{
cvNamedWindow("img_in",CV_WINDOW_AUTOSIZE);
cvShowImage("img_in",img_in);
cvSplit(img_in,img_blue,img_green,img_red,NULL);
cvAddWeighted(img_blue,1.0/3,img_green,1.0/3,0.0,img_all);
cvAddWeighted(img_all,2.0/3,img_red,1.0/3,0.0,img_all);
cvNamedWindow("img_all",CV_WINDOW_AUTOSIZE);
cvShowImage("img_all",img_all);
cvNamedWindow("img_control",CV_WINDOW_AUTOSIZE);
cvCreateTrackbar("Threshold","img_control",&t,255,on_trackbar_1);
cvCreateTrackbar("Max_value","img_control",&t,255,on_trackbar_2);
cvNamedWindow("img_out",CV_WINDOW_AUTOSIZE);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&img_in);
cvReleaseImage(&img_blue);
cvReleaseImage(&img_green);
cvReleaseImage(&img_red);
cvReleaseImage(&img_all);
cvReleaseImage(&img_out);
cvReleaseImage(&img_control);
return 0;
}
截断模式:
二值化模式: