生丝疵点检测

#include<opencv2/opencv.hpp>
#include<iostream>


using namespace cv;
using namespace std;

/*
滤波
二值化
分割

*/

Mat gray, img_smooth, img_thre,img_seg;
const char* window_name1 = "原始灰度图";
const char* window_name2 = "GaussianBlur";
const char* window_name3 = "Threshold";
const char* window_name4 = "Segmention";
int threshold_value = 0;//初始阈值
int const max_value = 255;//bar 的最大值
int const max_binary_value = 255;//可选输出向量

const char* trackbar_value = "Threshold Value";// 滑动条的名称

vector<vector<Point> > contours;//检测到的轮廓,每条轮廓线都以点向量的形式存储
vector<Vec4i> hierarchy;//包含有关图像拓扑的信息

static void img_Threshold(int, void*);




int main(int argc, char** argv) {
	gray = imread("E:\\研究生\\数字图像处理\\c++\\生丝瑕疵检测\\Project1\\silk.jpg",0);

	namedWindow(window_name1, WINDOW_AUTOSIZE);
	imshow(window_name1, gray);

	// 图像滤波处理 Smoothing Images
	GaussianBlur(gray, img_smooth, Size(3, 3), 0, 0);
	namedWindow(window_name2, WINDOW_AUTOSIZE);
	imshow(window_name2, img_smooth);
	
	// 图像阈值化
	namedWindow(window_name3, WINDOW_AUTOSIZE);
	//createTrackbar(trackbar_value, window_name3,&threshold_value, max_value, img_Threshold);
	//img_Threshold(0, 0);
	threshold(img_smooth, img_thre, 188, max_binary_value, THRESH_BINARY);
	imshow(window_name3, img_thre);

	//图像分割
	//二值化之后的图片存在一些小白点,
	Mat element = getStructuringElement(MORPH_RECT, Size(2, 2));
	morphologyEx(img_thre, img_seg, MORPH_OPEN, element);
	Mat element1 = getStructuringElement(MORPH_RECT, Size(5, 5));
	morphologyEx(img_seg, img_seg, MORPH_CLOSE, element1);
	int w = img_seg.cols;
	int h = img_seg.rows;
	resize(img_seg, img_seg,Size(w*2,h*2), 0, 0, INTER_LINEAR);
	findContours(img_seg, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE, Point());
	//在图上用矩形标出位置
	
	int i = 0;
	
	for (i = 0; i < contours.size(); i++) {
		Rect fRect = boundingRect(contours[i]);
		int area = fRect.height * fRect.width;
		if ( area >= 50) {
			rectangle(img_seg, Point(fRect.x, fRect.y), Point(fRect.x + fRect.width, fRect.y + fRect.height), Scalar(255), 1);	
			string text = to_string(i);
			putText(img_seg, text, Point(fRect.x, fRect.y), FONT_HERSHEY_SIMPLEX, 0.7, (255), 1);

		}
	}
	imshow(window_name4, img_seg);
	waitKey(0);
	destroyAllWindows();
	return 0;
}



//手动调节阈值的 回调函数
static void img_Threshold(int, void*){
	threshold(img_smooth, img_thre, threshold_value, max_binary_value, THRESH_BINARY);
	imshow(window_name3, img_thre);// 一定要放在这里显示,不然就寄了
	//结果显示188最好
}

 找出图像中的瑕疵点

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值