How do i autosize an image so that i do not get IndexError: index 303 is out of bounds for axis 0 with size 303

asked 2018-10-28 09:15:54 -0600

Caseguy gravatar image

updated 2018-10-28 09:44:12 -0600

berak gravatar image

Whenever i'm trying to run the program i have made i get the indexError:

IndexError: index 303 is out of bounds for axis 0 with size 303

i can fix it and go into my np.zeros((rows,cols),3),np.uint8) and increas the values of the rows or cols to make the image fit, when tranformed. But is there any easier way to no do that an make a autosize function or something i've tried with namedWindow, but no luck so far.

# Important imports
import cv2
import numpy as np

# read image
img = cv2.imread("Mig.png", 0)
# image rows and columns
rows = img.shape[0]
cols = img.shape[1]
# Copys of image for shearing
imgCopy1 = np.zeros((rows+266, cols, 3), np.uint8)
imgCopy2 = np.zeros((rows+266, cols, 3), np.uint8)
imgCopy3 = np.zeros((rows, cols, 3), np.uint8)
imgCopy4 = np.zeros((rows, cols, 3), np.uint8)
# Shear factors
Bx = 1
By = 1

for i in range(0, rows):
    for j in range(0, cols):
        yOffSet = By * j
        k = img[i, j]
        imgCopy1[i + yOffSet, j] = k
for i in range(0,rows):
    for j in range(0,cols):
        yOffSet = By * j
        k = img[i,j]
        imgCopy2[i - yOffSet + cols,j] = k




cv2.imshow("normal", img)
cv2.imshow("shear", imgCopy1)
cv2.imshow("shear1", imgCopy2)
cv2.imwrite("sheardImg.png",imgCopy1)
cv2.waitKey()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

your algorithm is wrong : 0 < i + yOffSet < rows+266 but i in [ 0, 511 ] and yoffset in [ 0 , cols ] then if cols>266 there is a problem

LBerger gravatar imageLBerger ( 2018-10-28 11:47:36 -0600 )edit

It displays what i want, why is it wrong it displays the corret thing i need. I didn't quiet get what you meant sorry

Caseguy gravatar imageCaseguy ( 2018-10-28 12:45:16 -0600 )edit