一:double threshold( InputArray src, OutputArray dst, double thresh, double maxval, int type );
code:
void visionagin::BinaryOperate(Mat&img)//二值化操作
{
Mat temp=img.reshape(1, 0);
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
Mat output, output2;
double min_val = 0, max_val = 0;
Point min_p, max_p;
minMaxLoc(temp, &min_val, &max_val, &min_p, &max_p);
threshold(img,output, 200, max_val, THRESH_BINARY);//img可为多通道,大于阈值200的像素值都为最大值,其余为0
adaptiveThreshold(gray, output2, max_val, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 5, 0);//src1必须为cv_8uc1单通道
imshow("binary", output);
imshow("binary2", output2);
waitKey(0);
}
LUT查找表映射:
代码:
void visionagin::LutOperate(Mat&img)
{
uchar Lutfirst[256];
for (int i = 0; i < 256; ++i)
{
if (i <= 100)
{
Lutfirst[1] = 0;
}
else if (i <= 150)
{
Lutfirst[i] = 200;
}
else
{
Lutfirst[i] = 250;
}
}
uchar Lutsecond[256];
for (int i = 0; i < 256; ++i)
{
if (i <= 80)
{
Lutfirst[1] = 0;
}
else if (i <= 120)
{
Lutfirst[i] = 150;
}
else if (i<=160)
{
Lutsecond[i] = 200;
}
else
{
Lutsecond[i] = 250;
}
}
uchar Lutthird[256];
for (int i = 0; i < 256; ++i)
{
if (i <= 150)
{
Lutthird[1] = 0;
}
else if (i <= 200)
{
Lutthird[i] = 190;
}
else
{
Lutthird[i] = 250;
}
}
Mat Lut1(1, 256, CV_8UC1, Lutfirst);
Mat Lut2(1, 256, CV_8UC1, Lutsecond);
Mat Lut3(1, 256, CV_8UC1, Lutthird);
vector<Mat>Luts;
Luts.push_back(Lut1);
Luts.push_back(Lut2);
Luts.push_back(Lut3);
Mat unionlut;
merge(Luts, unionlut);//拥有三通道的查找表矩阵
Mat output1,output2,output3, gry;
cvtColor(img, gry, COLOR_BGR2GRAY);
LUT(img, Lut1, output1);//第一个参数只能说CV_8U
LUT(img, Lut2, output2);
LUT(img, Lut3, output3);
imshow("LUT1", output1);
imshow("LUT2", output2);
imshow("LUT3", output3);
}