repl.it
@AlephZero/

TestingOpenCV

C++

No description

fork
loading
Files
  • main.cpp
  • page1.png
main.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
ayoungprogrammer.blogspot.com

Part 1: Extracting contours from text

*/

#include <iostream>

#include <Windows.h>

#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include "ml.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#endif

using namespace std;
using namespace cv;


class comparator{
public:
	bool operator()(vector<Point> c1,vector<Point>c2){
		
		return boundingRect( Mat(c1)).x<boundingRect( Mat(c2)).x;

	}

};



void extractContours(Mat& image,vector< vector<Point> > contours_poly){


	
	//Sort contorus by x value going from left to right
	sort(contours_poly.begin(),contours_poly.end(),comparator());


	//Loop through all contours to extract
	 for( int i = 0; i< contours_poly.size(); i++ ){

		Rect r = boundingRect( Mat(contours_poly[i]) );
		
		
		Mat mask = Mat::zeros(image.size(), CV_8UC1);
		//Draw mask onto image
		drawContours(mask, contours_poly, i, Scalar(255), CV_FILLED);

		//Check for equal sign (2 dashes on top of each other) and merge
		if(i+1<contours_poly.size()){
			Rect r2 = boundingRect( Mat(contours_poly[i+1]) );
			if(abs(r2.x-r.x)<20){
				//Draw mask onto image
				drawContours(mask, contours_poly, i+1, Scalar(255), CV_FILLED); 
				i++;
				int minX = min(r.x,r2.x); 
				int minY = min(r.y,r2.y);
				int maxX =  max(r.x+r.width,r2.x+r2.width);
				int maxY = max(r.y+r.height,r2.y+r2.height);
				r = Rect(minX,minY,maxX - minX,maxY-minY);

			}
		}
		//Copy 
		 Mat extractPic;
		 //Extract the character using the mask
		 image.copyTo(extractPic,mask);
		 Mat resizedPic = extractPic(r);
	
		cv::Mat image=resizedPic.clone();

		//Show image
		imshow("image",image);
		char ch  = waitKey(0);
		stringstream searchMask;
		searchMask<<i<<".jpg";
		imwrite(searchMask.str(),resizedPic);

	 }





}

void getContours(const char* filename)
{
  cv::Mat img = cv::imread(filename, 0);


  //Apply blur to smooth edges and use adapative thresholding
   cv::Size size(3,3);
  cv::GaussianBlur(img,img,size,0);
   adaptiveThreshold(img, img,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,75,10);
  cv::bitwise_not(img, img);



 
  cv::Mat img2 = img.clone();

  
  std::vector<cv::Point> points;
  cv::Mat_<uchar>::iterator it = img.begin<uchar>();
  cv::Mat_<uchar>::iterator end = img.end<uchar>();
  for (; it != end; ++it)
    if (*it)
      points.push_back(it.pos());
 
  cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));

   double angle = box.angle;
  if (angle < -45.)
    angle += 90.;
	  
  cv::Point2f vertices[4];
  box.points(vertices);
  for(int i = 0; i < 4; ++i)
    cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(255, 0, 0), 1, CV_AA);
 
	  

   cv::Mat rot_mat = cv::getRotationMatrix2D(box.center, angle, 1);

   cv::Mat rotated;
  cv::warpAffine(img2, rotated, rot_mat, img.size(), cv::INTER_CUBIC);

	

  cv::Size box_size = box.size;
  if (box.angle < -45.)
    std::swap(box_size.width, box_size.height);
  cv::Mat cropped;
  
  cv::getRectSubPix(rotated, box_size, box.center, cropped);
  cv::imshow("Cropped", cropped);
  imwrite("example5.jpg",cropped);
	
	Mat cropped2=cropped.clone();
cvtColor(cropped2,cropped2,CV_GRAY2RGB);

Mat cropped3 = cropped.clone();
cvtColor(cropped3,cropped3,CV_GRAY2RGB);

 vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;

  /// Find contours
  cv:: findContours( cropped, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_KCOS, Point(0, 0) );



  /// Approximate contours to polygons + get bounding rects and circles
  vector<vector<Point> > contours_poly( contours.size() );
  vector<Rect> boundRect( contours.size() );
  vector<Point2f>center( contours.size() );
  vector<float>radius( contours.size() );


  //Get poly contours
	for( int i = 0; i < contours.size(); i++ )
     { 
		 approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
     }


  //Get only important contours, merge contours that are within another
  vector<vector<Point> > validContours;
	for (int i=0;i<contours_poly.size();i++){
		
		Rect r = boundingRect(Mat(contours_poly[i]));
		if(r.area()<100)continue; 
		bool inside = false;
		for(int j=0;j<contours_poly.size();j++){
			if(j==i)continue;
			
			Rect r2 = boundingRect(Mat(contours_poly[j]));
			if(r2.area()<100||r2.area()<r.area())continue;
			if(r.x>r2.x&&r.x+r.width<r2.x+r2.width&&
				r.y>r2.y&&r.y+r.height<r2.y+r2.height){

				inside = true;
			}
		}
		if(inside)continue;
		validContours.push_back(contours_poly[i]);
	}


	//Get bounding rects
	for(int i=0;i<validContours.size();i++){
		boundRect[i] = boundingRect( Mat(validContours[i]) );
	}
  

	//Display
  Scalar color = Scalar(0,255,0);
  for( int i = 0; i< validContours.size(); i++ )
     {
	if(boundRect[i].area()<100)continue;
      drawContours( cropped2, validContours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
       rectangle( cropped2, boundRect[i].tl(), boundRect[i].br(),color, 2, 8, 0 );
     }

  //imwrite("example6.jpg",cropped2);
  imshow("Contours",cropped2);

  extractContours(cropped3,validContours);
  
cv::waitKey(0);

}





int main(void){

char fileName[256];
cin>>fileName;
getContours(fileName);

}
Fetching token
?