├── .gitignore ├── .vscode ├── launch.json ├── settings.json └── tasks.json ├── build ├── cornerDetection.cpp ├── createocr.cpp ├── dehaze.cpp ├── denoise.cpp ├── gaussDiff.cpp ├── image ├── drink.jpg ├── gf.jpg ├── hazedImage │ ├── 1.jpeg │ ├── 10.png │ ├── 11.png │ ├── 4.jpeg │ ├── 5.jpg │ ├── 6.jpg │ └── 9.jpg ├── lena.jpg ├── numberImage │ ├── 0.jpg │ ├── 1.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── 4.jpg │ ├── 5.jpg │ ├── 6.jpg │ ├── 7.jpg │ ├── 8.jpg │ └── 9.jpg ├── ocr.gif ├── ocr.jpg ├── produceNumber.png ├── retification │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.jpg │ └── 5.jpg ├── screenshoot.png └── timg.jpeg ├── kernel.cpp ├── number.dat ├── paintFromPicture.cpp ├── readImage.cpp ├── recorrection.cpp ├── run ├── updatedehaze.cpp └── verfiyCode.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.out 2 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "(gdb) Launch", 6 | "type": "cppdbg", 7 | "request": "launch", 8 | "program": "${workspaceRoot}/a.out", 9 | "args": [], 10 | "stopAtEntry": false, 11 | "cwd": "${workspaceRoot}", 12 | "environment": [], 13 | "externalConsole": true, 14 | "MIMode": "gdb", 15 | "setupCommands": [ 16 | { 17 | "description": "Enable pretty-printing for gdb", 18 | "text": "-enable-pretty-printing", 19 | "ignoreFailures": true 20 | } 21 | ] 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "array": "cpp", 5 | "*.tcc": "cpp", 6 | "cctype": "cpp", 7 | "clocale": "cpp", 8 | "cmath": "cpp", 9 | "complex": "cpp", 10 | "cstddef": "cpp", 11 | "cstdint": "cpp", 12 | "cstdio": "cpp", 13 | "cstdlib": "cpp", 14 | "cstring": "cpp", 15 | "ctime": "cpp", 16 | "cwchar": "cpp", 17 | "cwctype": "cpp", 18 | "exception": "cpp", 19 | "fstream": "cpp", 20 | "functional": "cpp", 21 | "initializer_list": "cpp", 22 | "iomanip": "cpp", 23 | "iosfwd": "cpp", 24 | "istream": "cpp", 25 | "limits": "cpp", 26 | "new": "cpp", 27 | "ostream": "cpp", 28 | "sstream": "cpp", 29 | "stdexcept": "cpp", 30 | "streambuf": "cpp", 31 | "system_error": "cpp", 32 | "tuple": "cpp", 33 | "type_traits": "cpp", 34 | "typeinfo": "cpp", 35 | "utility": "cpp" 36 | } 37 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "0.1.0", 5 | "command": "g++", 6 | "isShellCommand": true, 7 | "args": ["-g","${workspaceRoot}/${fileBasename}"], 8 | "showOutput": "always" 9 | //g++ -g main.cpp `pkg-config --cflags --libs opencv` 10 | } -------------------------------------------------------------------------------- /build: -------------------------------------------------------------------------------- 1 | sudo g++ -g $1 `pkg-config --cflags --libs opencv` 2 | -------------------------------------------------------------------------------- /cornerDetection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main(int argc,char**argv) 8 | { 9 | 10 | Mat src = imread("image/lena.jpg"); 11 | Mat gray = Mat::zeros(src.size(),CV_8UC1); 12 | cvtColor(src,gray,CV_BGR2GRAY); 13 | imshow("gray image",gray); 14 | Mat dst; 15 | cornerHarris(gray,dst,2,3,0.01); 16 | threshold(dst,dst,0.00001,255,THRESH_BINARY); 17 | imshow("dst image",dst); 18 | while(waitKey()!='q'); 19 | return 0; 20 | 21 | } -------------------------------------------------------------------------------- /createocr.cpp: -------------------------------------------------------------------------------- 1 | //创建验证码图片 2 | #include 3 | #include"opencv2/opencv.hpp" 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | const int width = 600; 9 | const int height = 600; 10 | 11 | int main(int argc,char**argv) 12 | { 13 | RNG rng; 14 | Mat ocr = Mat::zeros(Size(height,width),CV_8UC3); 15 | bitwise_not(ocr,ocr); 16 | 17 | 18 | putText(ocr,"ABCD",Point(30,300),CV_FONT_HERSHEY_COMPLEX,3.0,Scalar(230,123,89),8,LINE_8); 19 | 20 | vector points; 21 | 22 | for(int i = 0;i<30;i++) 23 | { 24 | Point point; 25 | point.x = rng.uniform(0,height); 26 | point.y = rng.uniform(0,width); 27 | circle(ocr,point,2,Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)),FILLED,LINE_8); 28 | } 29 | 30 | for(int i = 0;i<20;i++) 31 | { 32 | line(ocr,Point(rng.uniform(0,height),rng.uniform(0,height)),Point(rng.uniform(0,height),rng.uniform(0,height)),Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)),1,LINE_8); 33 | } 34 | 35 | imshow("ocr",ocr); 36 | imwrite("image/ocr.jpg",ocr); 37 | 38 | 39 | while(waitKey()!='q'){} 40 | return 0; 41 | } -------------------------------------------------------------------------------- /dehaze.cpp: -------------------------------------------------------------------------------- 1 | //导向滤波算法实现图片去雾 2 | //参考网站 https://blog.csdn.net/s12244315/article/details/50292049 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace cv; 8 | 9 | const double omega = 0.9; 10 | const int windowsize = 9; 11 | const int radius = 4; 12 | const double t0 = 0.1; 13 | 14 | void minFilter(Mat&,Mat&); 15 | 16 | double max(double,double); 17 | 18 | int main(int argc,char**argv) 19 | { 20 | double startTime=(double)getTickCount(); 21 | Mat srcImage = imread("image/hazedImage/5.jpg"); 22 | resize(srcImage,srcImage,Size(640,480)); 23 | 24 | Mat channels[3]; 25 | split(srcImage,channels); 26 | //先创建最小化的channel 27 | Mat minRGB = Mat::zeros(srcImage.size(),CV_8UC1); 28 | 29 | int rows = srcImage.rows; 30 | int cols = srcImage.cols; 31 | 32 | //1.求暗通道 33 | for(int i = 0;i(i,j); 38 | for(int c= 1;c<3;c++) 39 | minValue = minValue > channels[0].at(i,j) ? channels[0].at(i,j) : minValue; 40 | minRGB.at(i,j) = minValue; 41 | } 42 | 43 | } 44 | 45 | //imshow("min BGR",minRGB); 46 | 47 | //求全球大气光值A 48 | const double A = 255;//假定A是255 49 | 50 | Mat minFilterImage; 51 | minFilter(minRGB,minFilterImage); 52 | 53 | //imshow("minFilter",minFilterImage); 54 | waitKey(); 55 | 56 | //求预估的投射图T(x) 57 | Mat T = Mat::zeros(minFilterImage.size(),CV_8UC1); 58 | for(int i = 0;i(i,j) = saturate_cast(A - omega*minFilterImage.at(i,j)); 63 | } 64 | } 65 | 66 | //imshow("预估投射图",T); 67 | 68 | Mat srcROI = srcImage(Rect(radius,radius,T.cols,T.rows)); 69 | 70 | Mat finalImage = Mat::zeros(srcROI.size(),srcROI.type()); 71 | 72 | for(int i = 0;i(i,j),255*t0); 77 | 78 | for(int c = 0;c(i,j)[c] = saturate_cast(double(srcROI.at(i,j)[c] - A)/tempT +A); 81 | } 82 | 83 | } 84 | } 85 | 86 | // resize(srcROI,srcROI,Size(700,800)); 87 | // resize(finalImage,finalImage,Size(700,800)); 88 | imshow("srcImage",srcROI); 89 | imshow("final image",finalImage); 90 | 91 | double time =double((getTickCount()-startTime)/getTickFrequency()); 92 | cout<(x,y);//先得到中点 118 | 119 | for(int a = 0;asrc.at(x-a,y-b)) minValue=src.at(x-a,y-b); 124 | if(minValue>src.at(x+a,y-b)) minValue=src.at(x+a,y-b); 125 | if(minValue>src.at(x-a,y+b)) minValue=src.at(x-a,y+b); 126 | if(minValue>src.at(x+a,y+b)) minValue=src.at(x+a,y+b); 127 | } 128 | } 129 | minImage.at(i,j) = minValue; 130 | } 131 | } 132 | dst = minImage; 133 | } 134 | 135 | //返回最大值 并且归一化 136 | double max(double a,double b) 137 | { 138 | return a>b?a/255:b/255; 139 | } -------------------------------------------------------------------------------- /denoise.cpp: -------------------------------------------------------------------------------- 1 | //利用开操作去除 线和噪音点 2 | #include 3 | #include "opencv2/opencv.hpp" 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | int main() 9 | { 10 | Mat ocr = imread("image/ocr.jpg"); 11 | if(!ocr.data) 12 | { 13 | cout<<"cannot open the image"; 14 | return -1; 15 | } 16 | imshow("src Image",ocr); 17 | medianBlur(ocr,ocr,9); 18 | Mat grayImage; 19 | cvtColor(ocr,grayImage,CV_BGR2GRAY); 20 | 21 | Mat binaryImage = grayImage.clone(); 22 | adaptiveThreshold(grayImage,binaryImage,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY_INV,15,-2); 23 | //imshow("binary",binaryImage); 24 | 25 | Mat kernel = getStructuringElement(MORPH_RECT,Size(5,5),Point(-1,-1)); 26 | 27 | Mat dstImage; 28 | morphologyEx(binaryImage,dstImage,CV_MOP_OPEN,kernel); 29 | 30 | bitwise_not(dstImage,dstImage); 31 | 32 | imshow("effort Image",dstImage); 33 | 34 | 35 | while(waitKey()!='q'){} 36 | return 0; 37 | 38 | } -------------------------------------------------------------------------------- /gaussDiff.cpp: -------------------------------------------------------------------------------- 1 | //高斯不同 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | int main(int argc,char**argv) 9 | { 10 | Mat srcImage = imread("image/lena.jpg"); 11 | Mat grayImage; 12 | cvtColor(srcImage,grayImage,CV_BGR2GRAY); 13 | 14 | Mat g1,g2,dst; 15 | GaussianBlur(grayImage,g1,Size(3,3),0,0); 16 | GaussianBlur(g1,g2,Size(3,3),0,0); 17 | 18 | subtract(g1,g2,dst); 19 | normalize(dst,dst,255,0,NORM_MINMAX); 20 | 21 | imshow("destination Image",dst); 22 | 23 | while(waitKey()!='q'); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /image/drink.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/drink.jpg -------------------------------------------------------------------------------- /image/gf.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/gf.jpg -------------------------------------------------------------------------------- /image/hazedImage/1.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/hazedImage/1.jpeg -------------------------------------------------------------------------------- /image/hazedImage/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/hazedImage/10.png -------------------------------------------------------------------------------- /image/hazedImage/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/hazedImage/11.png -------------------------------------------------------------------------------- /image/hazedImage/4.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/hazedImage/4.jpeg -------------------------------------------------------------------------------- /image/hazedImage/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/hazedImage/5.jpg -------------------------------------------------------------------------------- /image/hazedImage/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/hazedImage/6.jpg -------------------------------------------------------------------------------- /image/hazedImage/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/hazedImage/9.jpg -------------------------------------------------------------------------------- /image/lena.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/lena.jpg -------------------------------------------------------------------------------- /image/numberImage/0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/numberImage/0.jpg -------------------------------------------------------------------------------- /image/numberImage/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/numberImage/1.jpg -------------------------------------------------------------------------------- /image/numberImage/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/numberImage/2.jpg -------------------------------------------------------------------------------- /image/numberImage/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/numberImage/3.jpg -------------------------------------------------------------------------------- /image/numberImage/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/numberImage/4.jpg -------------------------------------------------------------------------------- /image/numberImage/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/numberImage/5.jpg -------------------------------------------------------------------------------- /image/numberImage/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/numberImage/6.jpg -------------------------------------------------------------------------------- /image/numberImage/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/numberImage/7.jpg -------------------------------------------------------------------------------- /image/numberImage/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/numberImage/8.jpg -------------------------------------------------------------------------------- /image/numberImage/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/numberImage/9.jpg -------------------------------------------------------------------------------- /image/ocr.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/ocr.gif -------------------------------------------------------------------------------- /image/ocr.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/ocr.jpg -------------------------------------------------------------------------------- /image/produceNumber.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/produceNumber.png -------------------------------------------------------------------------------- /image/retification/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/retification/1.png -------------------------------------------------------------------------------- /image/retification/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/retification/2.png -------------------------------------------------------------------------------- /image/retification/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/retification/3.png -------------------------------------------------------------------------------- /image/retification/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/retification/4.jpg -------------------------------------------------------------------------------- /image/retification/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/retification/5.jpg -------------------------------------------------------------------------------- /image/screenshoot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/screenshoot.png -------------------------------------------------------------------------------- /image/timg.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/starhiking/opencv-programme/70d38a1d7ec0ebdd9e788a7250cc0913df450f3a/image/timg.jpeg -------------------------------------------------------------------------------- /kernel.cpp: -------------------------------------------------------------------------------- 1 | //测试各种矩阵算子 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace cv; 7 | using namespace std; 8 | 9 | int main(int argc,char**argv) 10 | { 11 | Mat src = imread("image/drink.jpg"); 12 | if(!src.data) 13 | { 14 | cout<<"connot open this picture"; 15 | return -1; 16 | } 17 | 18 | Mat dst1,dst2,dst; 19 | 20 | Mat kernel_x = (Mat_(2,2)<<1,0,0,-1); 21 | filter2D(src,dst1,-1,kernel_x,Point(-1,-1),0,0); 22 | 23 | Mat kernel_y = (Mat_(2,2)<<0,1,-1,0); 24 | filter2D(src,dst2,-1,kernel_y,Point(-1,-1),0,0); 25 | 26 | 27 | Mat kernel = (Mat_(3,3)<<1,0,-1,2,0,-2,1,0,-1); 28 | filter2D(src,dst,-1,kernel,Point(-1,-1),0,0); 29 | 30 | imshow("kernel",dst); 31 | 32 | imshow("kernelx",dst1); 33 | 34 | imshow("kernely",dst2); 35 | 36 | while(waitKey()!='q'){} 37 | return 0; 38 | 39 | } -------------------------------------------------------------------------------- /number.dat: -------------------------------------------------------------------------------- 1 | 2 | 3 | 234567217432145032137421432421 4 | 8328432411414 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /paintFromPicture.cpp: -------------------------------------------------------------------------------- 1 | //通过图片画出他的轮廓 2 | #include 3 | #include "opencv2/opencv.hpp" 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | const char window_name_1[] = "srcImage from lena"; 9 | const char window_name_2[] = "paint result"; 10 | 11 | int main() 12 | { 13 | Mat srcImage = imread("image/timg.jpeg"); 14 | 15 | //resize(srcImage,srcImage,Size(500,500),0,0); 16 | Mat grayImage,dstImage; 17 | vector< vector > contours; 18 | vector hierarchy; 19 | 20 | cvtColor(srcImage,grayImage,CV_BGR2GRAY); 21 | Canny(grayImage,dstImage,50,200,3,false); 22 | //imshow("canny image",dstImage); 23 | findContours(dstImage,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point(0,0)); 24 | 25 | Mat finalImage = Mat::zeros(dstImage.size(),CV_8UC3); 26 | 27 | for(int i =contours.size()-1;i>0;i--) 28 | { 29 | Scalar color = Scalar(255,255,255); 30 | drawContours(finalImage,contours,i,color,1,8,hierarchy,0,Point()); 31 | imshow(window_name_2,finalImage); 32 | 33 | waitKey(40); 34 | } 35 | 36 | while(waitKey()!='q'){} 37 | return 0; 38 | } -------------------------------------------------------------------------------- /readImage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "opencv2/opencv.hpp" 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main() 8 | { 9 | Mat num[10]; 10 | for(int i =0;i<10;i++) 11 | { 12 | ostringstream fileName; 13 | fileName<<"image/numberImage/"< 7 | #include 8 | 9 | using namespace std; 10 | using namespace cv; 11 | 12 | int main() 13 | { 14 | Mat srcImage = imread("image/retification/3.png"); 15 | 16 | if(srcImage.cols>1000||srcImage.rows>800){ 17 | pyrDown(srcImage,srcImage); 18 | pyrDown(srcImage,srcImage); 19 | pyrDown(srcImage,srcImage); 20 | } 21 | 22 | Mat grayImage,binaryImage; 23 | cvtColor(srcImage,grayImage,CV_BGR2GRAY); 24 | adaptiveThreshold(grayImage,binaryImage,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,7,0); 25 | 26 | vector > contours; 27 | //只匹配外框CV_RETR_EXTERNAL,找出最大的轮廓 28 | findContours(binaryImage,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); 29 | float area = boundingRect(contours[0]).area();int index = 0; 30 | for(int i = 1;iarea) 34 | { 35 | area = boundingRect(contours[i]).area(); 36 | index = i; 37 | } 38 | } 39 | 40 | Rect maxRect = boundingRect(contours[index]); 41 | Mat ROI = binaryImage(maxRect); 42 | imshow("maxROI",ROI); 43 | 44 | RotatedRect rect = minAreaRect(contours[index]); 45 | Point2f rectPoint[4]; 46 | rect.points(rectPoint);//获取四个顶点坐标 47 | double angle = rect.angle; 48 | angle += 90; 49 | 50 | Point2f center = rect.center; 51 | Mat RoiSrcImg = Mat::zeros(srcImage.size(),srcImage.type()); 52 | 53 | drawContours(binaryImage,contours,-1,Scalar(255),CV_FILLED); 54 | // srcImage.copyTo(RoiSrcImg,binaryImage); 55 | srcImage.copyTo(RoiSrcImg); 56 | 57 | Mat Matrix = getRotationMatrix2D(center,angle,0.8);//得到旋转矩阵算子 58 | warpAffine(RoiSrcImg,RoiSrcImg,Matrix,RoiSrcImg.size(),1,0,Scalar(0,0,0)); 59 | 60 | imshow("src Image",srcImage); 61 | imshow("recorrected",RoiSrcImg); 62 | 63 | while(waitKey()!='q'){} 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /run: -------------------------------------------------------------------------------- 1 | sudo g++ -g $1 `pkg-config --cflags --libs opencv` && ./a.out 2 | -------------------------------------------------------------------------------- /updatedehaze.cpp: -------------------------------------------------------------------------------- 1 | //导向滤波算法实现图片去雾 2 | //参考网站 https://blog.csdn.net/s12244315/article/details/50292049 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace cv; 8 | 9 | const double omega = 0.90;//空间颗粒杂质影响程度 10 | const int windowsize = 15;//windowsize = 2r+1 11 | const int radius = 7; //窗口半径r 12 | const double t0 = 0.1; //tr预估值(最小) 13 | const double top = 0.001; //取top 求均值为A 14 | 15 | void minFilter(Mat&,Mat&); 16 | 17 | double max(double,double); 18 | 19 | double getAtmosphere(Mat&,double); 20 | 21 | int main(int argc,char**argv) 22 | { 23 | double startTime=(double)getTickCount(); 24 | Mat srcImage = imread("image/hazedImage/11.png"); 25 | if(!srcImage.data) 26 | { 27 | cout<<"cannot read this picture,the filename may be wrong"; 28 | return -1; 29 | } 30 | if(srcImage.cols>640) 31 | { 32 | resize(srcImage,srcImage,Size(640,480)); 33 | } 34 | //resize(srcImage,srcImage,Size(640,480)); 35 | 36 | Mat channels[3]; 37 | split(srcImage,channels); 38 | //先创建最小化的channel 39 | Mat minRGB = Mat::zeros(srcImage.size(),CV_8UC1); 40 | 41 | int rows = srcImage.rows; 42 | int cols = srcImage.cols; 43 | 44 | //1.求暗通道 45 | for(int i = 0;i(i,j); 50 | for(int c= 1;c<3;c++) 51 | minValue = minValue > channels[0].at(i,j) ? channels[0].at(i,j) : minValue; 52 | minRGB.at(i,j) = minValue; 53 | } 54 | 55 | } 56 | 57 | //imshow("min BGR",minRGB); 58 | 59 | //求全球大气光值A top = 0.001 60 | 61 | double A = getAtmosphere(minRGB,top);//假定A是255 62 | 63 | cout<<"A: "<(i,j) = saturate_cast(A - omega*minFilterImage.at(i,j)); 78 | } 79 | } 80 | 81 | //imshow("预估投射图",T); 82 | 83 | Mat srcROI = srcImage(Rect(radius,radius,T.cols,T.rows)); 84 | 85 | Mat finalImage = Mat::zeros(srcROI.size(),srcROI.type()); 86 | 87 | for(int i = 0;i(i,j),255*t0); 92 | 93 | for(int c = 0;c(i,j)[c] = saturate_cast(double(srcROI.at(i,j)[c] - A)/tempT +A); 96 | } 97 | 98 | } 99 | } 100 | 101 | finalImage *= 1.08; 102 | //boxFilter(finalImage,finalImage,-1,Size(3,3)); 103 | 104 | // resize(srcROI,srcROI,Size(700,800)); 105 | // resize(finalImage,finalImage,Size(700,800)); 106 | imshow("srcImage",srcROI); 107 | imshow("final image",finalImage); 108 | 109 | double time =double((getTickCount()-startTime)/getTickFrequency()); 110 | cout<<"时间为: "<(x,y);//先得到中点 136 | 137 | for(int a = 0;asrc.at(x-a,y-b)) minValue=src.at(x-a,y-b); 142 | if(minValue>src.at(x+a,y-b)) minValue=src.at(x+a,y-b); 143 | if(minValue>src.at(x-a,y+b)) minValue=src.at(x-a,y+b); 144 | if(minValue>src.at(x+a,y+b)) minValue=src.at(x+a,y+b); 145 | } 146 | } 147 | minImage.at(i,j) = minValue; 148 | } 149 | } 150 | dst = minImage; 151 | } 152 | 153 | //返回最大值 并且归一化 154 | double max(double a,double b) 155 | { 156 | return a>b?a/255:b/255; 157 | } 158 | 159 | 160 | double getAtmosphere(Mat&img,double precison) 161 | { 162 | const int num = img.rows*img.cols*precison; 163 | int tempNum = num; 164 | 165 | //设定取值范围 166 | float range[2] = {0, 255}; 167 | const float* histRange[1] = {range}; 168 | Mat hist; 169 | int histSize[1] = {256}; 170 | calcHist(&img,1,0,Mat(),hist,1,histSize,histRange,true,false); 171 | 172 | int sum = 0; 173 | for(int i =255;i>0;i--) 174 | { 175 | if(hist.at(i)==0) continue; 176 | if(hist.at(i)(i); 179 | sum+=i*hist.at(i); 180 | }else 181 | { 182 | sum+=i*tempNum; 183 | tempNum = 0; 184 | break; 185 | } 186 | } 187 | 188 | return (sum/num); 189 | } -------------------------------------------------------------------------------- /verfiyCode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"opencv2/opencv.hpp" 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | Mat db[10] ; 8 | const int dbLen = 10; 9 | 10 | int main(int argc,char**argv) 11 | { 12 | //载入数据库 10张数字图片作为模板 13 | for(int i =0;i<10;i++) 14 | { 15 | ostringstream fileName; 16 | fileName <<"image/numberImage/"< > contours; 39 | vector hierarchy; 40 | findContours(dstImage,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); 41 | drawContours(Image,contours,-1,Scalar(255,255,255)); 42 | 43 | Mat copyImage = Image.clone(); 44 | 45 | Rect rect[64]; //存放10个数字的rect 46 | 47 | //根据contours迭代器遍历每一个轮廓 画出所有最小矩阵 48 | vector >::iterator It; int index; 49 | for( index = 0, It = contours.begin();It < contours.end();It++,index++){ //画出可包围数字的最小矩形 50 | 51 | Point2f vertex[4]; 52 | rect[index] = boundingRect(*It); 53 | vertex[0] = rect[index].tl(); //矩阵左上角的点 54 | vertex[1].x = (float)rect[index].tl().x, vertex[1].y = (float)rect[index].br().y; //矩阵左下方的点 55 | vertex[2] = rect[index].br(); //矩阵右下角的点 56 | vertex[3].x = (float)rect[index].br().x, vertex[3].y = (float)rect[index].tl().y; //矩阵右上方的点 57 | 58 | for(int j = 0; j < 4; j++) 59 | { 60 | line(Image,vertex[j], vertex[ (j+1)%4 ],Scalar(0,0,255),1); 61 | } 62 | 63 | 64 | } 65 | 66 | imshow("line rects",Image); 67 | 68 | for(int t = index -1;t>=0;t--) 69 | { 70 | 71 | //调整矩阵 与数据库图片大小一致 72 | Mat tempRect = Mat::zeros(db[0].size(),db[0].type()); 73 | resize(copyImage(rect[t]),tempRect,tempRect.size()); 74 | int rows = tempRect.rows; 75 | int cols = tempRect.cols*tempRect.channels(); 76 | 77 | //每个rect在数据库中进行匹配,找出最相近的值 78 | int Matchresult = 0;//最的小匹配值 79 | int MatchIndex = 0;//最小匹配值对应的的db序号 80 | 81 | for(int u=0;u(v); 88 | uchar*data2 = db[u].ptr(v); 89 | 90 | for(int w = 0;wMatchresult) 98 | { 99 | Matchresult = same; 100 | MatchIndex = u; 101 | } 102 | 103 | //如果这张图片完全一致 则不匹配其他的图片 104 | if(same==rows*cols/3) 105 | { 106 | break; 107 | } 108 | } 109 | //输出匹配的结果 110 | cout<=0;t--,name++) 116 | // { 117 | // Mat tempImg; 118 | // resize(copyImage(rect[t]),tempImg,Size(30,30)); 119 | // ostringstream fileName; 120 | // fileName <<"image/numberImage/"<