Text Segmentation

Binarize

  1. Mat binarize(Mat input) 
  2. {	
  3. 	//Uses otsu to threshold the input image
  4. 	Mat binaryImage;
  5. 	cvtColor(input, input, CV_BGR2GRAY);
  6. 	threshold(input, binaryImage, 0, 255, THRESH_OTSU);
  7.  
  8. 	//Count the number of black and white pixels
  9. 	int white = countNonZero(binaryImage);
  10. 	int black = binaryImage.size().area() - white;
  11.  
  12. 	//If the image is mostly white (white background), invert it
  13. 	return white < black ? binaryImage : ~binaryImage;
  14. }

Dilate & Find Contours

  1. vector<RotatedRect> findTextAreas(Mat input) {
  2. 	//Dilate the image
  3. 	Mat kernel = getStructuringElement(MORPH_CROSS, Size(3,3));
  4. 	Mat dilated;
  5. 	dilate(input, dilated, kernel, cv::Point(-1, -1), 5);
  6.  
  7. 	//Find all image contours
  8. 	vector<vector<Point> > contours;
  9. 	findContours(dilated, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
  10.  
  11. 	//For each contour
  12. 	vector<RotatedRect> areas;
  13. 	for (auto contour : contours) 
  14. 	{	
  15. 		//Find it's rotated rect
  16. 		auto box = minAreaRect(contour);
  17.  
  18. 		//Discard very small boxes
  19. 		if (box.size.width < 20 || box.size.height < 20)
  20. 			continue;
  21.  
  22. 		//Discard squares shaped boxes and boxes 
  23. 		//higher than larger
  24. 		double proportion = box.angle < -45.0 ?
  25. 			box.size.height / box.size.width : 
  26. 			box.size.width / box.size.height;
  27.  
  28. 		if (proportion < 2) 
  29. 			continue;
  30.  
  31. 		//Add the box
  32. 		areas.push_back(box);
  33. 	}
  34. 	return areas;
  35. }

Crop

  1. Mat deskewAndCrop(Mat input, const RotatedRect& box)
  2. {
  3. 	double angle = box.angle;	
  4. 	Size2f size = box.size;
  5.  
  6. 	//Adjust the box angle
  7.    if (angle < -45.0) 
  8. 	{
  9.         angle += 90.0;
  10.         std::swap(size.width, size.height);		
  11. 	}
  12.  
  13. 	//Rotate the text according to the angle
  14. 	Mat transform = getRotationMatrix2D(box.center, angle, 1.0);
  15. 	Mat rotated;
  16. 	warpAffine(input, rotated, transform, input.size(), INTER_CUBIC);
  17.  
  18. 	//Crop the result
  19. 	Mat cropped;
  20. 	getRectSubPix(rotated, size, box.center, cropped);
  21. 	copyMakeBorder(cropped,cropped,10,10,10,10,BORDER_CONSTANT,Scalar(0));
  22. 	return cropped;
  23. }

Leave a Reply

Your email address will not be published. Required fields are marked *