import numpy as np
import cv2
from cvui import *
WINDOW_NAME = 'CVUI Hello World!'
def main():
src = cv2.imread("C:\\Users\\chechaojun\\Desktop\\2.jpg")
INTER_LINEAR = src.copy()
INTER_LINEAR_EXACT = src.copy()
INTER_NEAREST = src.copy()
INTER_CUBIC = src.copy()
INTER_AREA = src.copy()
INTER_LANCZOS4 = src.copy()
INTER_BITS = src.copy()
INTER_BITS2 = src.copy()
# Init cvui and tell it to create a OpenCV window, i.e. cv2.namedWindow(WINDOW_NAME).
cvui.init(WINDOW_NAME)
floatValue = [2.]
floatValueLast = [1.]
max_ = 3
min_= 0.5
frame_width = src.shape[1] * max_ * 4
frame_hight = src.shape[0] + src.shape[0] * max_ * 2 + 100
frame = np.zeros((frame_hight, frame_width, 3), np.uint8)
while (True):
frame[:] = (49, 52, 49)
cvui.trackbar(frame, src.shape[1] + 20, src.shape[0] / 3, 300, floatValue, min_, max_)
cvui.image(frame, 0, 0, src)
if(floatValueLast[0] != floatValue[0]):
floatValueLast[0] = floatValue[0]
fx = floatValue[0]
fy = floatValue[0]
INTER_LINEAR = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_LINEAR )
INTER_LINEAR_EXACT = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_LINEAR_EXACT)
INTER_NEAREST = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_NEAREST )
INTER_CUBIC = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_CUBIC )
INTER_AREA = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_AREA )
INTER_LANCZOS4 = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_LANCZOS4 )
INTER_BITS = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_BITS)
INTER_BITS2 = cv2.resize(src,(0, 0), fx = fx, fy = fy, interpolation=cv2.INTER_BITS2)
y_text= src.shape[0] + 10
x = 0
y = y_text + 20
cvui.text(frame, x, y_text, 'INTER_LINEAR')
cvui.image(frame, x, y, INTER_LINEAR)
x += src.shape[1] * max_
cvui.text(frame, x, y_text, 'INTER_LINEAR_EXACT')
cvui.image(frame, x, y, INTER_LINEAR_EXACT)
x += src.shape[1] * max_
cvui.text(frame, x, y_text, 'INTER_NEAREST')
cvui.image(frame, x, y, INTER_NEAREST)
x += src.shape[1] * max_
cvui.text(frame, x, y_text, 'INTER_CUBIC')
cvui.image(frame, x, y, INTER_CUBIC)
y_text += src.shape[0] * max_ + 20
x = 0
y = y_text + 20
cvui.text(frame, x, y_text, 'INTER_AREA')
cvui.image(frame, 0, y, INTER_AREA)
x += src.shape[1] * max_
cvui.text(frame, x, y_text, 'INTER_LANCZOS4')
cvui.image(frame, x, y, INTER_LANCZOS4)
x += src.shape[1] * max_
cvui.text(frame, x, y_text, 'INTER_BITS')
cvui.image(frame, x, y, INTER_BITS)
x += src.shape[1] * max_
cvui.text(frame, x, y_text, 'INTER_BITS2')
cvui.image(frame, x, y, INTER_BITS2)
cvui.update()
cv2.imshow(WINDOW_NAME, frame)
if cv2.waitKey(20) == 27:
break
if __name__ == '__main__':
main()
opencv 可选的插值方式:
- INTER_LINEAR 线性插值
- INTER_LINEAR_EXACT
- INTER_NEAREST 最近邻插值
- INTER_AREA 区域插值(利用像素区域关系的重采样插值)
- INTER_CUBIC 三次样条插值(超过4x4像素领域内的双三次插值)
- INTER_LANCZOS4 Lanczos插值(超过8x8像素邻域的Lanczos插值)
- INTER_BITS
- INTER_BITS2
缩小图像一般情况下用INTER_AREA来插值,而若要放大图像,一般情况下用INTER_CUBIC(效率不高,不推荐)或INTER_LINEAR(效率高推荐)
INTER_NEAREST 最近邻插值
从显示效果上看,最近邻插值,是所有插值算法中速度最快的,但不论是放大还是缩小,都有锯齿波
算法:
选取离目标点最近的点作为新的插入点,计算公式为
插值后的边缘效果:由于是以最近的点作为新的插入点,因此边缘不会出现缓慢那的渐慢过度区域,这也导致放大的图像容易出现锯齿现象。
INTER_LINEAR 线性插值
插值后的效果可以有效的避免出现锯齿的现象
算法原理:
线性插值是以距离为权重的一种插值方式。