1. import numpy as np
  2. import cv2
  3. from cvui import *
  4. WINDOW_NAME = 'CVUI Hello World!'
  5. def main():
  6. src = cv2.imread("C:\\Users\\chechaojun\\Desktop\\2.jpg")
  7. INTER_LINEAR = src.copy()
  8. INTER_LINEAR_EXACT = src.copy()
  9. INTER_NEAREST = src.copy()
  10. INTER_CUBIC = src.copy()
  11. INTER_AREA = src.copy()
  12. INTER_LANCZOS4 = src.copy()
  13. INTER_BITS = src.copy()
  14. INTER_BITS2 = src.copy()
  15. # Init cvui and tell it to create a OpenCV window, i.e. cv2.namedWindow(WINDOW_NAME).
  16. cvui.init(WINDOW_NAME)
  17. floatValue = [2.]
  18. floatValueLast = [1.]
  19. max_ = 3
  20. min_= 0.5
  21. frame_width = src.shape[1] * max_ * 4
  22. frame_hight = src.shape[0] + src.shape[0] * max_ * 2 + 100
  23. frame = np.zeros((frame_hight, frame_width, 3), np.uint8)
  24. while (True):
  25. frame[:] = (49, 52, 49)
  26. cvui.trackbar(frame, src.shape[1] + 20, src.shape[0] / 3, 300, floatValue, min_, max_)
  27. cvui.image(frame, 0, 0, src)
  28. if(floatValueLast[0] != floatValue[0]):
  29. floatValueLast[0] = floatValue[0]
  30. fx = floatValue[0]
  31. fy = floatValue[0]
  32. INTER_LINEAR = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_LINEAR )
  33. INTER_LINEAR_EXACT = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_LINEAR_EXACT)
  34. INTER_NEAREST = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_NEAREST )
  35. INTER_CUBIC = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_CUBIC )
  36. INTER_AREA = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_AREA )
  37. INTER_LANCZOS4 = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_LANCZOS4 )
  38. INTER_BITS = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_BITS)
  39. INTER_BITS2 = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_BITS2)
  40. y_text= src.shape[0] + 10
  41. x = 0
  42. y = y_text + 20
  43. cvui.text(frame, x, y_text, 'INTER_LINEAR')
  44. cvui.image(frame, x, y, INTER_LINEAR)
  45. x += src.shape[1] * max_
  46. cvui.text(frame, x, y_text, 'INTER_LINEAR_EXACT')
  47. cvui.image(frame, x, y, INTER_LINEAR_EXACT)
  48. x += src.shape[1] * max_
  49. cvui.text(frame, x, y_text, 'INTER_NEAREST')
  50. cvui.image(frame, x, y, INTER_NEAREST)
  51. x += src.shape[1] * max_
  52. cvui.text(frame, x, y_text, 'INTER_CUBIC')
  53. cvui.image(frame, x, y, INTER_CUBIC)
  54. y_text += src.shape[0] * max_ + 20
  55. x = 0
  56. y = y_text + 20
  57. cvui.text(frame, x, y_text, 'INTER_AREA')
  58. cvui.image(frame, 0, y, INTER_AREA)
  59. x += src.shape[1] * max_
  60. cvui.text(frame, x, y_text, 'INTER_LANCZOS4')
  61. cvui.image(frame, x, y, INTER_LANCZOS4)
  62. x += src.shape[1] * max_
  63. cvui.text(frame, x, y_text, 'INTER_BITS')
  64. cvui.image(frame, x, y, INTER_BITS)
  65. x += src.shape[1] * max_
  66. cvui.text(frame, x, y_text, 'INTER_BITS2')
  67. cvui.image(frame, x, y, INTER_BITS2)
  68. cvui.update()
  69. cv2.imshow(WINDOW_NAME, frame)
  70. if cv2.waitKey(20) == 27:
  71. break
  72. if __name__ == '__main__':
  73. main()

opencv 可选的插值方式:

  1. INTER_LINEAR 线性插值
  2. INTER_LINEAR_EXACT
  3. INTER_NEAREST 最近邻插值
  4. INTER_AREA 区域插值(利用像素区域关系的重采样插值)
  5. INTER_CUBIC 三次样条插值(超过4x4像素领域内的双三次插值)
  6. INTER_LANCZOS4 Lanczos插值(超过8x8像素邻域的Lanczos插值)
  7. INTER_BITS
  8. INTER_BITS2

缩小图像一般情况下用INTER_AREA来插值,而若要放大图像,一般情况下用INTER_CUBIC(效率不高,不推荐)或INTER_LINEAR(效率高推荐)
opencv resize - 图1

INTER_NEAREST 最近邻插值

从显示效果上看,最近邻插值,是所有插值算法中速度最快的,但不论是放大还是缩小,都有锯齿波
算法:
选取离目标点最近的点作为新的插入点,计算公式为
opencv resize - 图2
插值后的边缘效果:由于是以最近的点作为新的插入点,因此边缘不会出现缓慢那的渐慢过度区域,这也导致放大的图像容易出现锯齿现象。

INTER_LINEAR 线性插值

插值后的效果可以有效的避免出现锯齿的现象
算法原理:
线性插值是以距离为权重的一种插值方式。

opencv resize - 图3

INTER_AREA 区域插值