Halcon实现
思路:
通过中值滤波后,对图像进行动态阈值提取细化缺陷部分,结合开运算,闭运算提取缺陷。
read_image (Image, 'D:/opencv练习图片/复杂背景提取缺陷.jpg')
dev_set_line_width (3)
threshold (Image, Region, 30, 255)
reduce_domain (Image, Region, ImageReduced)
mean_image (ImageReduced, ImageMean, 150, 150)
dyn_threshold (ImageReduced, ImageMean, SmallRaw, 37, 'dark')
opening_circle (SmallRaw, RegionOpening,4.5)
closing_circle (RegionOpening, RegionClosing, 7)
connection (RegionClosing, ConnectedRegions)
dev_set_color ('red')
dev_display (Image)
dev_set_draw ('margin')
dev_display (ConnectedRegions)
Opencv实现
实现方法与思路:
原图转灰度图后使用核大小201(奇数)做中值滤波;
灰度图与滤波图像做差,阈值处理
形态学进一步提取缺陷
轮廓查找,通过面积筛选缺陷,显示
大林上位机机器视觉_苏州电工培训_苏州PLC培训_苏州上位机培训_苏州机器视觉培训_苏州工业机器人培训_最适合电工及plc编程人员学习的上位机机器视觉课程 大林老师:15861139266(微信同号)
int main(int argc, char** argv)
{
Mat src = imread("D:/opencv练习图片/复杂背景提取缺陷.jpg");
imshow("输入图像", src);
Mat gray, gray_mean,dst,binary1, binary2, binary;
cvtColor(src, gray, COLOR_BGR2GRAY);
medianBlur(gray, gray_mean, 201);
imshow("中值滤波", gray_mean);
addWeighted(gray, -1, gray_mean, 1, 0, dst);
imshow("做差", dst);
//阈值提取
threshold(dst, binary1, 10, 255, THRESH_BINARY|THRESH_OTSU);
imshow("二值化", binary1);
Mat src_open, src_close;
//形态学
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(7, 7), Point(-1, -1));
morphologyEx(binary1, src_open, MORPH_OPEN, kernel, Point(-1, -1));
imshow("开运算", src_open);
morphologyEx(src_open, src_close, MORPH_CLOSE, kernel, Point(-1, -1));
imshow("闭运算", src_close);
vector<vector
findContours(src_close, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE, Point());
for (int i = 0; i < contours.size(); i++)
{
float area = contourArea(contours[i]);
cout << area << endl;
if (area > 1000)
{
drawContours(src, contours, i, Scalar(0, 0, 255), 2, 8);
}
}
imshow("结果", src);
waitKey(0);
return 0;
}
版权所有:江苏和讯自动化设备有限公司所有 备案号:苏ICP备2022010314号-1
技术支持: 易动力网络