user18386987
user18386987

Reputation:

I can not display the whole image in 4 slices

I slice the image into four parts using the mentioned code below and display it but I am not able to see the full image in the last slice. Seems like a part is missing. Can't seem to find the mistake. For the specific code I am using the dimensions of the image 1600 x 872.

n = 4
widthDiv = int(width/4)
widthDiv1 = widthDiv
heightDiv = int(height/4)
heightDiv1 = heightDiv
initialw = 0
initialh = 0
for i in range(0,n):
    for x in range(0, height):
        if (height >= heightDiv):
            for y in range(0, width):
                if (width >= widthDiv):
                    value = oriImg[initialw:widthDiv, initialh:heightDiv]
                    arr.append(value)
                    np.shape(value)
                    cv2.imshow(f" part {i}", arr[i])
                    cv2.waitKey()
                    widthDiv = widthDiv + widthDiv1
                    heightDiv = heightDiv + heightDiv1
                    initialw = initialw + initialw
                    initialh = initialh + initialh
                    i=i+1

Upvotes: 0

Views: 44

Answers (1)

Alexander
Alexander

Reputation: 17335

You have your array indices backwards when you assign to the value variable. I made a note in the code below as well.

Try this:

n = 4
widthDiv = int(width/4)
widthDiv1 = widthDiv
heightDiv = int(height/4)
heightDiv1 = heightDiv
initialw = 0
initialh = 0
for i in range(0,n):
    for x in range(0, height):
        if (height >= heightDiv):
            for y in range(0, width):
                if (width >= widthDiv):
                    # this next line is the change
                    value = oriImg[initialh:heightDiv, initialw:widthDiv]  
                    arr.append(value)
                    np.shape(value)
                    cv.imshow(f" part {i}", arr[i])
                    cv.waitKey()
                    widthDiv = widthDiv + widthDiv1
                    heightDiv = heightDiv + heightDiv1
                    initialw = initialw + initialw
                    initialh = initialh + initialh
                    i=i+1

Upvotes: 2

Related Questions