I am comparing two slices of the same array. The two slices are the same, except they are offset in the [1] axis. I compare these two slices and return their minimum repeatedly in a while loop.
My expectation: The size of the arrays stay the same relative to each other, shrinking by the value of "offset"
What happens instead: One of the arrays shrinks in size relative to the other in the second iteration, shrinking by more than the value of "offset"
As I am using the same offset value to compare them, and as (x-y) - 0 = x - (0 + y), these values must be the same. For the first iteration, they are the same, but for the second iteration, they are not, as you can see from the printed output.
I thought that the problem might be related to pass-by-value issues, so I tried using copy.deepcopy, but this is not correcting the problem either. I also thought there might be a quirk in indexing overflow indices, so I set height and width to one less, just in case, but that produced no effect either. I am wondering if i might have a fundamental misunderstanding of how slicing or advanced indexing works.
Here is the printed output:
Slice 1: offset value: 1 height, width: 999 1332
Slice 2: offset value: 1 height, width: 999 1332
Slice 1: offset value: 2 height, width: 999 1331
Slice 2: offset value: 2 height, width: 999 1330
Here is the error I get, It triggers on the second iteration of the while loop:
image = np.minimum(slice1, slice2)
ValueError: operands could not be broadcast together with shapes (999,1331) (999,1330)
Here is my code:
def horizontal_smear(image, distance):
height = len(image) - 1
width = len(image[0] - 1)
offset = 1
while offset < distance /2:
slice1 = copy.deepcopy(image[0 : height, 0 : (width - offset)])
print ("Slice 1: offset value:", offset, " height, width:", len(slice1), len(slice1[0]))
slice2 = copy.deepcopy(image[0 : height, offset : width])
print ("Slice 2: offset value:", offset, " height, width:", len(slice2), len(slice2[0]))
image = np.minimum(slice1, slice2)
offset *= 2
return image