码迷,mamicode.com
首页 > 其他好文 > 详细

Opencv做mark点寻边定位

时间:2020-12-08 12:58:42      阅读:8      评论:0      收藏:0      [点我收藏+]

标签:vector   第一个   ext   isp   row   面积   min   cal   marker   

此文转载自:https://blog.csdn.net/weixin_45081640/article/details/110471722

Opencv做mark点寻边定位

通过mark点进行定位是很常见的项目,mark点就是在图上或者工件上设置一个标志性的mark点,在这里以圆点为例,如图:
技术图片

这个原图是一个很大的板子,然后四个角分别有一个黑色圆点,黑点就是mark点了,在这里,mark点的作用就是为了让摄像头在运动中通过mark点来确定板子的具体位置,然后根据mark点的位置确定整个板子的加工路径。项目比较简单,主要说一下实现方式。

第一步:检测mark点存在的问题:
要能够准确的提取到完整的mark点轮廓
图中其他与mark点相似部分的干扰(如图中卡通人物的眼睛)
环境变化导致的光线变化使阈值难以确定
第二步:设计解决问题的方案:
因为环境光具有不确定性,一旦光线发生变化,必然会影响mark点轮廓的提取,为此经过多次尝试,最终制定了两套可行方案。
1.利用图像的直方图来确定阈值,因为mark点一定是黑色,所以mark点的灰度阈值一般是图像直方图的第一个波峰,所以可以将直方图的第一个波谷作为二值化的阈值,另外,在实际测试中有时会因为背景有过黑的物体导致阈值错误,因此还需设置一个最小阈值。
2.通过梯度提取轮廓,利用sobel算子计算图像的梯度,然后对梯度图进行二值化,这样也有利于克服环境变换的影响,但是弊端在于相机与物体的距离极限要远小于直接二值化,而且强光也会导致梯度断裂,出现轮廓断续不完整。
解决mark点与图中其他相似点的方法也是通过直方图和灰度像素来确定的,已知mark点周边的像素必定是白色的所以mark点周边的像素应该是接近直方图的最后一个波峰,因为检测mark点有两种情况,一种是一定存在mark点,另一种是在机床行进中捕获mark点,先针对一定存在mark点的,如果一定存在mark点,那么就直接寻找视野内周边像素值最大的黑色圆点即可,如果是寻边捕获mark点,那就需要不断地寻找黑色圆点且周围像素值接近直方图的最后一个波峰。
第三步:确定mark点所需参数:
因为检测mark点时,相机已经固定,相机到板子的距离也就已经固定了,所以需要通过面积,样式,半径大小等方式来限制mark点,过滤掉过大或者过小的轮廓,可以提高准确率。
主要代码解析:
这部分代码主要是用来实现直方图确定二值化阈值,获取二值化图像。
int histSize = 256;
cv::MatND hist;
float range[] = { 0, 255 };
const float *ranges = { range };
int threshNum = 100;
std::vector threshNums;
//计算直方图
calcHist(&grayImg, 1, 0, cv::Mat(), hist, 1, &histSize, &ranges, true, false);
//找到第一个波谷
for (int i = 3; i < 253; i++) {
if (hist.at(i) > hist.at(i - 1) && hist.at(i) > hist.at(i + 1) && hist.at(i - 1) > hist.at(i - 2) && hist.at(i + 1) > hist.at(i + 2) && hist.at(i - 2) > hist.at(i - 3) && hist.at(i + 2) > hist.at(i + 3)) {
//std::cout << i << std::endl;
threshNums.push_back(i);
}
}
if (threshNums.size() >= 2) {
threshNum = (threshNums[0] + threshNums[1]) / 2;
}
//设置最小阈值
if (threshNum < 80) {
threshNum = 80;
}
//进行二值化
cv::threshold(grayImg, thresh1, threshNum, 255, cv::THRESH_BINARY);
如图:这是二值化后的结果,mark点非常清晰。
技术图片

这部分的代码是通过sobel算出梯度图并计算二值图像。
bool_t sobelImage(cv::Mat src, cv::Mat &sobelMat)
{
if (src.empty()) {
printf(“sobelImage(): image empty!\r\n”);
return false;
}
cv::Mat srcGray;
if (src.channels() == 3) {
cv::cvtColor(src, srcGray, cv::COLOR_BGR2GRAY);
}
else {
srcGray = src.clone();
}
// 定义边缘图,水平及垂直
cv::Mat edgeMat, edgeXMat, edgeYMat, edgeMat2;
// 求x方向Sobel边缘
cv::Sobel(srcGray, edgeXMat, CV_16S, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
// 求y方向Sobel边缘
cv::Sobel(srcGray, edgeYMat, CV_16S, 0, 1, 3, 1, 0, cv::BORDER_DEFAULT);
// 线性变换转换输入数组元素为8位无符号整型
convertScaleAbs(edgeXMat, edgeXMat);
convertScaleAbs(edgeYMat, edgeYMat);
// x与y方向边缘叠加
addWeighted(edgeXMat, 0.5, edgeYMat, 0.5, 0, edgeMat);
edgeMat2 = edgeMat.clone();

for (int i = 0; i < edgeXMat.rows; i++) {
	for (int j = 0; j < edgeXMat.cols; j++) {
		uchar v1 = edgeXMat.at<uchar>(i, j);
		uchar v2 = edgeYMat.at<uchar>(i, j);
		int value = (int)sqrt(v1 * v1 + v2 * v2);
		if (value > 255) value = 255;
		edgeMat2.at<uchar>(i, j) = value;
	}
}
cv::Mat mean_mat;
cv::meanStdDev(edgeMat2, mean_mat, cv::noArray(), cv::noArray());
cv::threshold(edgeMat2, sobelMat, 10 * mean_mat.at<double>(0), 255, 0);
cv::imshow("edgMat", sobelMat);

return true;

}
图像如图所示:mark的轮廓也很完整。
技术图片

Mark点的检测分两步,第一步需要先获取图像中mark点的参数,这个参数需要手动获取,在操作平台上用鼠标点击一个mark点,即可获得mark点的面积,半径的阈值范围。这部分代码对应代码中的getAreaAndP函数,mark点的检测对应的imgMatch函数。
如果需要测距,请参考我的相机标定计算两点距离的博客。
附上完整代码:
// imageMatch.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv.hpp>
#include <vector>
#include <ctime>
#include <string>

typedef bool bool_t;
bool_t imgMatch(bool_t isMark, cv::Mat img, double contScope[2], int findObj, double pointCrd[2], int minRadius, int maxRadius, std::vector<cv::Point>& markContours);
double aveImg(cv::Mat imgC);
bool_t getAreaAndP(cv::Mat img, cv::Point2f getPoints, int findObj, double& minArea, double& maxArea, int& minR, int& maxR);
bool_t findMarkCenter(bool_t isMark, cv::Mat img, cv::Mat grayImg, cv::Mat thresh, double contScope[2], int findObj, double pointCrd[2], int minRadius, int maxRadius, std::vector<cv::Point>& markContours, double minPix);
bool_t sobelImage(cv::Mat src, cv::Mat &sobelMat);
bool_t getAreaAndR(cv::Mat thresh, cv::Point2f getPoints, std::vector<cv::Point> &cont_1, int &flag_1);

bool_t sobelImage(cv::Mat src, cv::Mat &sobelMat)
{
	if (src.empty()) {
		printf("sobelImage(): image empty!\r\n");
		return false;
	}
	cv::Mat srcGray;
	if (src.channels() == 3) {
		cv::cvtColor(src, srcGray, cv::COLOR_BGR2GRAY);
	}
	else {
		srcGray = src.clone();
	}
	// 定义边缘图,水平及垂直
	cv::Mat edgeMat, edgeXMat, edgeYMat, edgeMat2;
	// 求x方向Sobel边缘
	cv::Sobel(srcGray, edgeXMat, CV_16S, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
	// 求y方向Sobel边缘
	cv::Sobel(srcGray, edgeYMat, CV_16S, 0, 1, 3, 1, 0, cv::BORDER_DEFAULT);
	// 线性变换转换输入数组元素为8位无符号整型
	convertScaleAbs(edgeXMat, edgeXMat);
	convertScaleAbs(edgeYMat, edgeYMat);
	// x与y方向边缘叠加
	addWeighted(edgeXMat, 0.5, edgeYMat, 0.5, 0, edgeMat);
	edgeMat2 = edgeMat.clone();

	for (int i = 0; i < edgeXMat.rows; i++) {
		for (int j = 0; j < edgeXMat.cols; j++) {
			uchar v1 = edgeXMat.at<uchar>(i, j);
			uchar v2 = edgeYMat.at<uchar>(i, j);
			int value = (int)sqrt(v1 * v1 + v2 * v2);
			if (value > 255) value = 255;
			edgeMat2.at<uchar>(i, j) = value;
		}
	}
	cv::Mat mean_mat;
	cv::meanStdDev(edgeMat2, mean_mat, cv::noArray(), cv::noArray());
	cv::threshold(edgeMat2, sobelMat, 10 * mean_mat.at<double>(0), 255, 0);
	cv::imshow("edgMat", sobelMat);

	return true;
}

bool_t getAreaAndR(cv::Mat thresh, cv::Point2f getPoints, std::vector<cv::Point> &cont_1, int &flag_1) {
	std::vector<std::vector<cv::Point>> contours_1;
	std::vector<cv::Vec4i> hierachy_1;
	
	cv::findContours(thresh, contours_1, hierachy_1, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE, cv::Point());
	//寻找点所在的轮廓
	for (int i = 0; i < contours_1.size(); i++) {
		double tmp = cv::pointPolygonTest(contours_1[i], getPoints, true);
		if (tmp >= 0) {
			cont_1 = contours_1[i];
			flag_1 = 0;
			break;
		}
	}
	return 1;
}

bool_t getAreaAndP(cv::Mat img, cv::Point2f getPoints, int findObj, double& minArea, double& maxArea, int& minR, int& maxR) {
	cv::Point2f center;
	cv::Mat FilterImg;
	cv::Mat grayImg, thresh1, thresh2;
	float radius;
	cv::bilateralFilter(img, FilterImg, 2, 40, 40);
	if (FilterImg.channels() == 3) {
		cv::cvtColor(FilterImg, grayImg, cv::COLOR_RGB2GRAY);
	}
	else {
		grayImg = FilterImg;
	}
	sobelImage(grayImg, thresh2);
	int histSize = 256;
	cv::MatND hist;
	float range[] = { 0, 255 };
	const float *ranges = { range };
	int threshNum = 100;
	std::vector<int> threshNums;
	//计算直方图
	calcHist(&grayImg, 1, 0, cv::Mat(), hist, 1, &histSize, &ranges, true, false);
	//找到第一个波谷
	for (int i = 3; i < 253; i++) {
		if (hist.at<float>(i) > hist.at<float>(i - 1) && hist.at<float>(i) > hist.at<float>(i + 1) && hist.at<float>(i - 1) > hist.at<float>(i - 2) && hist.at<float>(i + 1) > hist.at<float>(i + 2) && hist.at<float>(i - 2) > hist.at<float>(i - 3) && hist.at<float>(i + 2) > hist.at<float>(i + 3)) {
			//std::cout << i << std::endl;
			threshNums.push_back(i);
		}
	}
	if (threshNums.size() >= 2) {
		threshNum = (threshNums[0] + threshNums[1]) / 2;
	}
	//设置最小阈值
	if (threshNum < 80) {
		threshNum = 80;
	}
	//进行二值化
	cv::threshold(grayImg, thresh1, threshNum, 255, cv::THRESH_BINARY);
	cv::bitwise_not(thresh1, thresh1);

	int flag_1 = 1;
	int flag_2 = 1;

	std::vector<cv::Point> cont_1, cont_2;
	getAreaAndR(thresh1, getPoints, cont_1, flag_1);
	getAreaAndR(thresh2, getPoints, cont_2, flag_2);
	if (flag_1 || flag_2) {
		return 0;
	}
	std::vector<std::vector<cv::Point>> contours_2;
	contours_2.push_back(cont_1);
	if (findObj == 0) {
		cv::Mat image_1 = cv::Mat(img.rows, img.cols, CV_8UC3, cv::Scalar(255, 255, 255));
		//画出轮廓
		cv::drawContours(image_1, contours_2, -1, (0, 0, 255), 1);
		//灰度处理
		cv::Mat gray_1;
		if (image_1.channels() == 3) {
			cv::cvtColor(image_1, gray_1, cv::COLOR_BGR2GRAY);
		}
		else {
			gray_1 = image_1;
		}
		
		std::vector<cv::Vec3f> objects;
		//检测轮廓中的圆形
		//cv::imshow("gray", gray_1);
		cv::HoughCircles(gray_1, objects, cv::HOUGH_GRADIENT, 1, 50, 100, 20, 5, 200);
		if (objects.size() == 1) {
			minEnclosingCircle(cont_1, center, radius);
			if (radius > 300) {
				return 0;
			}
			//最小筛选面积
			minArea = 0.7 * 3.14 * radius * radius;
			//最大筛选面积
			maxArea = 1.3 * 3.14 * radius * radius;
			//最小检测圆半径
			minR = radius - 10;
			//最大检测圆半径
			maxR = radius + 10;
			// mark点周边像素
			std::cout << minArea << std::endl;
			std::cout << maxArea << std::endl;
			std::cout << minR << std::endl;
			std::cout << maxR << std::endl;
		}
		else {
			return 0;
		}
	}
	if (findObj == 1) {
		std::vector<cv::Point>approx(cont_1);
		double epsilon = cv::arcLength(cont_1, true) * 0.01;
		cv::approxPolyDP(cont_1, approx, epsilon, true);
		if (approx.size() >= 12 && approx.size() <= 16) {
			//拟合轮廓最小外接矩形
			cv::RotatedRect rect1 = cv::minAreaRect(cont_1);
			cv::Point2f P[4];
			rect1.points(P);
			//获取轮廓面积
			double resArea = cv::contourArea(cont_1);
			//最小筛选面积
			minArea = 0.8 * resArea;
			//最大筛选面积
			maxArea = 1.2 * resArea;
			float x = (P[0].x + P[2].x) / 2;
			float y = (P[0].y + P[2].y) / 2;
			center = cv::Point2f(x, y);
			double longX = P[0].x - x;
			double longY = P[0].y - y;
			//std::cout << minArea << std::endl;
			//std::cout << maxArea << std::endl;

		}
		else {
			return 0;
		}
	}
	return 1;
}

double aveImg(cv::Mat imgC) {
	double sum = 0.0, ave;
	for (int i = 0; i < imgC.rows; i++)
	{
		for (int j = 0; j < imgC.cols; j++)
		{
			sum = sum + imgC.at<bool>(i, j);
		}
	}
	ave = sum / (imgC.rows * imgC.cols);
	return ave;
}

bool_t findMarkCenter(bool_t isMark, cv::Mat img, cv::Mat grayImg, cv::Mat thresh, double contScope[2], int findObj, double pointCrd[2], int minRadius, int maxRadius, std::vector<cv::Point> &markContours, double minPix){
	std::vector<std::vector<cv::Point>> contours;
	std::vector<cv::Vec4i> hierachy;
	std::vector<std::vector<cv::Point>> contours1;
	//寻找轮廓边框
	cv::findContours(thresh, contours1, hierachy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point());
	//筛选边框,过滤掉过大或过小的边框
	for (int i = 0; i < contours1.size(); i++) {
		double contArea = cv::contourArea(contours1[i]);
		if (contArea > contScope[0] && contArea < contScope[1]) {
			//std::cout << "markArea :" << contArea << std::endl;
			contours.push_back(contours1[i]);
		}
	}
	//std::cout << "contours_size :" << contours.size() << std::endl;
	cv::Mat image = cv::Mat(img.rows, img.cols, CV_8UC3, cv::Scalar(255, 255, 255));
	//画出轮廓
	cv::drawContours(image, contours, -1, (0, 0, 255), 3);
	//灰度处理
	cv::Mat gray;
	if (image.channels() == 3) {
		cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
	}
	else {
		gray = image;
	}
	int mark = -1;
	cv::Mat imgCGray;
	if (img.channels() == 3) {
		cv::cvtColor(img, imgCGray, cv::COLOR_BGR2GRAY);
	}
	else {
		imgCGray = img;
	}
	
	std::vector<double> somePs;
	if (findObj == 0) {
		std::vector<cv::Vec3f> objects;
		//检测轮廓中的圆形
		cv::HoughCircles(gray, objects, cv::HOUGH_GRADIENT, 1, 50, 100, 20, minRadius/* 10 */, maxRadius/* 40 */);
		if (objects.size() == 0) {
			return 0;
		}
		/*for (int i = 0; i < objects.size(); i++) {
			std::cout << objects[i] << std::endl;
		}*/
		//计算检测到的圆形周边的平均像素,取周围平均像素最大的圆为mark点所在位置
		for (int i = 0; i < objects.size(); i++) {
			if (objects[i][0] <= objects[i][2] * 2 || objects[i][1] <= objects[i][2] * 2 || (image.cols - objects[i][0]) <= objects[i][2] * 2 || (image.rows - objects[i][1]) <= objects[i][2] * 2) {
				continue;
			}
			std::vector<cv::Point2f> rectCnt;
			for (int k = objects[i][0] - objects[i][2] * 2; k < objects[i][0] + objects[i][2] * 2; k++) {
				rectCnt.push_back(cv::Point2f(k, (objects[i][1] - objects[i][2] * 2)));
				rectCnt.push_back(cv::Point2f(k, (objects[i][1] + objects[i][2] * 2)));
			}
			for (int k = objects[i][1] - objects[i][2] * 2; k < objects[i][1] + objects[i][2] * 2; k++) {
				rectCnt.push_back(cv::Point2f((objects[i][0] - objects[i][2] * 2), k));
				rectCnt.push_back(cv::Point2f((objects[i][0] + objects[i][2] * 2), k));
			}
			double sumP = 0;
			for (int i = 0; i < rectCnt.size(); i++) {
				sumP = sumP + grayImg.at<bool>(rectCnt[i].y, rectCnt[i].x);
			}

			double kP = sumP / rectCnt.size();
			//std::cout << "sideP :" << kP << std::endl;
			somePs.push_back(kP);
		}
		if (isMark) {
			if (somePs.size() > 0) {
				double p1 = somePs[0];
				for (int i = 0; i < somePs.size(); i++) {
					if (somePs[i] >= p1) {
						p1 = somePs[i];
						mark = i;
					}
				}
			}
		}
		else {
			if (somePs.size() > 0) {
				for (int i = 0; i < somePs.size(); i++) {
					if (somePs[i] > minPix) {
						minPix = somePs[i];
						mark = i;
					}
				}
			}

		}

		if (mark == -1) {
			return 0;
		}
		//最终的mark点,包含圆心和半径
		pointCrd[0] = objects[mark][0];
		pointCrd[1] = objects[mark][1];
		//std::cout << "R :" << objects[mark][2] << std::endl;
		//椭圆拟合
		for (int i = 0; i < contours.size(); i++)
		{
			if (pointPolygonTest(contours[i], cv::Point2f(pointCrd[0], pointCrd[1]), true) > 0)
			{
				cv::RotatedRect box = fitEllipse(contours[i]);
				pointCrd[0] = box.center.x;
				pointCrd[1] = box.center.y;
				markContours = contours[i];
				//std::cout << box.center << std::endl;
				//std::cout << markContours << std::endl;
				break;
			}
		}
	}
	if (findObj == 1)
	{
		std::vector<std::vector<cv::Point>> approx(contours);
		std::vector<std::vector<cv::Point>> conts;
		std::vector<cv::Vec4f> objects;
		//检测图像中的十字叉
		//std::cout << contours.size() << std::endl;
		for (int i = 0; i < contours.size(); i++) {
			double epsilon = cv::arcLength(contours[i], true) * 0.01;
			cv::approxPolyDP(contours[i], approx[i], epsilon, true);
			//std::cout << approx[i].size() << std::endl;
			if (approx[i].size() >= 12 && approx[i].size() <= 16) {
				conts.push_back(contours[i]);
			}
		}
		if (conts.size() == 0) {
			return 0;
		}
		for (int i = 0; i < conts.size(); i++) {
			cv::RotatedRect rect = minAreaRect(contours[i]);
			cv::Point2f P[4];
			rect.points(P);
			float x = (P[0].x + P[2].x) / 2;
			float y = (P[0].y + P[2].y) / 2;
			objects.push_back(cv::Vec4f(x, y, abs(P[0].x - x) * 2, abs(P[0].y - y) * 2));
		}
		//计算检测到的叉周边的平均像素,取周围平均像素最大的圆为mark点所在位置
		for (int i = 0; i < objects.size(); i++) {
			if (objects[i][0] <= 50 || objects[i][1] <= 50 || (image.cols - objects[i][0]) <= 50 || (image.rows - objects[i][1]) <= 50) {
				continue;
			}
			cv::Mat imgC1, imgC2, imgC3, imgC4;
			cv::Rect rect1(objects[i][0] - objects[i][2], objects[i][1] - objects[i][3] / 2, objects[i][2] / 2, objects[i][3]);
			imgCGray(rect1).copyTo(imgC1);
			double k1 = aveImg(imgC1);
			cv::Rect rect2(objects[i][0] + objects[i][2] / 2, objects[i][1] - objects[i][3] / 2, objects[i][2] / 2, objects[i][3]);
			imgCGray(rect2).copyTo(imgC2);
			double k2 = aveImg(imgC2);
			cv::Rect rect3(objects[i][0] - objects[i][2] / 2, objects[i][1] - objects[i][3], objects[i][2], objects[i][3] / 2);
			imgCGray(rect3).copyTo(imgC3);
			double k3 = aveImg(imgC3);
			cv::Rect rect4(objects[i][0] - objects[i][2] / 2, objects[i][1] + objects[i][3] / 2, objects[i][2], objects[i][3] / 2);
			imgCGray(rect4).copyTo(imgC4);
			double k4 = aveImg(imgC4);
			double kP = (k1 + k2 + k3 + k4) / 4;
			somePs.push_back(kP);
		}
		if (isMark) {
			if (somePs.size() > 0) {
				double p1 = somePs[0];
				for (int i = 0; i < somePs.size(); i++) {
					if (somePs[i] >= p1) {
						p1 = somePs[i];
						mark = i;
					}
				}
			}
		}
		else {
			if (somePs.size() > 0) {
				for (int i = 0; i < somePs.size(); i++) {
					if (somePs[i] > minPix) {
						minPix = somePs[i];
						mark = i;
					}
				}
			}

		}
		if (mark == -1) {
			return 0;
		}
		//最终的mark点,包含中点和长宽
		pointCrd[0] = objects[mark][0];
		pointCrd[1] = objects[mark][1];
		markContours = contours[mark];
		//std::cout << pointCrd[0] << "," << pointCrd[1] << std::endl;
		//std::cout << markContours << std::endl;
	}

	return true;
}

bool_t imgMatch(bool_t isMark, cv::Mat img, double contScope[2], int findObj, double pointCrd[2], int minRadius, int maxRadius, std::vector<cv::Point> &markContours)
{
	cv::Mat FilterImg;
	cv::Mat grayImg, thresh1, thresh2;
	cv::Mat openImg;
	cv::bilateralFilter(img, FilterImg, 2, 40, 40);
	if (FilterImg.channels() == 3) {
		cv::cvtColor(FilterImg, grayImg, cv::COLOR_RGB2GRAY);
	}
	else {
		grayImg = FilterImg;
	}
	sobelImage(grayImg, thresh2);
	int histSize = 256;
	cv::MatND hist;
	float range[] = { 0, 255 };
	const float *ranges = { range };
	double pointCrd1[2], pointCrd2[2];
	int threshNum = 100;
	std::vector<int> threshNums;
	std::vector<cv::Point> markContours1, markContours2;
	//计算直方图
	calcHist(&grayImg, 1, 0, cv::Mat(), hist, 1, &histSize, &ranges, true, false);
	//找到波峰
	for (int i = 3; i < 253; i++) {
		if (hist.at<float>(i) > hist.at<float>(i - 1) && hist.at<float>(i) > hist.at<float>(i + 1) && hist.at<float>(i - 1) > hist.at<float>(i - 2) && hist.at<float>(i + 1) > hist.at<float>(i + 2) && hist.at<float>(i - 2) > hist.at<float>(i - 3) && hist.at<float>(i + 2) > hist.at<float>(i + 3)) {
			//std::cout << i << std::endl;
			threshNums.push_back(i);
		}
	}
	if (threshNums.size() >= 2) {
		threshNum = (threshNums[0] + threshNums[1]) / 2;
	}
	if (threshNum < 80) {
		threshNum = 80;
	}
	double minPix = threshNums[threshNums.size() - 1] * 0.9;
	//std::cout << "thresh :" << threshNum << std::endl;
	cv::threshold(grayImg, thresh1, threshNum, 255, cv::THRESH_BINARY);
	cv::bitwise_not(thresh1, thresh1);
	cv::imshow("thresh", thresh1);
	findMarkCenter(isMark, img, grayImg, thresh1, contScope, findObj, pointCrd1, minRadius, maxRadius, markContours1, minPix);
	findMarkCenter(isMark, img, grayImg, thresh2, contScope, findObj, pointCrd2, minRadius, maxRadius, markContours2, minPix);
	if (markContours1.size() == 0 || markContours2.size() == 0) {
		return false;
	}
	//std::cout << pointCrd1[0] << "," << pointCrd1[1] << std::endl;
	//std::cout << pointCrd2[0] << "," << pointCrd2[1] << std::endl;
	if (abs(pointCrd1[0] - pointCrd2[0]) <= 1 && abs(pointCrd1[1] - pointCrd2[1]) <= 1) {
		pointCrd[0] = (pointCrd1[0] + pointCrd2[0]) / 2;
		pointCrd[1] = (pointCrd1[1] + pointCrd2[1]) / 2;
		markContours = markContours1;
	}
	else {
		return false;
	}
	std::cout << pointCrd[0] << "," << pointCrd[1] << std::endl;
	return true;
	
}

int main()
{
	long start = clock();
	/*std::cout << time1 << std::endl;*/
	//std::vector<cv::Mat> images;
	//images.push_back(cv::imread("E:\\python\\opencv\\002\\10208.bmp"));

	cv::Mat img = cv::imread("E:\\python\\opencv\\002\\10231.bmp");
	//轮廓面积的最大值和最小值
	double contScope[2] = { 500, 5000 };
	//mark点周边平均像素的最小阈值
	double minPix = 135;
	//选择要检测的形状
	int findObj = 0;
	double pointCrd[2];
	int minRadius = 10;
	int maxRadius = 30;
	bool_t isMark = true;
	std::vector<cv::Point> markContours;
	imgMatch(isMark, img, contScope, findObj, pointCrd, minRadius, maxRadius, markContours);
	/*long end = clock();*/
	//std::cout << end - start << std::endl;
	cv::Point2f Points;
	Points = cv::Point2f(1515, 806);
	int minR, maxR;
	double minArea, maxArea, resP;
	getAreaAndP(img, Points, findObj, minArea, maxArea, minR, maxR);

	cv::imshow("img", img);
	cv::waitKey(0);
}

Opencv做mark点寻边定位

标签:vector   第一个   ext   isp   row   面积   min   cal   marker   

原文地址:https://www.cnblogs.com/phyger/p/14084477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!