OpenCV缺陷检测

新手上路,欢迎高手指点。
原图:

OpenCV缺陷检测代码:

// An highlighted block
#include "head.h"

int main() {
	Mat img = imread("image/1204.bmp");
	//assert(im.data);
	if (!img.data) { cout << "cuowu!" << endl; return -1; }
	Mat img_gray; Mat img2;
	cvtColor(img,img_gray, COLOR_BGR2GRAY);
	//二值化
	threshold(img_gray, img2,100, 255, THRESH_BINARY_INV);
	
	//形态学处理 闭操作  先膨胀后腐蚀
	Mat element = getStructuringElement(MORPH_RECT,Size(17, 17));
	morphologyEx(img2, img2, MORPH_CLOSE, element);
	
	//腐蚀
	Mat element1 = getStructuringElement(MORPH_RECT, Size(3, 1));
	morphologyEx(img2, img2, MORPH_ERODE, element1);
	
	vector<vector<Point> > contours;
	findContours(img2, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);
	drawContours(img, contours, -1, Scalar(0, 255, 0), 1, 8);
	vector<vector<Point> > contours1;
	for (int i=0; i <= contours.size() - 1; i++) {
		float area = contourArea(contours[i]);
		if ((area < 5500&&area>5000)||area>25000) {
			contours1.push_back(contours[i]);
		}
	}
	string str[2] = { "D","S" };
	drawContours(img, contours1, -1, Scalar(0, 0, 255), 1, 8);
	
	for (int i = 0; i <= contours1.size() - 1; i++) {
		Rect rect = boundingRect(contours1[i]);
		rectangle(img, rect, Scalar(0, 0, 255), 1, 8);
		putText(img,str[i] , Point(rect.x -20+ rect.width / 2, rect.y - 30), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2);
	}
	namedWindow("1");
	imshow("1", img);
	namedWindow("2");
	imshow("2", img_gray);
	namedWindow("3");
	imshow("3", img2);
	waitKey(0);
	return 0;
}

结果:

你可能感兴趣的:(OpenCV图像处理)