Ask Your Question
0

How to do shear transformation

asked 2013-09-24 05:37:46 -0500

123ezone gravatar image

updated 2018-01-27 06:22:40 -0500

image description

I'm trying to remove slant of cursive text by using shearing transformation. Can someone tell me where I can find any tutorials regarding shear transformation in opencv or solution for that. Thank You Very much !

edit retag flag offensive close merge delete

Comments

1

This stackoverflow topic covers all the basics that you need for shearing.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-24 06:06:04 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-09-24 09:02:23 -0500

theodore gravatar image

updated 2013-09-24 09:03:13 -0500

regarding to the theory that @StevenPuttermans posted through the stackoverflow website here is a code example, without using any predefined function from opencv but by accessing raw pixel values:

#include <iostream>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <math.h>
#include <vector>

using namespace std;
using namespace cv;

int main()
{
  Mat src = imread("baboon.jpg", 0);

  if(src.empty())
    cerr << "Error: Loading image" << endl;

  int r1, c1; // tranformed point
  int rows, cols; // original image rows and columns
  rows = src.rows;
  cols = src.cols;

  float Bx = 0.5; // amount of shearing in x-axis
  float By = 0; // amount of shearing in y-axis

  int maxXOffset = abs(cols * Bx);
  int maxYOffset = abs(rows * By);

  Mat out = Mat::ones(src.rows+maxYOffset, src.cols+maxXOffset, src.type()); // create output image to be the same as the source

  for(int r = 0; r < out.rows; r++) // loop through the image
    {
      for(int c = 0; c < out.cols; c++)
        {
          r1 = r + c * By - maxYOffset; // map old point to new
          c1 = r * Bx + c - maxXOffset;

          if(r1 >= 0 && r1 <= rows && c1 >= 0 && c1 <= cols) // check if the point is within the boundaries
            {
              out.at<uchar>(r, c) = src.at<uchar>(r1, c1); // set value
            }

        }
    }

  namedWindow("Source image", CV_WINDOW_AUTOSIZE);
  namedWindow("Rotated image", CV_WINDOW_AUTOSIZE);
  imshow("Source image", src);
  imshow("Rotated image", out);

  waitKey(0);

  return 0;
}
edit flag offensive delete link more

Comments

Is there a way to convert that code to python ? :D

Caseguy gravatar imageCaseguy ( 2018-10-18 01:19:48 -0500 )edit

Question Tools

Stats

Asked: 2013-09-24 05:37:46 -0500

Seen: 4,485 times

Last updated: Jan 27 '18