In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian introduces in detail for you "C++ opencv image processing how to achieve picture geometric transformation", the details are detailed, the steps are clear, and the details are handled properly. I hope this article "C++ opencv image processing how to achieve picture geometric transformation" can help you solve your doubts.
Brief introduction
The geometric transformation of the image does not change the pixel value of the image, but changes the geometric position of the pixel. According to the nature of the transformation, the geometric transformation of the image includes the position transformation of the image (translation, mirror image, rotation), the shape transformation of the image (magnification, reduction, miscut) and other basic transformations, as well as the composite transformation of the image.
I. Image translation
Image translation is to move all the points in an image horizontally and vertically according to the specified amount of translation, and the translated image is the same as the original image. Every point on the translated image can find the corresponding point in the original image. The image is made up of pixels. It is assumed that the original pixel coordinates are (x0jiny0), and then the coordinates of the translation amount (△ x, △ y) are changed to (x1jiny1).
It can be expressed by mathematics: x1mathematics x0 + △ xrecoery y1fuy0 + △ xy.
Translation transformation is divided into two types, one is image size change, so that part of the final original image is not in the image, and the other is image size change, so that the content of the original image can be preserved.
1. Image translation code (does not change image size) # include#includeusing namespace std;using namespace cv;int main () {Mat img1; img1 = imread ("cat 1.jpg"); imshow ("original image", img1); int r = img1.rows; int c = img1.cols; int x0 = 100; int y0 = 100; Mat img2 (img1.size (), img1.type ()) For (int I = 0; I
< r; i++) { for (int j = 0; j < c; j++) { int x = j - x0; int y = i - y0; if (x >= 0 & y > = 0 & x
< c&&y < r) { img2.at(i, j) = img1.ptr(y)[x]; } } } imshow("不改变图像大小", img2); waitKey(0);} 效果如下: 2.图像平移代码 (改变图像大小) 代码如下(示例): int main(){ Mat img1; img1 = imread("猫1.jpg"); imshow("原图", img1); int x0 = 100; int y0 = 100; int r = img1.rows + y0; int c = img1.cols + x0; Mat img2(r,c, img1.type()); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { int x = j - x0; int y = i - y0; if (x >= 0 & & y > = 0 & & x < cased images < r) {img2.at (I, j) = img1.ptr (y) [x];} imshow ("resize the image", img2); waitKey (0);}
The effect is as follows:
2. Image rotation
Image rotation is a very important part of digital image processing, and it is one of the techniques of image geometric transformation. Generally, the rotation of the image is the position transformation of the image, but after rotation, the size of the image will generally change. In the image rotation transformation, the image transferred out of the display area can be truncated, or the image range can be expanded to display the image used.
1. Image rotation function
Opencv provides the getRotationMatrix2D function to achieve image rotation, which is used to calculate the rotation matrix.
Mat getRotationMatrix2D (Point2f center, double angle, double scale) center rotation center point angle rotation angle scale image scaling factor 2. Affine transformation function
After calculating the rotation matrix, the rotation needs to be applied to the output of the affine transformation, and the affine transformation function is warpAffine.
Void warpAffine (InputArray src, OutputArray dst, InputArray M, Size dsize, int flags = INTER_LINEAR, int borderMode = BORDER_CONSTANT, const Scalar& borderValue = Scalar ()) Src input dst output M transform matrix Size size flags interpolation algorithm identifier borderMode boundary pixel mode borderValue boundary value 3. Code int main () {Mat img1; img1 = imread ("cat 1.jpg"); imshow ("original", img1); Point center (img1.cols / 2, img1.rows / 2); Mat m = getRotationMatrix2D (center, 30,0.5); Mat img2; warpAffine (img1, img2, m, img1.size ()); imshow ("rotation", img2); waitKey (0);}
The effect is as follows:
III. Image scaling
Image scaling is a value that scales a given image by fx times in the x-axis direction and fy times in the y-axis direction.
1. Image scaling function void resize (InputArray src, OutputArray dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR); src input dst output dsize size fx in x-axis scale fy in y-axis scale interpolation interpolation mode 2. Image reduction code int main () {Mat img1; img1 = imread ("cat 1.jpg"); imshow ("original image", img1); Mat img2; resize (img1, img2, Size (img1.cols / 2, img1.rows / 2)); imshow ("zoom out", img2); waitKey (0);}
The effect is as follows:
3. Image magnification code int main () {Mat img1; img1 = imread ("cat 1.jpg"); imshow ("original image", img1); Mat img2; resize (img1, img2, Size (img1.cols * 2, img1.rows * 2)); imshow ("magnification", img2); waitKey (0);}
The effect is as follows:
Read here, the "C++ opencv image processing how to achieve picture geometry transformation" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.