24 #include "allheaders.h" 93 Pix* pix = pixConvertTo8(input,
false);
96 int width = pixGetWidth(input);
97 int height = pixGetHeight(input);
103 input = pixErodeGray(pix, 3, 3);
108 pix = pixBlockconv(input, 1, 1);
111 if (rotation !=
nullptr) {
112 float radians_clockwise = 0.0f;
114 radians_clockwise = *rotation;
115 }
else if (randomizer !=
nullptr) {
119 input = pixRotate(pix, radians_clockwise,
120 L_ROTATE_AREA_MAP, L_BRING_IN_WHITE,
123 *rotation = radians_clockwise;
129 if (exposure >= 3 || exposure == 1) {
135 input = pixErodeGray(pix, 3, 3);
141 int erosion_offset = 0;
153 l_uint32* data = pixGetData(input);
154 for (
int y = 0; y < height; ++y) {
155 for (
int x = 0; x < width; ++x) {
156 int pixel = GET_DATA_BYTE(data, x);
157 if (randomizer !=
nullptr)
160 pixel -= (2*x + y) * 32 / (height + width);
161 pixel += erosion_offset;
166 SET_DATA_BYTE(data, x, pixel);
179 bool white_noise,
bool smooth_noise,
bool blur,
180 int box_reduction,
TRand* randomizer,
182 Pix* distorted = pixCopy(
nullptr, const_cast<Pix*>(pix));
184 if (invert && randomizer->
SignedRand(1.0) < 0)
185 pixInvert(distorted, distorted);
186 if ((white_noise || smooth_noise) && randomizer->
SignedRand(1.0) > 0.0) {
190 Pix* pixn = pixAddGaussianNoise(distorted, 8.0);
191 pixDestroy(&distorted);
193 distorted = pixBlockconv(pixn, 1, 1);
199 if (blur && randomizer->
SignedRand(1.0) > 0.0) {
200 Pix* blurred = pixBlockconv(distorted, 1, 1);
201 pixDestroy(&distorted);
206 if (boxes !=
nullptr) {
207 for (
int b = 0; b < boxes->
size(); ++b) {
208 (*boxes)[b].scale(1.0f / box_reduction);
209 if ((*boxes)[b].width() <= 0)
210 (*boxes)[b].set_right((*boxes)[b].left() + 1);
221 if (pix !=
nullptr && *pix !=
nullptr) {
222 width = pixGetWidth(*pix);
223 height = pixGetHeight(*pix);
225 float* im_coeffs =
nullptr;
226 float* box_coeffs =
nullptr;
229 if (pix !=
nullptr && *pix !=
nullptr) {
231 Pix* transformed = pixProjective(*pix, im_coeffs, incolor);
232 if (transformed ==
nullptr) {
233 tprintf(
"Projective transformation failed!!\n");
239 if (boxes !=
nullptr) {
241 for (
int b = 0; b < boxes->
size(); ++b) {
243 const TBOX& box = (*boxes)[b];
244 projectiveXformSampledPt(box_coeffs, box.
left(), height - box.
top(), &x1,
246 projectiveXformSampledPt(box_coeffs, box.
right(), height - box.
bottom(),
248 TBOX new_box1(x1, height - y2, x2, height - y1);
249 projectiveXformSampledPt(box_coeffs, box.
left(), height - box.
bottom(),
251 projectiveXformSampledPt(box_coeffs, box.
right(), height - box.
top(), &x2,
253 TBOX new_box2(x1, height - y1, x2, height - y2);
254 (*boxes)[b] = new_box1.bounding_union(new_box2);
266 float** im_coeffs,
float** box_coeffs) {
268 Pta* src_pts = ptaCreate(4);
269 ptaAddPt(src_pts, 0.0f, 0.0f);
270 ptaAddPt(src_pts, width, 0.0f);
271 ptaAddPt(src_pts, width, height);
272 ptaAddPt(src_pts, 0.0f, height);
281 shear = shear >= 0.0 ? shear * shear : -shear * shear;
283 if (shear < -factors[
FN_X0]) shear = -factors[
FN_X0];
284 if (shear > factors[
FN_X1]) shear = factors[
FN_X1];
287 factors[i] = fabs(randomizer->
SignedRand(1.0));
289 factors[i] *= 5.0 / 8.0;
292 factors[i] *= factors[i];
296 Pta* dest_pts = ptaCreate(4);
297 ptaAddPt(dest_pts, factors[
FN_X0] * width, factors[
FN_Y0] * height);
298 ptaAddPt(dest_pts, (1.0f - factors[
FN_X1]) * width, factors[
FN_Y1] * height);
299 ptaAddPt(dest_pts, (1.0f - factors[
FN_X1] + shear) * width,
300 (1 - factors[
FN_Y2]) * height);
301 ptaAddPt(dest_pts, (factors[
FN_X0] + shear) * width,
302 (1 - factors[
FN_Y3]) * height);
303 getProjectiveXformCoeffs(dest_pts, src_pts, im_coeffs);
304 getProjectiveXformCoeffs(src_pts, dest_pts, box_coeffs);
305 ptaDestroy(&src_pts);
306 ptaDestroy(&dest_pts);
307 return factors[
FN_INCOLOR] > 0.5f ? L_BRING_IN_WHITE : L_BRING_IN_BLACK;
const int kExposureFactor
Pix * PrepareDistortedPix(const Pix *pix, bool perspective, bool invert, bool white_noise, bool smooth_noise, bool blur, int box_reduction, TRand *randomizer, GenericVector< TBOX > *boxes)
Pix * DegradeImage(Pix *input, int exposure, TRand *randomizer, float *rotation)
void GeneratePerspectiveDistortion(int width, int height, TRand *randomizer, Pix **pix, GenericVector< TBOX > *boxes)
DLLSYM void tprintf(const char *format,...)
double SignedRand(double range)
int ProjectiveCoeffs(int width, int height, TRand *randomizer, float **im_coeffs, float **box_coeffs)
const float kRotationRange