matlab提取线条,请问如何将图片中的红色激光线条给提取出来啊,有没有大佬救救孩子,贴出代码给瞅瞅啊!谢谢!...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

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;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值