Skip to content

Commit

Permalink
Merge pull request #2259 from Shreeshrii/distort
Browse files Browse the repository at this point in the history
implement PrepareDistortedPix as part of DegradeImage
  • Loading branch information
zdenop authored Feb 22, 2019
2 parents e250f34 + 2aded47 commit c02f5e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/training/degradeimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Pix* DegradeImage(Pix* input, int exposure, TRand* randomizer,
input = pix;
int width = pixGetWidth(input);
int height = pixGetHeight(input);

if (exposure >= 2) {
// An erosion simulates the spreading darkening of a dark copy.
// This is backwards to binary morphology,
Expand Down Expand Up @@ -167,6 +168,12 @@ Pix* DegradeImage(Pix* input, int exposure, TRand* randomizer,
}
data += input->wpl;
}

// pix = input;
// input = PrepareDistortedPix(pix, false, true, true, true, true,
// 1, randomizer, nullptr);
// pixDestroy(&pix);

return input;
}

Expand All @@ -181,8 +188,6 @@ Pix* PrepareDistortedPix(const Pix* pix, bool perspective, bool invert,
GenericVector<TBOX>* boxes) {
Pix* distorted = pixCopy(nullptr, const_cast<Pix*>(pix));
// Things to do to synthetic training data.
if (invert && randomizer->SignedRand(1.0) < 0)
pixInvert(distorted, distorted);
if ((white_noise || smooth_noise) && randomizer->SignedRand(1.0) > 0.0) {
// TODO(rays) Cook noise in a more thread-safe manner than rand().
// Attempt to make the sequences reproducible.
Expand Down Expand Up @@ -210,6 +215,8 @@ Pix* PrepareDistortedPix(const Pix* pix, bool perspective, bool invert,
(*boxes)[b].set_right((*boxes)[b].left() + 1);
}
}
if (invert && randomizer->SignedRand(1.0) < -0.9)
pixInvert(distorted, distorted);
return distorted;
}

Expand Down
28 changes: 28 additions & 0 deletions src/training/text2image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ BOOL_PARAM_FLAG(rotate_image, true, "Rotate the image in a random way.");
// Degradation to apply to the image.
INT_PARAM_FLAG(exposure, 0, "Exposure level in photocopier");

// Distort the rendered image by various means according to the bool flags.
BOOL_PARAM_FLAG(distort_image, false,
"Degrade rendered image with noise, blur, invert.");

// Distortion to apply to the image.
BOOL_PARAM_FLAG(invert, true, "Invert the image");

// Distortion to apply to the image.
BOOL_PARAM_FLAG(white_noise, true, "Add Gaussian Noise");

// Distortion to apply to the image.
BOOL_PARAM_FLAG(smooth_noise, true, "Smoothen Noise");

// Distortion to apply to the image.
BOOL_PARAM_FLAG(blur, true, "Blur the image");

// Distortion to apply to the image.
//BOOL_PARAM_FLAG(perspective, false, "Generate Perspective Distortion");

// Distortion to apply to the image.
//INT_PARAM_FLAG(box_reduction, 0, "Integer reduction factor box_scale");

// Output image resolution.
INT_PARAM_FLAG(resolution, 300, "Pixels per inch");

Expand Down Expand Up @@ -619,6 +641,12 @@ static int Main() {
pix = DegradeImage(pix, FLAGS_exposure, &randomizer,
FLAGS_rotate_image ? &rotation : nullptr);
}
if (FLAGS_distort_image) {
//Todo: perspective is set to false and box_reduction to 1.
pix = PrepareDistortedPix(pix, false, FLAGS_invert,
FLAGS_white_noise, FLAGS_smooth_noise, FLAGS_blur,
1, &randomizer, nullptr);
}
render.RotatePageBoxes(rotation);

if (pass == 0) {
Expand Down

0 comments on commit c02f5e9

Please sign in to comment.