code_#include <iostream>
#include <string>
#include<sstream>
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
void CBoardDetecion(Mat src, Mat& dst)
{
Mat mGr;
cvtColor(src, mGr, COLOR_BGR2GRAY);
equalizeHist(mGr, mGr);
//imshow("histogram equalization", mGr);
//threshold单通道灰度图像二值化(阈值处理)
Mat mTS;
threshold(mGr, mTS, 70, 255, 0);//60:板边缘阴影,分出电路板
//imshow("threshold", mTS);
//morphology operation 开闭运算去黑点白点干扰
Mat mMO;
morphologyEx(mTS, mMO, MORPH_OPEN, getStructuringElement(0, Size(3, 3)), Point(-1, -1), 1);
//morphologyEx(mMO, mMO, MORPH_OPEN, getStructuringElement(0, Size(3,3)));
//morphologyEx(mMO, mMO, MORPH_CLOSE, getStructuringElement(0, Size(5, 5)), Point(-1, -1), 1);
//morphologyEx(mMO, mMO, MORPH_OPEN, getStructuringElement(0, Size(6,6)));//Size?
//imshow("MORPHOLOGY", mMO); //3
//find contours 轮廓检测
vector<vector<Point>> contours; //conours数据类型
findContours(mMO, contours, 1, 2, Point(0, 0));//P225 method?offset?
//draw contours 绘制轮廓
for (int i = 0; i < contours.size(); i++) {
if (contourArea(contours[i]) > 300 * 190)//最小矩形面积
{
RotatedRect rectangle = minAreaRect(contours[i]);//RotatedRect类常用来存储最小外包矩形函数minAreaRect()和椭圆拟合函数fitEllipse的返回结果
Point2f points[4]; //4个点数组
rectangle.points(points); //读取最小外接矩形的4个顶点
Point2f center = rectangle.center;
for (int j = 0; j < 4; j++) {
line(dst, points[j], points[(j + 1) % 4], Scalar(0, 255, 0));
}
drawMarker(dst, center, Scalar(255, 0, 0), 2, 10);//绘制矩形中心
}
}
//焊点检测
Mat mgr;
cvtColor(src, mgr, COLOR_BGR2GRAY);
//median filter 中值滤波
Mat mFilt;
medianBlur(mgr, mFilt, 9);
//fragment using threshold 二值化
Mat mThresh;
threshold(mFilt, mThresh, 41, 255, 0);
//imshow("mThresh", mThresh);
Mat m3Thresh;
cvtColor(mThresh, m3Thresh, COLOR_GRAY2BGR);
//hough circle transform
vector<Vec3f> circles;
HoughCircles(mThresh, circles, HOUGH_GRADIENT_ALT, 0.01, 70, 255, 0.65, 5, 30);
for (size_t i = 0; i < circles.size(); i++)
{
int radius = cvRound(circles[i][2]);
Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); // the source's one
//Point centre(cvRound(circles[i][0]), cvRound(circles[i][1])); //ROIed one
//circle(mask, centre, 40, 255, -1, 8, 0);
drawMarker(dst, center, Scalar(0, 255, 0), i % 7, 5, 1, 0);
circle(dst, center, radius, Scalar(155, 50, 255), 1, 8, 0);
//drawMarker(m3Thresh, centre, Scalar(0, 255, 0), i % 7, 5, 1, 0);
//circle(m3Thresh, centre, radius, Scalar(155, 50, 255), 1, 8, 0);
}
//imshow("circles in median filter", dst1); //3
}
int main()
{
Mat src1, src2, src3, src4;
Mat dst1, dst2, dst3, dst4;
for (int i = 1; i < 5;i++)
{
Mat srci = imread("pagei.jpg", 1);
Mat dsti = srci.clone();
CBoardDetecion(srci, dsti);
imshow("resulti", dsti);
}
/*Mat src1 = imread("page2.jpg", 1);
Mat dst1 = src1.clone();
CBoardDetecion(src1, dst1);
imshow("result1", dst1);
Mat src2 = imread("page3.jpg", 1);
Mat dst2 = src2.clone();
CBoardDetecion(src2, dst2);
imshow("result2", dst2);
Mat src3 = imread("page4.jpg", 1);
Mat dst3 = src3.clone();
CBoardDetecion(src3, dst3);
imshow("result3", dst3);
Mat src4 = imread("page5.jpg", 1);
Mat dst4 = src4.clone();
CBoardDetecion(src4, dst4);
imshow("result4", dst4);*/
waitKey(0);
return 0;
}text
(2)//同心圆检测及矩形检测:
#include <iostream>
#include <string>
#include<sstream>
using namespace std;
#include "opencv2/opencv.hpp"
using namespace cv;
void FindRectangle1(Mat src, Mat& dst) {
//transform to gray
Mat mGr;
cvtColor(src, mGr, COLOR_BGR2GRAY);
//histogram equalization直方图均衡化,增强图像对比度,归一化图像亮度
equalizeHist(mGr, mGr);
//GaussianBlur(mGr, mGr, Size(5, 5), 2, 2);
imshow("gaussian blur", mGr); //2高斯滤波
//threshold单通道灰度图像二值化(阈值处理)
Mat mTS;
threshold(mGr, mTS, 80, 255, 1);//80:取在外圆和背景灰度值之前,提取矩形
//imshow("threshold", mTS);
//morphology operation 开闭运算去黑点白点干扰
Mat mMO;
morphologyEx(mTS, mMO, MORPH_OPEN, getStructuringElement(0, Size(3, 3)), Point(-1, -1), 1);
morphologyEx(mMO, mMO, MORPH_CLOSE, getStructuringElement(MORPH_RECT, Size(5, 5)));
morphologyEx(mMO, mMO, MORPH_OPEN, getStructuringElement(0, Size(5, 5)), Point(-1, -1), 1);
morphologyEx(mMO, mMO, MORPH_CLOSE, getStructuringElement(MORPH_RECT, Size(6, 6)));
//imshow("MORPHOLOGY", mMO); //3
//find contours 轮廓检测
vector<vector<Point>> contours; //conours数据类型
findContours(mMO, contours, 1, 2, Point(0, 0));//P225
//draw contours 绘制轮廓
for (int i = 0; i < contours.size(); i++) {
if (contourArea(contours[i]) > 150 * 150)//150*150矩形面积
{
RotatedRect rectangle = minAreaRect(contours[i]);//RotatedRect类常用来存储最小外包矩形函数minAreaRect()和椭圆拟合函数fitEllipse的返回结果
Point2f points[4]; //4个点数组
rectangle.points(points); //读取最小外接矩形的4个顶点
Point2f center = rectangle.center;
for (int j = 0; j < 4; j++) {
line(dst, points[j], points[(j + 1) % 4], Scalar(0, 255, 0));
}
drawMarker(dst, center, Scalar(255, 0, 0), 2, 10);//绘制标记函数
}
}
}
int main1(int argc, const char** argv)
{
Mat src = imread("page1,jpg", 1);
Mat dst = src.clone(); //use for show result
//imshow("src", src); //1
/*deal with the inner circles*/ //同心圆内圆
//get the roi
int roiStartCol = 280;
int roiEndCol = 500;
Mat imageROI; //region of interested 两列
imageROI = src(Range(0, src.rows), Range(roiStartCol, roiEndCol));
Mat mGray;
cvtColor(imageROI, mGray, COLOR_BGR2GRAY); //灰
//median filter 中值滤波
Mat mFilt;
medianBlur(mGray, mFilt, 11);
//fragment using threshold 二值化
Mat mThresh;
threshold(mFilt, mThresh, 137, 255, 0);
//imshow("after threshold", mThresh); //2
Mat mask = Mat::zeros(mThresh.size(), mThresh.type()); //use for solving outer circles 掩膜 全零矩阵
Mat m3Thresh;
cvtColor(mThresh, m3Thresh, COLOR_GRAY2BGR);
//hough circle transform
vector<Vec3f> circles;
HoughCircles(mThresh, circles, HOUGH_GRADIENT_ALT, 0.01, 66, 255, 0.5, 5, 30);
for (size_t i = 0; i < circles.size(); i++)
{
int radius = cvRound(circles[i][2]);
Point center(cvRound(circles[i][0]) + roiStartCol, cvRound(circles[i][1])); // the source's one
Point centre(cvRound(circles[i][0]), cvRound(circles[i][1])); //ROIed one
circle(mask, centre, 40, 255, -1, 8, 0);
drawMarker(dst, center, Scalar(0, 255, 0), i % 7, 5, 1, 0);
circle(dst, center, radius, Scalar(155, 50, 255), 1, 8, 0);
//drawMarker(m3Thresh, centre, Scalar(0, 255, 0), i % 7, 5, 1, 0);
//circle(m3Thresh, centre, radius, Scalar(155, 50, 255), 1, 8, 0);
}
//imshow("inner circles in median filter", m3Thresh); //3
/*deal with the outer circles*/
Mat imageROI2;
imageROI.copyTo(imageROI2, mask);
//imshow("ROI2", imageROI2); //4
//gaussian filter
Mat mGF;
GaussianBlur(imageROI2, mGF, Size(15, 15), 1);
//imshow("GF", mGF); //5
//equalize histogram
cvtColor(mGF, mGF, COLOR_BGR2GRAY);
Mat mEH = mGF.clone();
equalizeHist(mGF, mEH);
//imshow("equalize histogram", mEH); //6
Mat mHT;
adaptiveThreshold(mEH, mHT, 255, 0, 1, 25, 15); //自适应二值化 P74
//imshow("after threshold", mHT); //7
vector<Vec3f> outerCircles;
HoughCircles(mHT, outerCircles, HOUGH_GRADIENT, 1, 66, 100, 12, 20, 30);
for (size_t j = 0; j < outerCircles.size(); j++) {
int outerRadius = cvRound(outerCircles[j][2]);
Point outerCenter(cvRound(outerCircles[j][0]) + roiStartCol, cvRound(outerCircles[j][1])); // the source's one
drawMarker(dst, outerCenter, Scalar(0, 255, 0), 1, 5, 1, 0);
circle(dst, outerCenter, outerRadius, Scalar(155, 50, 255), 1, 8, 0);
}
//imshow("circles", dst); //8
/*deal with the rectangle*/
//find rectangle
FindRectangle1(src, dst);
//SHOW!
imshow("result", dst); //9
waitKey(0);
return 0;
}
X
Ximian02
@Ximian02
电路板边缘及焊点识别