#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "windows.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
#pragma warning(disable:4996)
static Mat src, gray_src,drawImg;
static Mat cutSrc;
//src.copyTo(drawImg);
static int threshold_v = 80;
static int threshold_max = 100;
static char * title = "rectAndRound";
static RNG rng(12345);
int nHeight; //图像宽
int nWidth; //图像高
int nBufferDist = 100;//图像中心矩形宽和高
Point photoCenterPoint;//图像中心点坐标
Rect photoCenterRect; //图像中心矩形
string itos(int i) // 将int 转换成string
{
stringstream s;
s << i;
return s.str();
}
static void m_contours(int, void *);
Mat resizeImag(cv::Mat imgSrc,float scale)
{
int nHeight = imgSrc.rows*scale;
int nWidth = imgSrc.cols*scale;
Size ResImgSize = Size(nWidth, nHeight);
Mat ResImg = Mat(ResImgSize, imgSrc.type());
resize(imgSrc, ResImg, ResImgSize, CV_INTER_CUBIC);
return ResImg;
}
void cutGrayImage(cv::Mat imgSrc, Rect boundRect,string str)
{
cv::Mat image = imgSrc;
//图像高宽
int nHeight = image.rows;
int nWidth = image.cols;
Mat TargImg;
int bufDis = 3;
int srcRectL = boundRect.x - bufDis;
int srcRectT = boundRect.y - bufDis;
int srcRectR = boundRect.x + boundRect.width + bufDis;
int srcRectB = boundRect.y + boundRect.height + bufDis;
//保证处理不越图像边界
if (srcRectL>0&& srcRectT>0&& srcRectR(cv::Point(bw, bh)) = imgSrc.at(cv::Point(m, n));
}
}
imwrite(str, TargImg);
TargImg.release();
}
}
cv::Mat filterSourceImage(cv::Mat imgSrc)
{
cv::Mat image = imgSrc;
//图像高宽
int nHeight = image.rows;
int nWidth = image.cols;
//创建灰度图像,初始化
Mat TargImg;
TargImg.create(nHeight, nWidth, CV_8UC1);
TargImg = Scalar::all(0);
int threRelativeVal = 20; //较差阈值
int threWhiteVal = 80; //像素绝对值阈值
int threBlackVal = 80; //像素绝对值阈值
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
int b = imgSrc.at(cv::Point(i, j))[0];
int g = imgSrc.at(cv::Point(i, j))[1];
int r = imgSrc.at(cv::Point(i, j))[2];
//目标找到,开始遍历;双判断条件,一是像素rgb值差值、二是像素绝对值
if (abs(b - r) < threRelativeVal&&abs(b - g) < threRelativeVal&&abs(g - b) < threRelativeVal&&b < threBlackVal
&&r < threBlackVal&&g < threBlackVal)
{
TargImg.at(cv::Point(i, j)) = 255;
}
}
}
return TargImg;
}
int index = 0;
void main(int argc, char** argv)
{
Mat rawImg;
rawImg = imread(("000005.jpg"));
//imshow("src31", src);
src=resizeImag(rawImg,0.15);
nHeight = src.rows;
nWidth = src.cols;
//图像中心点坐标
photoCenterPoint.x = (int)(nWidth / 2.0);
photoCenterPoint.y = (int)(nHeight / 2.0);
//图像中心左上角点坐标
photoCenterRect.x = photoCenterPoint.x - (int)(nBufferDist / 2.0);
photoCenterRect.y = photoCenterPoint.y - (int)(nBufferDist / 2.0);
photoCenterRect.width = nBufferDist;
photoCenterRect.height = nBufferDist;
//Mat gray_tar;
gray_src.create(nHeight, nWidth, CV_8UC1);
gray_src = Scalar::all(0);
//imshow("原始_src", src);
gray_src = filterSourceImage(src);
gray_src.copyTo(cutSrc);
//imshow("gray_src", gray_src);
imwrite("gray.jpg", cutSrc);
cutGrayImage(cutSrc, photoCenterRect, "ceshi.jpg");
createTrackbar("rect round:", title, &threshold_v, threshold_max, m_contours);
m_contours(0, 0);
waitKey(0);
}
//bool GreaterSort(vector a, vector b)
//{
// return (a.size() > b.size());
//}
void m_contours(int, void *)
{
//Mat binary_output;
vector> raw_contours;
vector> contours;
vector hierachy;
findContours(gray_src, raw_contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(-1, -1));//发现轮廓
imshow("m_contours", gray_src);
//对轮廓点进行过滤
for (int i = 0; i < raw_contours.size();i++)
{
if (raw_contours[i].size()>380 && raw_contours[i].size() < 590)
{
contours.push_back(raw_contours[i]);
cout<< index++<<":" << raw_contours[i].size()<30 && raw_contours[i].size() < 90)
{
contours.push_back(raw_contours[i]);
cout << index++ << ":" << raw_contours[i].size() << endl;;
}
}
src.copyTo(drawImg);
vector boundRect(contours.size());
vector> contours_ploy(contours.size());//减少点数后的轮廓的集合
for (size_t i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contours_ploy[i], 3, true);//减少轮廓点数,为后面的算法提高效率
{
boundRect[i] = boundingRect(contours_ploy[i]);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
rectangle(drawImg, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
string str;
str = itos(i);
str = "./subImg/" + str + ".jpg";
cutGrayImage(cutSrc, boundRect[i], str);
}
}
// draw it
//Point2f pts[4];
//画出仪表中心矩形
//rectangle(drawImg, photoCenterRect, color, 2, 8);//画矩形
//cutGrayImage(gray_src, photoCenterRect,"single.jpg");
//对目标链表进行排序,过滤多余链表
//sort(contours.begin(), contours.end(), GreaterSort);
//cutGrayImage(gray_src,boundRect);
//将检测到圆心和指针画到图上
//color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
//cv::drawContours(drawImg, refine_contours2, -1, color, 1);
imshow("", drawImg);
}