Halcon實現(xiàn)
思路:
通過中值濾波后,對圖像進(jìn)行動態(tài)閾值提取細(xì)化缺陷部分,結(jié)合開運(yùn)算,閉運(yùn)算提取缺陷。
read_image (Image, 'D:/opencv練習(xí)圖片/復(fù)雜背景提取缺陷.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實現(xiàn)
實現(xiàn)方法與思路:
原圖轉(zhuǎn)灰度圖后使用核大小201(奇數(shù))做中值濾波;
灰度圖與濾波圖像做差,閾值處理
形態(tài)學(xué)進(jìn)一步提取缺陷
輪廓查找,通過面積篩選缺陷,顯示
大林上位機(jī)機(jī)器視覺,_常州電工培訓(xùn)_常州PLC培訓(xùn)_常州機(jī)器視覺培訓(xùn)_常州上位機(jī)培訓(xùn)_常州工業(yè)機(jī)器人培訓(xùn),最適合電工及plc編程人員學(xué)習(xí)的上位機(jī)機(jī)器視覺課程 大林老師:15861139266(微信同號)
int main(int argc, char** argv)
{
Mat src = imread("D:/opencv練習(xí)圖片/復(fù)雜背景提取缺陷.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;
//形態(tài)學(xué)
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(7, 7), Point(-1, -1));
morphologyEx(binary1, src_open, MORPH_OPEN, kernel, Point(-1, -1));
imshow("開運(yùn)算", src_open);
morphologyEx(src_open, src_close, MORPH_CLOSE, kernel, Point(-1, -1));
imshow("閉運(yùn)算", src_close);
vector<vector<Point>>contours;
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("結(jié)果", src);
waitKey(0);
return 0;
}