Ask Your Question
3

Number extraction on metal surface1

asked 2014-03-04 07:09:35 -0600

Nagaraj gravatar image

updated 2020-11-02 11:18:35 -0600

<strong>image description</strong>

Dear sir,

@Haris Moonamkunnu, my original pic is exactly like this , In my previous post I have cropped the region and i Have posted .

If this is the case what methods i have to adopt and how to proceed further. After the process the number has to detected by tessseract-ocr.

I am struggling from past 2 months plz help me .

image description

Blockquote

Hi, I have to extract the number 1 in an Image and the number recognized by tesseract.

Please suggest me the ideas to implement and process this image .

Regards Nagaraj

edit retag flag offensive close merge delete

Comments

prakharmohan gravatar imageprakharmohan ( 2014-02-20 01:00:36 -0600 )edit

@Nagaraj So your sample image always be like this only ?

Haris gravatar imageHaris ( 2014-02-20 22:58:43 -0600 )edit

@prakharmohan , Ya I tried I have done threshold and segmentation . Due to uneven contrast of the image I will get too much noise and tesseract won't detect . plz can u suggest me how can extract 1 from this image without background .

Nagaraj gravatar imageNagaraj ( 2014-02-24 02:46:54 -0600 )edit

@Haris Moonamkunnu , Yes my sample image is like this only

Nagaraj gravatar imageNagaraj ( 2014-02-24 02:48:34 -0600 )edit

@Haris Moonamkunnu, Sir, thanks a lot :)

Nagaraj gravatar imageNagaraj ( 2014-02-24 23:37:25 -0600 )edit

Welcome.........

Haris gravatar imageHaris ( 2014-02-24 23:42:16 -0600 )edit

@Nagaraj Accept the answer if it is working then!

prakharmohan gravatar imageprakharmohan ( 2014-02-25 00:40:44 -0600 )edit

first you have good lighting. Metal tends to be reflective so it's good that you have the camera above the number and the light source from the side. Next you need a image of each number. I would then think you could use one of the object detectors.

GrumbleLion gravatar imageGrumbleLion ( 2014-03-08 19:06:03 -0600 )edit

@GrumbleLion thanks for the info :)

Nagaraj gravatar imageNagaraj ( 2014-03-09 23:08:34 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
5

answered 2014-02-24 06:18:54 -0600

Haris gravatar image

You can segment the number easily using watershed() for this you need to create markers to distinguish foreground and background.

1. Create Mask: As first step we will create make image, you need to do adaptiveThreshold(), contour finding(largest), convexHull() etc..

image description image description

Below is the code how to do it

Mat src=imread("src.jpg",1);
Mat thr,gray;
cvtColor(src,gray,CV_BGR2GRAY);
adaptiveThreshold(gray,thr,255,ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY,51,5);

vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
int largest_contour_index=0;
int largest_area=0;
Mat mask(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
findContours( thr.clone(), contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
 {
  double a=contourArea( contours[i],false);  //  Find the area of contour
  if(a>largest_area){
  largest_area=a;
  largest_contour_index=i;                //Store the index of largest contour
  }
 }
 vector<vector<Point> >hull(1);
 convexHull(contours[largest_contour_index],  hull[0],false,true );
 drawContours( mask, hull, 0, Scalar(125), -1, 8, vector<Vec4i>(), 0, Point() );

2. Create Foreground: For this you need to do again adaptiveThreshold() but with THRESH_BINARY_INV option, and erode() result to reduce noise, finally find biggest contour which represent your foreground object, here you need to consider the mask while copying to foreground image. The result look like the object to be segmented, but for better result we will move to next steps like background creation, watershed etc..

image description

 Mat fg;
 adaptiveThreshold(gray,thr,255,ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV,151,5);
 erode(thr,thr,Mat(),Point(-1,-1),1);
 contours.clear();
 hierarchy.clear();
 largest_contour_index=0;
 largest_area=0;
 thr.copyTo(fg,mask),
 findContours( fg, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
  for( int i = 0; i< contours.size(); i++ ) // iterate through each contour.
   {
    double a=contourArea( contours[i],false);  //  Find the area of contour
    if(a>largest_area){
    largest_area=a;
    largest_contour_index=i;                //Store the index of largest contour
    }
   }

 fg.setTo(0);
 drawContours( fg,contours, largest_contour_index, Scalar(255),CV_FILLED, 8, hierarchy );

3. Create Background: Creating background marker is easy as you have mask and foreground just subtract foreground from mask, then apply erode() to make little separation between fg and bg while merging.

image description

 Mat bg=mask-fg;
 erode(bg,bg,Mat(),Point(-1,-1),7);

4. Create Marker for Watershed: Just add up fg and bg and convert CV_32S for passing watershed() image description

5. Perform watershed Operation: Here you need to pass this marker image along with your source image.

image description

 markers.convertTo(markers, CV_32S);
 watershed(src, markers);
 markers.convertTo(markers,CV_8U);
 imshow("Segmented",markers);
edit flag offensive delete link more

Comments

Brilliant! :)

prakharmohan gravatar imageprakharmohan ( 2014-02-25 00:39:55 -0600 )edit

@Haris Moonamkunnu, sir, This output picture is input to tesseract , Unable to detect the number by tessseract . and how to correct the edges in the image .

Nagaraj gravatar imageNagaraj ( 2014-03-04 06:59:46 -0600 )edit
0

answered 2014-02-24 03:36:18 -0600

Nagaraj gravatar image

I have tried like this ..!(manually croped and apply threshold and segmentation ) image description image description If do more threshold i will lose some part of number . please suggest me how to extract only 1 from image .

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-03-04 07:09:35 -0600

Seen: 555 times

Last updated: Nov 02 '20