Image resizing, IndexError: list assignment index out of range
Image resizing, IndexError: list assignment index out of range
我的任务是在不使用外部库函数的情况下缩放/调整图像大小,需要自己创建算法,搜索过,但我在不同论坛上找到的代码没有用,但我有想出一个我认为可行的代码,但我在旁注中得到索引错误我还需要进行与缩放图像相关的前向和后向映射。
我的代码可以在下面看到
import cv2 as cv
img = cv.imread('Scale.jpg', 1)
cv.imshow('unscaled', img)
h,w = img.shape[:2]
print(h)
print(w)
def resizePixels(pixels,w1,h1,w2,h2) :
retval = [w2,h2]
# EDIT: added +1 to remedy an early rounding problem
x_ratio = (int)((w1<<16)/w2) +1
y_ratio = (int)((h1<<16)/h2) +1
#int x_ratio = (int)((w1<<16)/w2)
#int y_ratio = (int)((h1<<16)/h2)
#two = int(x2,y2)
for i in range (h2):
i += 1
for j in range(w2):
j += 1
x2 = ((j*x_ratio)>>16)
y2 = ((i*y_ratio)>>16)
retval[(i*w2)+j] = pixels[(y2*w1)+x2]
return retval;
dst = resizePixels(img,h,w,300,300)
#cv.imshow('Resize',dst)
cv.waitKey(0)
编辑:这是我收到的回溯
Traceback (most recent call last):
File "C:/Users/Asus/PycharmProjects/Scaling/Scaling.py", line 27, in
<module>
dst = resizePixels(img,h,w,300,300)
File "C:/Users/Asus/PycharmProjects/Scaling/Scaling.py", line 23, in
resizePixels
retval[(i*w2)+j] = pixels[(y2*w1)+x2]
IndexError: list assignment index out of range
The picture I use to scale
我对您的代码做了一些小改动,它可以正常工作。阅读我在代码中的评论以获取更多详细信息。
import cv2
import numpy as np
img = cv2.imread('1.png', 0)
cv2.imshow('unscaled', img)
h,w = img.shape[:2]
print(h)
print(w)
def resizePixels(pixels,w1,h1,w2,h2):
retval = []
# EDIT: added +1 to remedy an early rounding problem
x_ratio = (int)((w1<<16)/w2) +1
print(x_ratio)
y_ratio = (int)((h1<<16)/h2) +1
print(y_ratio)
#int x_ratio = (int)((w1<<16)/w2)
#int y_ratio = (int)((h1<<16)/h2)
#two = int(x2,y2)
for i in range(h2):
# no need to have this: i += 1
for j in range(w2):
# no need to have this too: j += 1
x2 = ((j*x_ratio)>>16)
y2 = ((i*y_ratio)>>16)
#add pixel values from original image to an array
retval.append(pixels[y2,x2])
return retval;
ret = resizePixels(img,w,h,300,300)
# reshape the array to get the resize image
dst = np.reshape(ret,(300,300))
cv2.imshow('Resize',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
我的任务是在不使用外部库函数的情况下缩放/调整图像大小,需要自己创建算法,搜索过,但我在不同论坛上找到的代码没有用,但我有想出一个我认为可行的代码,但我在旁注中得到索引错误我还需要进行与缩放图像相关的前向和后向映射。
我的代码可以在下面看到
import cv2 as cv
img = cv.imread('Scale.jpg', 1)
cv.imshow('unscaled', img)
h,w = img.shape[:2]
print(h)
print(w)
def resizePixels(pixels,w1,h1,w2,h2) :
retval = [w2,h2]
# EDIT: added +1 to remedy an early rounding problem
x_ratio = (int)((w1<<16)/w2) +1
y_ratio = (int)((h1<<16)/h2) +1
#int x_ratio = (int)((w1<<16)/w2)
#int y_ratio = (int)((h1<<16)/h2)
#two = int(x2,y2)
for i in range (h2):
i += 1
for j in range(w2):
j += 1
x2 = ((j*x_ratio)>>16)
y2 = ((i*y_ratio)>>16)
retval[(i*w2)+j] = pixels[(y2*w1)+x2]
return retval;
dst = resizePixels(img,h,w,300,300)
#cv.imshow('Resize',dst)
cv.waitKey(0)
编辑:这是我收到的回溯
Traceback (most recent call last):
File "C:/Users/Asus/PycharmProjects/Scaling/Scaling.py", line 27, in
<module>
dst = resizePixels(img,h,w,300,300)
File "C:/Users/Asus/PycharmProjects/Scaling/Scaling.py", line 23, in
resizePixels
retval[(i*w2)+j] = pixels[(y2*w1)+x2]
IndexError: list assignment index out of range
The picture I use to scale
我对您的代码做了一些小改动,它可以正常工作。阅读我在代码中的评论以获取更多详细信息。
import cv2
import numpy as np
img = cv2.imread('1.png', 0)
cv2.imshow('unscaled', img)
h,w = img.shape[:2]
print(h)
print(w)
def resizePixels(pixels,w1,h1,w2,h2):
retval = []
# EDIT: added +1 to remedy an early rounding problem
x_ratio = (int)((w1<<16)/w2) +1
print(x_ratio)
y_ratio = (int)((h1<<16)/h2) +1
print(y_ratio)
#int x_ratio = (int)((w1<<16)/w2)
#int y_ratio = (int)((h1<<16)/h2)
#two = int(x2,y2)
for i in range(h2):
# no need to have this: i += 1
for j in range(w2):
# no need to have this too: j += 1
x2 = ((j*x_ratio)>>16)
y2 = ((i*y_ratio)>>16)
#add pixel values from original image to an array
retval.append(pixels[y2,x2])
return retval;
ret = resizePixels(img,w,h,300,300)
# reshape the array to get the resize image
dst = np.reshape(ret,(300,300))
cv2.imshow('Resize',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()