#include <opencv2\core\core.hpp>
#include <opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Mat g_mSrcImage, g_mGrayImage, g_mDstImage;
Mat g_mShresholdImage, g_mErodeImage, g_mContourImage;
int g_nThresholdTypeValue = 0;
int g_nThresholdProgramValue = 100;
int g_nStructElementSize = 3;
int g_nKernelType = 0;
int g_nContourThresh = 80;
int g_nContourMaxThresh = 255;
RNG g_rng(12345);
vector<vector<Point>> g_vContours;
vector<Vec4i> g_vHierarchy;
void on_threshold(int, void*);
int thresholdType(int, void*);
void on_erode(int,void*);
void on_contourThresh(int, void*);
int main()
{
g_mSrcImage = imread("D:\\ImageProcess\\ImageBox\\1.bmp");
if (!g_mSrcImage.data)
{
cout << "error" << endl << endl;
return -1;
}
else
cout << "sucess" << endl << endl;
cvtColor(g_mSrcImage, g_mGrayImage, CV_BGR2GRAY);
namedWindow("程序窗口",0);
imshow("程序窗口", g_mGrayImage);
createTrackbar("模式", "程序窗口", &g_nThresholdTypeValue, 4, on_threshold);
on_threshold(g_nThresholdTypeValue, 0);
createTrackbar("参数", "程序窗口", &g_nThresholdProgramValue, 255, on_threshold);
on_threshold(g_nThresholdProgramValue, 0);
createTrackbar("内核尺寸", "程序窗口", &g_nKernelType, 2, on_erode);
on_erode(g_nKernelType, 0);
blur(g_mErodeImage, g_mErodeImage, Size(3, 3));
createTrackbar("canny阈值", "程序窗口", &g_nContourThresh, g_nContourMaxThresh, on_contourThresh);
on_contourThresh(g_nContourThresh, 0);
while (1)
{
int key;
key = waitKey(20);
if ((char)key == 27)
break;
}
}
int thresholdType(int, void*)
{
int typeValue;
if (g_nThresholdTypeValue == 0)
typeValue = THRESH_BINARY;
else if (g_nThresholdTypeValue == 1)
typeValue = THRESH_BINARY_INV;
else if (g_nThresholdTypeValue == 2)
typeValue = THRESH_TRUNC;
else if (g_nThresholdTypeValue == 3)
typeValue = THRESH_TOZERO;
else
typeValue = THRESH_TOZERO_INV;
return typeValue;
}
void on_threshold(int, void*)
{
threshold(g_mGrayImage, g_mShresholdImage, g_nThresholdProgramValue, 255, g_nThresholdTypeValue);
imshow("程序窗口", g_mShresholdImage);
}
int kernelType(int, void*)
{
int kernelTypeValue;
if (g_nKernelType == 0)
kernelTypeValue = MORPH_RECT;
else if (g_nKernelType == 1)
kernelTypeValue = MORPH_CROSS;
else
kernelTypeValue = MORPH_ELLIPSE;
return kernelTypeValue;
}
void on_erode(int,void*)
{
int kernelTypeValue;
kernelTypeValue = kernelType(g_nKernelType, 0);
Mat element = getStructuringElement(kernelTypeValue, Size(2 * g_nStructElementSize + 1, 2 * g_nStructElementSize + 1));
erode(g_mShresholdImage, g_mErodeImage, element);
imshow("程序窗口", g_mErodeImage);
}
void on_contourThresh(int, void*)
{
// Mat cannyImage;
Canny(g_mErodeImage, g_mContourImage, g_nContourThresh, g_nContourThresh * 2, 3);
findContours(g_mContourImage, g_vContours, g_vHierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
Mat drawingContourImage = Mat::zeros(g_mContourImage.size(), CV_8UC3);
for (int i = 0; i < g_vContours.size(); i++)
{
size_t count = g_vContours.size();
Mat pointsf;
Mat(g_vContours[i]).convertTo(pointsf, CV_32F);
RotatedRect box = fitEllipse(pointsf);
Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));
drawContours(drawingContourImage, g_vContours, i, color, 2, 8, g_vHierarchy);
ellipse(drawingContourImage, box, color, 1, CV_AA);
Point2f vtx[4];
box.points(vtx);
for (int j = 0; j < 4; j++)
{
line(drawingContourImage, vtx[j], vtx[(j + 1) % 4], Scalar(0, 255, 0), 1, CV_AA);
}
}
imshow("程序窗口", drawingContourImage);
}
评论2