#include "stdafx.h"
#include "ImgFilters.h"
#include<opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;ios
ImgFilters::ImgFilters()
{
}函數
ImgFilters::~ImgFilters()
{
}spa
//圖片倒轉
void ImgFilters::filp() {
const char* filename = "G:\\b.png";
Mat src = imread(filename);
int flipCode = atoi(filename);
if (src.empty()) {
throw("Faild open file");
}排序
Mat dst;
flip(src, dst, flipCode);圖片
imshow("src", src);
//顯示原圖像
imshow("dst", dst);
//顯示翻轉後的圖像ip
waitKey();
}ci
//圖片倒轉
void ImgFilters::myResize() {
const char *filename = "G:\\b.png";
Mat src, dst;
//定義圖像的大小,寬度縮小80%
float scaleW = 0.8;
//定義圖像的大小,高度縮小80%
float scaleH = scaleW;element
//去色讀取圖片
src = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
if (src.empty()) {
throw("Faild open file.");
}
int width = static_cast<float>(src.cols*scaleW);
//定義想要擴大或者縮小後的寬度,src.cols爲原圖像的寬度,乘以80%則獲得想要的大小,並強制轉換成float型
int height = static_cast<float>(src.rows*scaleH);
//定義想要擴大或者縮小後的高度,src.cols爲原圖像的高度,乘以80%則獲得想要的大小,並強制轉換成float型get
resize(src, dst, Size(width, height));
//從新定義大小的函數string
imshow("src", src);
imshow("dst", dst);
waitKey();
}
void ImgFilters::myRotate() {
Mat src, dst;
float angle = 55.5;
//定義翻轉的角度爲55.5度
const char* filename = "G:\\b.png";
src = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
if (src.empty()) {
throw("Faild open file.");
}
cv::Point2f center = cv::Point2f(static_cast<float>(src.cols / 2),
static_cast<float>(src.rows / 2));
//定義中心點的座標,2f的意思爲float型,中心點極爲X,Y即寬度與高度的二分之一
cv::Mat affineTrans = getRotationMatrix2D(center, angle, 1.0);
//getRotationMatrix2D爲圖像旋轉的函數,第一個參數center是旋轉中心點,第二個參數angle爲旋轉角度
//第三個1.0的參數爲double scale:圖像縮放因子
cv::warpAffine(src, dst, affineTrans, src.size(), cv::INTER_CUBIC, cv::BORDER_REPLICATE);
//仿射變換函數warpAffine
//第一個參數src爲輸入原圖像
//第二個參數dst爲輸出圖像
//第三個參數爲affineTrans轉換矩陣
//第四個參數爲輸出圖像的size大小
//第五個參數爲立方差值法
//第六個參數爲邊界用上下行或者左右列來複制填充
cv::imshow("src", src);
cv::imshow("dst", dst);
cv::imwrite("G:\\RotateDst.jpg", dst);
//輸出圖像
cv::waitKey();
}
void ImgFilters::circularity()
{
Mat imageSource, image;
imageSource = imread("G:\\b.png");
cvtColor(imageSource, image, CV_BGR2GRAY);
//高斯濾波
GaussianBlur(image, image, Size(9, 9), 2, 2);
vector<Vec3f> circles;
//霍夫圓
HoughCircles(image, circles, CV_HOUGH_GRADIENT, 1.5, image.rows / 8, 200, 100, 0, 0);
cout << "circles.size():"<<circles.size() << endl;
for (size_t i = 0; i < circles.size(); i++)
{
cout << "圓心" <<i<<":"<< circles[i][0]<<","<<cvRound(circles[i][1]) << endl;
cv::Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
//繪製圓心
cv::circle(imageSource, center, 3, Scalar(0, 255, 0), -1, 8, 0);
//繪製圓輪廓
cv::circle(imageSource, center, radius, Scalar(155, 50, 255), 3, 8, 0);
}
imshow("Point of Contours", imageSource); //向量contours內保存的全部輪廓點集
waitKey();
}
void ImgFilters::imageCrop()
{
Mat image = imread("G:\\b.png");
if (image.empty()) {
return;
}
Rect rect1(0, 0, 400, 400);
Mat roi1;
image(rect1).copyTo(roi1); // copy the region rect1 from the image to roi1
imshow("裁剪窗口", roi1);
waitKey(0);
}
//////////////////////////答題卡識別小例子////////////////////////////
class RectComp //Rect排序
{
Rect rm;
public:
RectComp(Rect rms)
{
rm = rms;
}
bool operator< (const RectComp& ti) const
{
return rm.x < ti.rm.x;
}
Rect getRect() {
return rm;
}
};
void ImgFilters::exmple() {
//裝載圖片
Mat sourceImg = imread("G:\\aaa.png");
Mat grayImg;
//圖片變成灰度圖片
cvtColor(sourceImg, grayImg, CV_BGR2GRAY);
Mat thresholdImg;
//圖片二值化
threshold(grayImg, thresholdImg, 200, 255, THRESH_BINARY_INV);
Mat erodeImg;
//肯定腐蝕和膨脹核的大小
Mat element = getStructuringElement(MORPH_RECT, Size(4, 4));
//腐蝕操做
erode(thresholdImg, erodeImg, element);
//膨脹操做
Mat dilateImg;
dilate(erodeImg, dilateImg, element);
//肯定每張答題卡的ROI區域
Mat imag_ch1;
dilateImg(Rect(5, 30, 95, 60)).copyTo(imag_ch1);
imshow("img1", imag_ch1);
//提取已經塗好了的選項
std::vector<std::vector<cv::Point> > chapter1;
findContours(imag_ch1, chapter1, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
Mat result(imag_ch1.size(), CV_8U, cv::Scalar(255));
cv::drawContours(result, chapter1, -1, cv::Scalar(0), 2);
namedWindow("resultImage", 1);
cv::imshow("resultImage", result);
vector<RectComp>RectCompList;
for (int i = 0; i<chapter1.size(); i++)
{
Rect rm = cv::boundingRect(cv::Mat(chapter1[i]));
cout << "長方形" << i << ":" << "面積:" << rm.area() << endl;
RectComp *ti = new RectComp(rm);
RectCompList.push_back(*ti);
// printf("Rect %d x = %d,y = %d \n",i,rm.x,rm.y);
}
sort(RectCompList.begin(), RectCompList.end());
map<int, string>listenAnswer;
//判斷這部分的答題卡是否都已塗上
for (int t = 0; t<RectCompList.size(); t++)
{
if (RectCompList.at(t).getRect().y<32)
{
listenAnswer[t] = "A";
}
else if ((RectCompList.at(t).getRect().y>32) && (RectCompList.at(t).getRect().y<48))
{
listenAnswer[t] = "B";
}
else if (RectCompList.at(t).getRect().y>48)
{
listenAnswer[t] = "C";
}
printf("sorted %d x = %d,y = %d \n", t, RectCompList.at(t).getRect().x, RectCompList.at(t).getRect().y);
}
for (map<int, string>::iterator it = listenAnswer.begin(); it != listenAnswer.end(); ++it)
{
cout << "num:" << it->first + 1 << "," << "answer:" << it->second << endl;
}
waitKey(); }