The last time I spoke about digital image processing, I will post another question today.
Geometric transform (test image: fig3.tif)
Develope geometric transform program that will rotate, translate, and scale an imageby specified amounts, using the nearest neighbor and bilinear interpolationmethods, respectively.
Background
In the process of spatial transformation of an image, a typical situation is that the image will be distorted when the image is enlarged or rotated. This is because there are some pixel locations not included in the transformed image. The method for processing this problem is called gray-level Interpolation of images. There are three common interpolation methods: Recent neighbor interpolation, bilinear interpolation, and double cubic Interpolation. Theoretically, the latest neighbor interpolation has the worst effect, and the double three interpolation has the best effect. The bilinear interpolation effect is between the two. However, for image interpolation that is not strictly required, bilinear interpolation is usually enough.
Recent domain algorithm Matlab code
SourcePic = imread ('fig3. tif '); % The following is a color image % [m, n, o] = size (sourcePic); % grayPic = rgb2gray (sourcePic); grayPic = sourcePic; [m, n] = size (grayPic); % ratio coefficient is 0.2-5.0 K = str2double (inputdlg ('enter ratio coefficient (0.2-5.0) ', 'enter ratio coefficient ', 1, {'0. 5'}); % verification range: if (K <0.2) & (K> 5.0) errordlg ('proportional coefficient is not within the range of 0.2-5.0 ', 'error '); error ('Enter the proportional coefficient (0.2-5.0) '); endfigure; imshow (grayPic); width = K * m; height = K * n; resultPic = uint8 (zeros (width, height); widthScale = m/width; heightScale = n/height; for x = 5: width-5 for y = 5: height-5 xx = x * widthScale; yy = y * heightScale; if (xx/double (uint16 (xx) = 1.0) & (yy/double (uint16 (yy) = 1.0) % if xx and yy is integer, then J (x, y) <-I (x, y) resultPic (x, y) = grayPic (int16 (xx), int16 (yy); else % xx or yy is not integer a = double (round (xx )); % (a, B) is the base-dot B = double (round (yy); resultPic (x, y) = grayPic (a, B ); % calculate J (x, y) end endfigure; rotate (resultPic,-20); imshow (resultPic );
Bilinear interpolation Matlab algorithm
SourcePic = imread ('fig3. tif '); % The following is a color image % [m, n, o] = size (sourcePic); % grayPic = rgb2gray (sourcePic); grayPic = sourcePic; [m, n] = size (grayPic); % ratio coefficient is 0.2-5.0 K = str2double (inputdlg ('enter ratio coefficient (0.2-5.0) ', 'enter ratio coefficient ', 1, {'0. 5'}); % verification range: if (K <0.2) or (K> 5.0) errordlg ('ratio not in the range of 0.2-5.0 ', 'error '); error ('Enter the proportional coefficient (0.2-5.0) '); endfigure; imshow (grayPic); % output image length/width = K * m; height = K * n; resultPic = uint8 (zeros (width, height); widthScale = n/width; heightScale = m/height; for x = 5: width-5 for y = 5: height-5 xx = x * widthScale; yy = y * heightScale; if (xx/double (uint16 (xx) = 1.0) & (yy/double (uint16 (yy) = 1.0) % if xx and yy is integer, then J (x, y) <-I (x, y) resultPic (x, y) = grayPic (int16 (xx), int16 (yy); else % xx or yy is not integer a = double (uint16 (xx )); % (a, B) is the base-dot B = double (uint16 (yy); x11 = double (grayPic (a, B )); % x11 <-I (a, B) x12 = double (grayPic (a, B + 1); % x12 <-I (a, B + 1) x21 = double (grayPic (a + 1, B); % x21 <-I (a + 1, B) x22 = double (grayPic (a + 1, B + 1); % x22 <-I (a + 1, B + 1) resultPic (x, y) = uint8 (B + 1-yy) * (xx-a) * x21 + (a + 1-xx) * x11) + (yy-B) * (xx-a) * x22 + (a + 1-xx) * x12); end endfigure; resultPic = imrotate (resultPic,-20); imshow (resultPic );
The effect is as follows:
Recently, the domain algorithm doubles and rotates 20 degrees clockwise.
The bilinear interpolation algorithm doubles in size and rotates 20 degrees clockwise.
Experience
This experiment shows that the image obtained by bilinear interpolation is better. This solution can avoid image blurring and block distortion that may occur when using the interpolation method in recent fields. However, there are also problems with bilinear interpolation. When the magnification is relatively high, the image distortion will be severe. In this case, we should consider using a higher-order interpolation algorithm.