该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
Mat findlaser(Mat img, int model)
{
if (!img.data)
{
std::cout << "No data!" << std::endl;
system("pause");
exit;
}
int h = img.rows, w = img.cols; //h是行,是y轴;w是列,是x轴
Mat mask_BGR[3];
Mat RG_img; //红绿差
Mat RB_img; //红蓝差
split(img, mask_BGR);
absdiff(mask_BGR[2], mask_BGR[1], RG_img);
absdiff(mask_BGR[2], mask_BGR[0], RB_img);
double threshold1 = 15;
Mat maskT1 = (RG_img - threshold1) / ((RG_img - threshold1) + 0.001); //对应位置相除
Mat maskT2 = (RB_img - threshold1) / ((RB_img - threshold1) + 0.001);//?
Mat mask = maskT1 & maskT2;
Mat img_t = mask_BGR[2].mul(mask);
//imshow("Image View", img_t);
//char c = (char)waitKey();
//Mat maxImg = Mat::zeros(h, w, CV_64FC1);//生成和img_t同样大小的零矩阵
Mat laser_mask = Mat::zeros(h, w, CV_8UC1); // 有效激光线mask
//自适应(最小红色亮度)阈值法确定每行的最大阈值,寻找每行的最大值
int max_threshod = 200;
if (model == 1)//固定阈值寻找每行的最大值
{
int index = 0;
for (int i = 0; i < h; i++)
{
double max_row = 0;
for (int j = 0; j < w; j++)
{
if (img_t.at(i, j) > max_row)
{
max_row = img_t.at(i, j);
index = j;
}
}
/*maxImg.at(i, index) = max_row;*/
if (max_row > max_threshod)
laser_mask.at(i, index) = 1;
}
}
else if (model == 2)//自适应阈值法确定每行的最大阈值,寻找每行的最大值
{
int index = 0;
for (int i = 0; i < h; i++)
{
double max_row = 0;
int k = 0;
for (int threshod = 245; threshod > 190; threshod = threshod - 5)
{
for (int j = 0; j < w; j++)
{
if (img_t.at(i, j) > threshod || img_t.at(i, j) == threshod)
{
k++;
}
if (img_t.at(i, j) > max_row)
{
max_row = img_t.at(i, j);
index = j;
}
}
if (k > 6)
{
max_threshod = threshod;
break;
}
}
/*maxImg.at(i, index) = max_row;*/
if (max_row > max_threshod)
laser_mask.at(i, index) = 1;
}
}
else if (model == 3)//固定阈值寻找每行的最大值
{
int index = 0;
for (int i = 0; i < h; i++)
{
double max_row = 0;
for (int j = 0; j < w; j++)
{
if (img_t.at(i, j) > max_row)
{
max_row = img_t.at(i, j);
index = j;
}
}
/*maxImg.at(i, index) = max_row;*/
if (max_row > max_threshod)
laser_mask.at(i, index) = 1;
}
}
imshow("Image View", laser_mask * 255);
char c = (char)waitKey();
mask = laser_mask & laser_mask;
return mask;
}