阀值法是最简单的分割方法(根据对象像素和背景像素强度的不同)。
在Opencv的基本阀值函数threshold中支持5种不同类型的阀值操作:
- Threshold Binary:
2. Threshold Binary,Inverted
3. Truncate(截取)
4.Threshold to Zero
5. Threshold to Zero, Inverted
下面展示最简单的一种:THRESH_BINARY
#include "cv.h"
#include "highgui.h"
using namespace cv;
using namespace std;
int main(int argc,char *argv[])
{
Mat src,dst;
double thresh=30,maxval=255;
src=imread("fish.jpg",0);
imshow("src",src);
threshold(src,dst,thresh,maxval,THRESH_BINARY);
imshow("dst",dst);
cvWaitKey(0);
destroyAllWindows();
return 0;
}
效果:
fish.jpg: dst: