'How to account for resizing of images in image shears
I am trying to create a program where the user enters an image and a transformation matrix and sees it applied to the image. All of the 2x2 matrix transformations I have tested so far work except for shears. This is because the image size is actually changed in the case of a shear (compared to a reflection/rotation etc.).
I believe I either need to resize the image itself after applying the transformation or the window, but I am unable to figure out how to do so. I tried utilizing the resize() and windowResize() functions, but was not able to accomplish what I wanted. How can I resize the image itself or the window (if needed) after applying a transformation matrix such as a shear to include the entirety of the output image in the window? My code so far is shown below:
int main()
{
// Query user for file input
std::cout << "Please enter an image file: ";
std::string fileName;
std::cin >> fileName;
std::ifstream inputFile(fileName);
// Check if file exists
if (!inputFile)
{
std::cout << "Error opening file";
return 1;
}
// Query user for 2x2 transformation matrix input
std::cout << "Enter the four values of your 2x2 transformation matrix"
<< " separated by spaces: ";
double a, b, c, d;
std::cin >> a >> b >> c >> d;
// Read in and display input image
Mat src = imread(fileName);
imshow("Input Image", src);
// Matrix to translate the image to OpenCV's origin
Mat originMat = (Mat_<double>(3, 3)
<< 1, 0, -(src.cols / 2), 0, 1, -(src.rows / 2), 0, 0, 1);
// Matrix to apply the given transformation matrix
Mat tMat = (Mat_<double>(3, 3) //
<< a, b, 0, c, d, 0, 0, 0, 1);
// Matrix to translate the image back to its original position
Mat tOriginal = (Mat_<double>(3, 3)
<< 1, 0, (src.cols / 2), 0, 1, (src.rows / 2), 0, 0, 1);
// Multiply matrices to make things simpler
// and preserve all aspects of the image
Mat compMat = tOriginal * (tMat * originMat);
// Apply the given transformation matrix to the image
warpPerspective(src, src, compMat, src.size(),
INTER_LINEAR, BORDER_CONSTANT, Scalar());
// Display the final output image and wait for input before closing window
Size size = Size(900, 900);
imshow("Output Image", src);
// This doesn't really work. It resizes the image to scale as well
// which defeats the whole purpose and resizing the image itself
// leaves some parts of it cut off
resizeWindow("Output Image", size);
waitKey();
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
