回転せずに画像を回転(ポイント操作)



Image Rotation Without Imrotate



回転せずに画像を回転(ポイント操作)

The imrotate function is not used to achieve image rotation, the principle of which is to use point operation transformation to achieve. In general, when we realize image rotation, we first think of translating the center of rotation of the image to the origin of the upper left corner. After rotation and translation, we can get the rotated image. However, if we directly translate, rotate, and translate the original image, the new image is problematic. There will be many points in the new image that have no value, making the image like a honeycomb. Here we need to use reverse mapping, nearest neighbor interpolation (or bilinear interpolation) method to solve. Inverse mapping, nearest neighbor interpolation (or bilinear interpolation) (reference materials, self-understand): [https://blog.csdn.net/lkj345/article/details/50555870][1] [http://www.cnblogs.com/mlv5/archive/2012/02/02/2336321.html][2] [https://blog.csdn.net/glorydream2015/article/details/44873703][3] [https://www.cnblogs.com/linkr/p/3630902.html][4]

私のコードを添付してください:

% Image rotation
  1. clear
  2. clc
  3. src = imread('1.png')
  4. theta = 30
  5. angle = 30 * pi / 180
  6. figure
  7. imshow(src)
  8. % Original picturey xcorrespondingr c, After calculating the rotation angle of the new imager_new c_new, The angle will be negative when it is obtuse, so you need to addabs
  9. [r,c,k] = size(src)
  10. r_new = round(abs(r*cos(angle)) + abs(c*sin(angle)))
  11. c_new = round(abs(r*sin(angle)) + abs(c*cos(angle)))
  12. dst = zeros(r_new,c_new)% Construct a new image
  13. dst1 = zeros(r_new,c_new)% Construct a new image
  14. % Reverse mapping, the image is regarded as the translation, rotation, and translation of the new image to get the original image
  15. % Instead of getting a new image from the original image translation rotation translation, this can solve the problem that some points are mapped to empty
  16. translation1 = [1 0 -c_new/20 1 -r_new/20 0 1]% The center point of the new image is shifted to the origin in the upper left corner
  17. rotate = [cos(angle) -sin(angle) 0sin(angle) cos(angle) 00 0 1]% Spinangleangle
  18. translation2 = [1 0 c/20 1 r/20 0 1]% Pan back from origin
  19. % Reverse mapping Match from each pixel of the new image
  20. for i = 1:r_new
  21. for j = 1:c_new
  22. % Is mapped from the new image to the original image. If it is not in the original image, the new image is0, If it is, assign the nearest value to the point
  23. P = translation2 * (rotate * (translation1* [ji1] ))%P(1)forxValue of P(2)foryValue of
  24. if(round(P(1))>0 && round(P(1))<c && round(P(2))>0 && round(P(2))<r)
  25. dst(i,j,1) = src(round(P(2)),round(P(1)),1)%takesrcNearest value of
  26. dst(i,j,2) = src(round(P(2)),round(P(1)),2)
  27. dst(i,j,3) = src(round(P(2)),round(P(1)),3)
  28. else
  29. dst(i,j) = 0
  30. end
  31. end
  32. end
  33. figure
  34. imshow(dst/256)
  35. title('Nearest Interpolation')
  36. % Bilinear interpolation algorithm
  37. for i = 1:r_new
  38. for j = 1:c_new
  39. % Is mapped from the new image to the original image. If it is not in the original image, the new image is0, If it is, assign the nearest value to the point
  40. P = translation2 * (rotate * (translation1* [ji1] ))%P(1)forxValue of P(2)foryValue of
  41. x = P(1)
  42. y = P(2)
  43. % Four points for bilinear interpolation
  44. x_top = ceil(x)
  45. x_bottom = floor(x)
  46. y_top = ceil(y)
  47. y_bottom = floor(y)
  48. % Judgment conditions change accordingly
  49. if(y_top < r && y_bottom >0 && x_bottom > 0 && x_top <c)
  50. %m(i,j) = K(round(P(2,1)),round(P(1,1)))%yx
  51. dst1(i,j,1) = src(y_bottom,x_bottom,1)*(x_top - x)*(y_top - y) + src(y_top,x_bottom,1)*(x_top - x)*(y - y_bottom) + src(y_bottom,x_top,1)*(x - x_bottom)*(y_top - y) + src(y_top,x_top,1)*(x - x_bottom)*(y - y_bottom)
  52. dst1(i,j,2) = src(y_bottom,x_bottom,2)*(x_top - x)*(y_top - y) + src(y_top,x_bottom,2)*(x_top - x)*(y - y_bottom) + src(y_bottom,x_top,2)*(x - x_bottom)*(y_top - y) + src(y_top,x_top,2)*(x - x_bottom)*(y - y_bottom)
  53. dst1(i,j,3) = src(y_bottom,x_bottom,3)*(x_top - x)*(y_top - y) + src(y_top,x_bottom,3)*(x_top - x)*(y - y_bottom) + src(y_bottom,x_top,3)*(x - x_bottom)*(y_top - y) + src(y_top,x_top,3)*(x - x_bottom)*(y - y_bottom)
  54. else
  55. dst1(i,j) = 0
  56. end
  57. end
  58. end
  59. figure
  60. imshow(dst1/256)
  61. title('Bilinear interpolation')
  62. |_+_|