生活随笔
收集整理的這篇文章主要介紹了
利用opencv霍夫变检测中国象棋的外形——圆
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天將opencv的霍夫變換修改并優化了一下,就是先預處理,切割、二值化、去噪之后進行霍夫變換,效果還不錯,從識別70多個圓,慢慢修改到識別32個圓。
效果如下:
原理毛星云寫的就很好,他的文章鏈接:https://blog.csdn.net/poem_qianmo/article/details/26977557
下面附上代碼:
#include "stdafx.h"
#include "core/core.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <set>
#include<stdio.h>#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/video.hpp>
#include <opencv2/ml/ml.hpp>
#include "opencv2/ml/ml.hpp"
#include <time.h>
#include <ctime>
#include <string>
using namespace std;
using namespace cv;
int main()
{Mat srcImage = imread(
"3.jpg"); Mat midImage, dstImage;namedWindow(
"【原始圖】",
0);imshow(
"【原始圖】", srcImage);Rect rect(
600,
0,
1205,
1035); Mat image_cut = Mat(srcImage, rect); Mat image_copy = image_cut.clone(); namedWindow(
"分割后的圖片",
0);imshow(
"分割后的圖片", image_copy);cvtColor(image_copy, midImage, CV_BGR2GRAY);threshold(midImage, midImage,
128,
255, CV_THRESH_BINARY | CV_THRESH_OTSU);blur(midImage, midImage, Size(
3,
3));
vector<Vec3f> circles;
HoughCircles(midImage, circles, CV_HOUGH_GRADIENT,
1.5,
35,
100,
25,
36,
43);
for (size_t i =
0; i < circles.size(); i++){Point center(cvRound(circles[i][
0]), cvRound(circles[i][
1]));
int radius = cvRound(circles[i][
2]);
cout <<
"圓心 " << i <<
"= " << center <<
";\n" << endl;
cout <<
"半徑= " << i <<
"= " << radius <<
";\n" << endl;circle(midImage, center,
3, Scalar(
0,
255,
0), -
1,
8,
0);circle(midImage, center, radius, Scalar(
155,
50,
255),
3,
8,
0);circle(image_copy, center,
3, Scalar(
0,
255,
0), -
1,
8,
0);circle(image_copy, center, radius, Scalar(
155,
50,
255),
3,
8,
0);}
cout <<
"共檢測到圓" << circles.size() <<
" 個圓" <<
";\n" << endl;namedWindow(
"【二值化后效果圖】",
0);imshow(
"【二值化后效果圖】", midImage);namedWindow(
"【彩色效果圖】",
0);imshow(
"【彩色效果圖】", image_copy);waitKey(
0);
return 0;
}
總結
以上是生活随笔為你收集整理的利用opencv霍夫变检测中国象棋的外形——圆的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。