Von Ihrem Eingangsbild, müssen Sie Text als weiße und Hintergrund als schwarz

Sie dann zu berechnen, müssen dafür der Drehwinkel Ihrer Rechnung. Ein einfacher Ansatz ist die minAreaRect
alle weißen Punkte (findNonZero
) zu finden, und Sie erhalten:

Dann können Sie Ihre Rechnung drehen, so dass der Text ist horizontal:

Jetzt können Sie die horizontale Projektion berechnen (reduce
). Sie können den Durchschnittswert in jeder Zeile verwenden. Wende einen Schwellenwert th
auf das Histogramm an, um etwas Rauschen in dem Bild zu berücksichtigen (hier verwendete ich 0
, d. H. Kein Rauschen). Zeilen mit nur Hintergrund haben einen Wert >0
, Textzeilen haben im Histogramm den Wert 0
. Dann nehmen Sie die durchschnittliche Bin-Koordinate jeder kontinuierlichen Folge von weißen Bins im Histogramm. Das wird die y
Ihrer Linien koordinieren:

Hier ist der Code. Es ist in C++, aber da die meiste Arbeit mit OpenCV-Funktionen ist, sollte es leicht in Python konvertierbar sein.Zumindest können Sie diese als Referenz verwenden:
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
// Read image
Mat3b img = imread("path_to_image");
// Binarize image. Text is white, background is black
Mat1b bin;
cvtColor(img, bin, COLOR_BGR2GRAY);
bin = bin < 200;
// Find all white pixels
vector<Point> pts;
findNonZero(bin, pts);
// Get rotated rect of white pixels
RotatedRect box = minAreaRect(pts);
if (box.size.width > box.size.height)
{
swap(box.size.width, box.size.height);
box.angle += 90.f;
}
Point2f vertices[4];
box.points(vertices);
for (int i = 0; i < 4; ++i)
{
line(img, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0));
}
// Rotate the image according to the found angle
Mat1b rotated;
Mat M = getRotationMatrix2D(box.center, box.angle, 1.0);
warpAffine(bin, rotated, M, bin.size());
// Compute horizontal projections
Mat1f horProj;
reduce(rotated, horProj, 1, CV_REDUCE_AVG);
// Remove noise in histogram. White bins identify space lines, black bins identify text lines
float th = 0;
Mat1b hist = horProj <= th;
// Get mean coordinate of white white pixels groups
vector<int> ycoords;
int y = 0;
int count = 0;
bool isSpace = false;
for (int i = 0; i < rotated.rows; ++i)
{
if (!isSpace)
{
if (hist(i))
{
isSpace = true;
count = 1;
y = i;
}
}
else
{
if (!hist(i))
{
isSpace = false;
ycoords.push_back(y/count);
}
else
{
y += i;
count++;
}
}
}
// Draw line as final result
Mat3b result;
cvtColor(rotated, result, COLOR_GRAY2BGR);
for (int i = 0; i < ycoords.size(); ++i)
{
line(result, Point(0, ycoords[i]), Point(result.cols, ycoords[i]), Scalar(0, 255, 0));
}
return 0;
}
Können Sie Code anzeigen? – RvdK
Ok, ich habe den Code hinzugefügt, den ich benutze. – Alex
ein Beispielbild wird hilfreich sein. – sturkmen