Image Processing Algorithm

Source: Internet
Author: User

1. Image Translation


Image translation only changes the position of the image on the screen, and the image itself does not change.
Assume that the coordinates in the upper-left corner of the original image area are (x0, y0), and the lower-right coordinates are (x1, Y1), and the images are translated along X and Y axes Dx and Dy, respectively, then, the coordinates in the upper left corner of the new image are (x0 + dx, y0 + dy), and those in the lower right corner are (X1 + dx, Y1 + dy ). The coordinate translation and transformation formula is:

X' = x + dx
Y' = Y + dy

There are four steps to move an image on the screen:
(1) Save the original image to the buffer zone.
(2) erase the original image.
(3) Calculate the new coordinates after translation.
(4) re-display the original image at the new coordinate position.

The method used to erase the original image is the same as that used to erase the original image in the graphic transformation. in the implementation process, the original image is still erased by drawing XOR. For the calculation of the new coordinate value, the boundary condition should also be considered. Do not go beyond the allowed screen range after the image is translated. In addition, if the c Functions getimage () and putimage () are used to save and restore images, the image size cannot exceed 64 KB.

2. image upside down

Image inversion refers to displaying the defined image area up and down on the screen. After analyzing the image inversion process, we can find that the image information of each line remains unchanged, but changes the sequence of rows, switching the first line with the last n lines, the second line is exchanged with n-1 line ......, In this way, the image is reversed. You only need to use the line-based switching method to conveniently modify the buffer content and reverse the image. The basic steps are as follows:
(1) Use getimage () to save the original image and erase it.
(2) Calculate the Image Height, that is, the number of rows of height, calculate the image width, and calculate and save the information of a row of images.

Height = bottom-top + 1;
Width = right-left + 1;
Linebytes = (width + 7)/8*4;

(3) The linebuf in the row swap buffer is used to exchange information in the image memory buffer, that is, the first and last rows are exchanged, and the second and n-1 rows are exchanged ......, And so on until all the exchanges are completed.
(4) re-display the swap image buffer on the screen.

3. Image Transformation

Image conversion refers to displaying images in a specified area on the screen right and right. After analyzing the image transformation process, we can find that each line of image information is processed in the same way, and the row sequence does not change, only the pixel information of each row is reversed from left to right, thus realizing image transformation. Therefore, the image is transformed point-by-point by line.
First, for the (left, top) rectangular image in the upper left corner and (right, bottom) in the lower right corner, the new coordinates (x ′, y ') coordinate transformation formula:

X = right-x0 + left
Y' = y0

Based on the above formula, After calculating the new coordinates of each pixel, it is directly displayed on the corresponding position of the screen.
If the data is completely switched point by point, it is necessary to read the pixel value once to process a pixel, thus reducing the conversion speed. Because the pixels are stored sequentially on each bit, each read byte contains 8 pixel information, you only need to set different bit screen values bitmask, you can obtain the information of different pixels. Therefore, the line-by-line conversion method is used to convert 8 pixels each read to improve the conversion speed.

The basic steps for image transformation for an image in a rectangle are as follows:
(1) Use getimage () to save the image to the memory buffer and erase the original image.
(2) Calculate the Image Height, that is, the Row Height height and width, and calculate the number of bytes used to save the information of a row of images, linebytes. The calculation formula is as follows:

Height = bottom-top + 1;
Width = right-left + 1;
Linebyte = (width + 7)/8*4;

(3) image the image.
(4) release the memory image buffer.

4. Image Rotation

Image Rotation refers to the rotation of a defined image around a certain point to a certain angle in the clockwise or clockwise direction, usually refers to the rotation of the center around the image in the clockwise direction.
Assume that the upper left corner of the image is (left, top), and the lower right corner is (right, bottom) after the angle is rotated counterclockwise, the formula for calculating the new Coordinate Position (x', y') is as follows:

Xcenter = (right-left + 1)/2 + left;
Ycenter = (bottom-top + 1)/2 + top;
X' = (x0-xcenter) COS θ-(y0-ycenter) sin θ + xcenter;
Y' = (x0-xcenter) sin θ + (y0-ycenter) COS θ + ycenter;

Similar to image conversion, image rotation is also achieved through line-by-point conversion. The steps are as follows:
(1) Use getimage () to save the image to the memory buffer and erase the original image.
(2) Calculate the height, width, and width of the image, and the number of bytes occupied by storing a row of image information. The calculation formula is the same as that of image transformation.
(3) rotate the image line by line.
(4) release the memory image buffer.

It is worth noting that this method is not enough. For this reason, another method can be used: After processing in the image transform buffer, the transformed image will be displayed on the screen once. In this way, better display results can be achieved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.