Here I am trying to get out Equal sized blocks to get the numbers ready for an OCR application
1st try through little code that moves by fixed steps while at some positions it jumps high due to the space between numbers , the main problem is at the last 5 digits , sometimes they are 2 numbers , space then 3 numbers , sometimes they are 3 numbers , space then 2 numbers and finally they maybe 5 numbers if the 5 numbers are big
2nd try i used FindContour and when it find the object i resize the rectangle to fit it but the problem is that it didn't give me the numbers in-order from left to right or the opposite.
so how can i deal with that ?
1st try:
void DetectEqualRectangles(Mat image){
resize(image,image,Size(810,52));
int k=0;
for(int i=0;i<14;i++){
rectangle(image,Point(k,0),Point(45+k,52),Scalar(0,0,255),1,8,0);
imshow("1",image);
waitKey(0);
if(i==0){k+=70;}
else if(i==2){k+=71;}
else if(i==4){k+=75;}
else if(i==6){k+=78;}
else if(i==8){k+=76;}
else{k+=50;}
}}
2nd try:
void DetectUsingContours(Mat image){
resize(image,image,Size(810,52));
Mat gray;int BrightnessIndicator=0;
cvtColor(image,gray,CV_BGR2GRAY);
GaussianBlur(gray,gray,Size(5,5),3,0); // applying a gaussianBlur
BrightnessIndicator=EstimateBrighteness(image); // getting the approximate value for the brightness
cout<<BrightnessIndicator<<endl;
threshold(gray,gray,BrightnessIndicator-33,255,CV_THRESH_BINARY_INV); //thresholding
imshow("s",gray);
vector< vector<Point> > Contour;
findContours(gray,Contour,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE); //finding outer contours
cout<<Contour.size();
for(int i=0;i<Contour.size();i++){
Rect bounding = boundingRect(Contour[i]); // draw a rectangle
if(bounding.x>15 && bounding.x<image.cols-50){bounding.x-=15;bounding.width=50;}
else if(bounding.x>image.cols-50){bounding.x=image.cols-40;bounding.width=40;}
else{bounding.x=0;bounding.width=50;}
bounding.y-=bounding.y;
bounding.height=image.rows;
// rectangle(image,bounding,Scalar(0,255,0),1,8,0);
Mat CroppedImage=image(bounding);
stringstream ss;
ss<<"C:\\Users\\cdc\\Desktop\\GSC\\ExtractingNumbers\\"<<i<<".jpg";
imwrite(ss.str(),CroppedImage);
imshow("5",image);
imshow("23",CroppedImage);
waitKey(0);
}}