基本是最终版本的小工程

前言

昨天角度定位的问题解决了……角度还是算不出来,我调整了几个函数,在图像旋转之后再次使用Rotate()函数得到外接矩形的点阵信息,然后标记ROI图形区域裁剪图像并用imwrite()函数读出。

基本是最终版本的测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <string>
#include <cmath>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;

float angle;
Point2f rect[4];
void minRect(Mat& srcImage);
void Rotate(Mat &src,Mat &dst,float angle);
void drawLine(Mat& img);
void cutImage(Mat& img);
void saveImage(Mat& img,int i);
void makeBorder(Mat& src,Mat& dst,int B,int G,int R);
static inline bool ContoursSortFunction(vector<Point> contour1,vector<Point> contour2);
int main(){
string path = "/Users/cezarbao/Desktop/TestImages/*.bmp";
vector<Mat>images;
vector<String>srcImages;
glob(path,srcImages,false);
size_t cnt = srcImages.size();
for(int i = 0; i < cnt; i++)
{
images.push_back(imread(srcImages[i]));
makeBorder(images[i],images[i],255,255,255);
minRect(images[i]);
Rotate(images[i],images[i],angle);
minRect(images[i]);
//drawLine(images[i]);
cutImage(images[i]);
saveImage(images[i],i);
imshow("image",images[i]);
waitKey(0);
}
}
void minRect(Mat& srcImg){
Mat dstImage = srcImg.clone();
cvtColor(dstImage, dstImage, COLOR_BGR2GRAY);
threshold(dstImage, dstImage, 254, 255, THRESH_BINARY);
//imshow("srcImage", dstImage);
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(dstImage, contours, hierarcy, RETR_LIST, CHAIN_APPROX_NONE);
sort(contours.begin(),contours.end(),ContoursSortFunction);
contours.erase(contours.begin());
vector<RotatedRect> box(contours.size());
for(int i = 0; i < 1; i++)
{
box[i] = minAreaRect(Mat(contours[i]));
box[i].points(rect);
angle = box[i].angle;
//for(int j = 0; j < 4; j++)
//{
// cout << rect[j] << endl;
//}
}
}
static inline bool ContoursSortFunction(vector<Point> contour1,vector<Point> contour2){
return (contourArea(contour1) > contourArea(contour2));
}
void Rotate(Mat &src,Mat &dst,float angle){
makeBorder(src,dst,0,0,0);
//rotate
Point2f center( (float)(dst.cols/2) , (float) (dst.rows/2));
Mat affine_matrix = getRotationMatrix2D( center, angle, 1.0 );
warpAffine(dst, dst, affine_matrix, dst.size());
}
void drawLine(Mat& img){
for(int i=0; i<4; i++)
{
line(img, rect[i], rect[(i+1)%4], Scalar(0, 0, 255),2,LINE_AA);
}
}
void cutImage(Mat& img){
int width = rect[2].x - rect[1].x;
int height = rect[0].y - rect[1].y;
Mat ROI = img(Rect(rect[1].x,rect[1].y,width,height));
ROI.copyTo(img);
}
void saveImage(Mat& img,int i){
char path[256] = {0};
sprintf(path,"./outputImages/%d.jpg",i);
imwrite(path,img);
}
void makeBorder(Mat& src,Mat& dst,int B,int G,int R){
int maxBorder =(int) (max(src.cols, src.rows)* 1.414);
int dx = (maxBorder - src.cols)/2;
int dy = (maxBorder - src.rows)/2;
copyMakeBorder(src, dst, dy, dy, dx, dx, BORDER_CONSTANT,Scalar(B,G,R));
}

后记

测试图没啥好看的,老师发的零件图片,效果图也不贴了(不是我懒)
并无新函数,但就这玩意儿我写了三天……后面算是整理的比较清楚了,功能基本上都写在函数里了,就酱。
开始学习Pytorch。