|
| 1 | +#include "stdafx.h" |
| 2 | +#include "OcrImageNoiseCleaner.h" |
| 3 | + |
| 4 | +using namespace cv; |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +OcrImageNoiseCleaner::OcrImageNoiseCleaner(wstring imageFileName, Mat originalImageData, SystemMethods* systemMethods, bool runDebugging) |
| 8 | + : BasicReaderData(imageFileName, originalImageData, systemMethods, runDebugging) |
| 9 | +{ |
| 10 | +} |
| 11 | + |
| 12 | +OcrImageNoiseCleaner::~OcrImageNoiseCleaner() |
| 13 | +{ |
| 14 | +} |
| 15 | + |
| 16 | +void OcrImageNoiseCleaner::CleanImage(Mat& dirtyImage) { |
| 17 | + |
| 18 | + Mat originalImage; |
| 19 | + dirtyImage.copyTo(originalImage); |
| 20 | + |
| 21 | + threshold(dirtyImage, dirtyImage, 130, 255, THRESH_BINARY); |
| 22 | + Contours contours = ImageHelper::GetCannyContours(dirtyImage, 120); |
| 23 | + LetterAreas figures = ImageHelper::ToLetterAreas(contours); |
| 24 | + |
| 25 | + for (size_t i = 0; i < figures.size(); i++) { |
| 26 | + |
| 27 | + handleFigure(figures[i], dirtyImage); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +void OcrImageNoiseCleaner::handleFigure(LetterArea figure, Mat& dirtyImage) { |
| 32 | + |
| 33 | + if (figure.Box.size.area() == 0) { return; } |
| 34 | + |
| 35 | + Mat contourImage = ImageHelper::DrawLimits(dirtyImage, { figure.TightContour }, Hierarchy(), false); |
| 36 | + |
| 37 | + if (isNoise(figure, dirtyImage.size())) { |
| 38 | + |
| 39 | + Scalar white(255, 255, 255); |
| 40 | + ImageHelper::FillContour(dirtyImage, figure.OuterContour, white); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +bool OcrImageNoiseCleaner::isNoise(LetterArea figure, Size imageArea) { |
| 45 | + |
| 46 | + if (isOblong(figure)) { return true; } |
| 47 | + if (isSmallEnoughToBeNoise(figure) && |
| 48 | + !isInCenter(figure, imageArea)) { return true; } |
| 49 | + |
| 50 | + return false; |
| 51 | +} |
| 52 | + |
| 53 | +bool OcrImageNoiseCleaner::isOblong(LetterArea figure) { |
| 54 | + |
| 55 | + double height = min(figure.Box.size.height, figure.Box.size.width); |
| 56 | + double width = max(figure.Box.size.height, figure.Box.size.width); |
| 57 | + double relation = width / height; |
| 58 | + |
| 59 | + return (relation > 5.5); |
| 60 | +} |
| 61 | + |
| 62 | +bool OcrImageNoiseCleaner::isSmallEnoughToBeNoise(LetterArea figure) { |
| 63 | + |
| 64 | + bool isOpen = !isContourConvex(figure.TightContour); |
| 65 | + double area = contourArea(isOpen ? figure.OuterContour : figure.TightContour); |
| 66 | + int maxNoiseArea = (int)(WORKING_CARD_HEIGHT / 8.5); //80 |
| 67 | + |
| 68 | + return (area <= maxNoiseArea); |
| 69 | +} |
| 70 | + |
| 71 | +bool OcrImageNoiseCleaner::isInCenter(LetterArea figure, Size imageArea) { |
| 72 | + |
| 73 | + int borderMargin = (int)(WORKING_CARD_HEIGHT / 27.2); //25 |
| 74 | + Rect rCenter(borderMargin, borderMargin, imageArea.width - 2 * borderMargin, imageArea.height - 2 * borderMargin); |
| 75 | + RotatedRect rrCenter = ImageHelper::ToRotatedRectangle(rCenter); |
| 76 | + bool placedInCenter = ImageHelper::DoesRectangleContainPoint(rrCenter, figure.Box.center); |
| 77 | + |
| 78 | + return placedInCenter; |
| 79 | +} |
0 commit comments