图像练习-答题卡opencv(02)

原图 

图像练习-答题卡opencv(02)_第1张图片

 结果图像练习-答题卡opencv(02)_第2张图片
代码 
	// Load source image
	cv::Mat src = cv::imread("answer_card.jpg", cv::IMREAD_COLOR);
	if (src.empty())
	{
		return;
	}

	cv::Mat gray;
	cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);

	cv::Mat binary;
	double value = cv::threshold(gray, binary, 0, 255, cv::THRESH_BINARY_INV + cv::THRESH_OTSU);

	std::vector imgCorners{ cv::Point2f(0, 0), cv::Point2f(src.cols, 0),
									  cv::Point2f(src.cols, src.rows), cv::Point2f(0, src.rows) };

	cv::Mat dilate;
	cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(1, 7));
	cv::dilate(binary, dilate, kernel);

	std::vector> contours;
	cv::findContours(dilate, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);

	std::vector corners;
	for (size_t i = 0; i < contours.size(); i++)
	{
		cv::RotatedRect minRect = cv::minAreaRect(contours[i]);
		// rotated rectangle
		cv::Point2f sz[4];
		minRect.points(sz);
		if (cv::Rect(minRect.boundingRect()).width > src.cols / 2) // should be improved
		{
			for (int j = 0; j < 4; ++j) //找到与图像四点最近的对应四点
			{
				cv::Point2f pt = imgCorners[j];
				cv::Point2f nearest_pt = sz[0];
				float dist = cv::norm(pt - nearest_pt); //求两点的距离
				for (int k = 1; k < 4; ++k)
				{
					if (cv::norm(pt - sz[k]) < dist)
					{
						dist = cv::norm(pt - sz[k]); //更新两点距离
						nearest_pt = sz[k];
					}
				}
				corners.push_back(nearest_pt);
			}
		}
	}

	//提取涂抹的黑色小圆
	cv::Mat erode0;
	cv::erode(binary, erode0, cv::Mat(), cv::Point(-1, -1), 10); //should be improved

	cv::Mat dilat;
	cv::dilate(erode0, dilat, cv::Mat(), cv::Point(-1, -1), 5); //should be improved

	cv::Mat results(src.size(), CV_8UC3);
	cv::Mat transform = cv::getPerspectiveTransform(corners, imgCorners);
	cv::warpPerspective(src, results, transform, src.size()); // Create a Mat To Show results

	cv::Mat warp(src.size(), CV_8UC1); // should be improved
	cv::warpPerspective(dilat, warp, transform, src.size());

	//试卷答题卡选项数目
	cv::Size size(20, 5); // this variable should be changed according input
	cv::Mat resize;
	cv::resize(warp, resize, size);
	for (int i = 0; i < resize.cols; ++i)
	{
		std::string answer = "";
		answer += resize.at(1, i) == 0 ? "" : "A";
		answer += resize.at(2, i) == 0 ? "" : "B";
		answer += resize.at(3, i) == 0 ? "" : "C";
		answer += resize.at(4, i) == 0 ? "" : "D";

		if (answer.length() > 1) answer = "X"; // Double mark
		int y = 0;
		if (answer == "A") y = results.rows / size.height;
		if (answer == "B") y = results.rows / size.height * 2;
		if (answer == "C") y = results.rows / size.height * 3;
		if (answer == "D") y = results.rows / size.height * 4;
		if (answer == "") answer = "[-]";
		cv::putText(results, answer, cv::Point(50 * i + 15, 30 + y), cv::FONT_HERSHEY_PLAIN, 2, CV_RGB(0, 0, 255), 2);
	}

	imshow("results", results);
	cv::waitKey();

你可能感兴趣的:(#,【OpenCV_练习】,opencv,人工智能,计算机视觉)