Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
a6359e49
Commit
a6359e49
authored
Dec 16, 2019
by
Gourav Roy
Committed by
Alexander Smorkalov
Jan 23, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added tutorial for text skewness correction in C++ and Python.
parent
ed788229
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
132 additions
and
0 deletions
+132
-0
text_skewness_correction.cpp
samples/cpp/text_skewness_correction.cpp
+74
-0
text_skewness_correction.py
samples/python/text_skewness_correction.py
+58
-0
No files found.
samples/cpp/text_skewness_correction.cpp
0 → 100644
View file @
a6359e49
/*
This tutorial demonstrates how to correct the skewness in a text.
The program takes as input a skewed source image and shows non skewed text.
*/
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <iomanip>
#include <string>
using
namespace
cv
;
using
namespace
std
;
int
main
(
int
argc
,
char
**
argv
)
{
CommandLineParser
parser
(
argc
,
argv
,
"{@input | imageTextR.png | input image}"
);
// Load image from the disk
Mat
image
=
imread
(
samples
::
findFile
(
parser
.
get
<
String
>
(
"@input"
)
),
IMREAD_COLOR
);
if
(
image
.
empty
())
{
cout
<<
"Cannot load the image "
+
parser
.
get
<
String
>
(
"@input"
)
<<
endl
;
return
-
1
;
}
Mat
gray
;
cvtColor
(
image
,
gray
,
COLOR_BGR2GRAY
);
//Threshold the image, setting all foreground pixels to 255 and all background pixels to 0
Mat
thresh
;
threshold
(
gray
,
thresh
,
0
,
255
,
THRESH_BINARY_INV
|
THRESH_OTSU
);
// Applying erode filter to remove random noise
int
erosion_size
=
1
;
Mat
element
=
getStructuringElement
(
MORPH_RECT
,
Size
(
2
*
erosion_size
+
1
,
2
*
erosion_size
+
1
),
Point
(
erosion_size
,
erosion_size
)
);
erode
(
thresh
,
thresh
,
element
);
cv
::
Mat
coords
;
findNonZero
(
thresh
,
coords
);
RotatedRect
box
=
minAreaRect
(
coords
);
float
angle
=
box
.
angle
;
// The cv::minAreaRect function returns values in the range [-90, 0)
// if the angle is less than -45 we need to add 90 to it
if
(
angle
<
-
45.0
f
)
{
angle
=
(
90.0
f
+
angle
);
}
//Obtaining the rotation matrix
Point2f
center
((
image
.
cols
)
/
2.0
f
,
(
image
.
rows
)
/
2.0
f
);
Mat
M
=
getRotationMatrix2D
(
center
,
angle
,
1.0
f
);
Mat
rotated
;
// Rotating the image by required angle
stringstream
angle_to_str
;
angle_to_str
<<
fixed
<<
setprecision
(
2
)
<<
angle
;
warpAffine
(
image
,
rotated
,
M
,
image
.
size
(),
INTER_CUBIC
,
BORDER_REPLICATE
);
putText
(
rotated
,
"Angle "
+
angle_to_str
.
str
()
+
" degrees"
,
Point
(
10
,
30
),
FONT_HERSHEY_SIMPLEX
,
0.7
,
Scalar
(
0
,
0
,
255
),
2
);
cout
<<
"[INFO] angle: "
<<
angle_to_str
.
str
()
<<
endl
;
//Show the image
imshow
(
"Input"
,
image
);
imshow
(
"Rotated"
,
rotated
);
waitKey
(
0
);
return
0
;
}
samples/python/text_skewness_correction.py
0 → 100644
View file @
a6359e49
'''
Text skewness correction
This tutorial demonstrates how to correct the skewness in a text.
The program takes as input a skewed source image and shows non skewed text.
Usage:
python text_skewness_correction.py --image "Image path"
'''
import
numpy
as
np
import
cv2
as
cv
import
sys
import
argparse
def
main
():
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"-i"
,
"--image"
,
required
=
True
,
help
=
"path to input image file"
)
args
=
vars
(
parser
.
parse_args
())
# load the image from disk
image
=
cv
.
imread
(
cv
.
samples
.
findFile
(
args
[
"image"
]))
if
image
is
None
:
print
(
"can't read image "
+
args
[
"image"
])
sys
.
exit
(
-
1
)
gray
=
cv
.
cvtColor
(
image
,
cv
.
COLOR_BGR2GRAY
)
# threshold the image, setting all foreground pixels to
# 255 and all background pixels to 0
thresh
=
cv
.
threshold
(
gray
,
0
,
255
,
cv
.
THRESH_BINARY_INV
|
cv
.
THRESH_OTSU
)[
1
]
# Applying erode filter to remove random noise
erosion_size
=
1
element
=
cv
.
getStructuringElement
(
cv
.
MORPH_RECT
,
(
2
*
erosion_size
+
1
,
2
*
erosion_size
+
1
),
(
erosion_size
,
erosion_size
)
)
thresh
=
cv
.
erode
(
thresh
,
element
)
coords
=
cv
.
findNonZero
(
thresh
)
angle
=
cv
.
minAreaRect
(
coords
)[
-
1
]
# the `cv.minAreaRect` function returns values in the
# range [-90, 0) if the angle is less than -45 we need to add 90 to it
if
angle
<
-
45
:
angle
=
(
90
+
angle
)
(
h
,
w
)
=
image
.
shape
[:
2
]
center
=
(
w
//
2
,
h
//
2
)
M
=
cv
.
getRotationMatrix2D
(
center
,
angle
,
1.0
)
rotated
=
cv
.
warpAffine
(
image
,
M
,
(
w
,
h
),
flags
=
cv
.
INTER_CUBIC
,
borderMode
=
cv
.
BORDER_REPLICATE
)
cv
.
putText
(
rotated
,
"Angle: {:.2f} degrees"
.
format
(
angle
),
(
10
,
30
),
cv
.
FONT_HERSHEY_SIMPLEX
,
0.7
,
(
0
,
0
,
255
),
2
)
# show the output image
print
(
"[INFO] angle: {:.2f}"
.
format
(
angle
))
cv
.
imshow
(
"Input"
,
image
)
cv
.
imshow
(
"Rotated"
,
rotated
)
cv
.
waitKey
(
0
)
if
__name__
==
"__main__"
:
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment