opencv寻找棋盘格点位置

std::vector detect_chess_board(cv::Mat& image)
{
	//cv::Mat image, Extractcorner;
	std::vector corners;    //用来储存所有角点坐标
	cv::Size board_size = cv::Size(4, 5);   //标定板每行,每列角点数
	//image = cv::imread("2.jpg");
	//Extractcorner = image.clone();
	cv::Mat imageGray;
	cv::cvtColor(image, imageGray, CV_RGB2GRAY);
	bool patternfound = cv::findChessboardCorners(image, board_size, corners,  cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_NORMALIZE_IMAGE);//cv::CALIB_CB_FAST_CHECK +
	if (!patternfound)
	{
		std::cout << "can not find chessboard corners!" << std::endl;
		//exit(1);
	}
	else
	{
		//亚像素精确化
		cv::cornerSubPix(imageGray, corners, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
	}
	//角点检测图像显示
	std::cout << "corners:";
	for (int i = 0; i < corners.size(); i++)
	{
		cv::circle(image, corners[i], 5, cv::Scalar(255, 0, 255), 2);
		cv::putText(image,std::to_string(i+1) , corners[i], cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 0), 2, 2);
		std::cout << corners[i] << "|";
	}
	std::cout<

上面是实现对棋盘格角点检测,检测结果如下图所示。关键对于cv::findChessboardCorners(image, board_size, corners,  cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_NORMALIZE_IMAGE);//cv::CALIB_CB_FAST_CHECK 

理解,当获取棋盘格图像比较差的时候建议去掉cv::CALIB_CB_FAST_CHECK 参数,尽快速度慢点但保证检测率,特别是旋转结构的棋盘图就要去掉否则检测不出来。

opencv寻找棋盘格点位置_第1张图片

你可能感兴趣的:(opencv)