Help
cancel
Showing results for
Did you mean:
Highlighted
Copilot Lvl 3
Message 1 of 4

## Error with For loop and matrix multiplication in PYTHON

Solved! Go to Solution.
```ValueError                                Traceback (most recent call last)
<ipython-input-35-1ecd9c5afb6f> in <module>
19         print(right_temp.size)
20         print(coordinates_left, coordinates_right)
---> 21         same, h, w=if_same(left_temp, right_temp)
22         #plt.figure(figsize=[h,w])
23         #plt.imshow(out1)

<ipython-input-33-27346e0a4cef> in if_same(left_temp, right_temp)
32                     image1=left_temp_np[0:h][0:w]
33                     image2=right_temp_np[j:h+j][i:w+i]
---> 34                     same=mse(image1, image2)
35                     print("loop 1 i: %s j: %s image1: %s image2: %s" % (i,j,image1.size,image2.size))
36                     print(same)

<ipython-input-34-3ff65825a155> in mse(image1, image2)
1 def mse(image1, image2):
----> 2     diff=(image1.astype(float)-image2.astype(float))
3     error=diff**2
4     [w,h]=image1.shape
5     den=w*h

ValueError: operands could not be broadcast together with shapes (10,10) (10,11) ```

Above is the Error.

The code employs if conditions and for loop to scans unequal images from two cameras and checks for matches.

'if_same' : This is the main function which scans and checks.

'mse' calls another function which calculates squared error.

```def mse(image1, image2):
diff=(image1.astype(float)-image2.astype(float))
error=diff**2
[w,h]=image1.shape
den=w*h
mse=error/den

return mse
```

```def if_same(left_temp, right_temp):

h_L, w_L = left_temp.size
h_R, w_R = right_temp.size

left_temp_np=np.asarray(left_temp)
right_temp_np=np.asarray(right_temp)

diff_w=abs(w_L-w_R)+1
diff_h=abs(h_L-h_R)+1

image1=[]
image2=[]
#same= np.zeros([h,w], np.uint8)
same=0
h=0
w=0

if w_L<=w_R:
if h_L<=h_R:
[w,h]=[w_L,h_L]
#image1=np.zeros[h,w]
#image2=np.zeros[h,w]
i=0
for i in range(0,diff_w):
j=0
for j in range(0,diff_h):
print(j, h, h+j, i, w, w+i)
image1=left_temp_np[0:h][0:w]
image2=right_temp_np[j:h+j][i:w+i]
same=mse(image1, image2)
print("loop 1 i: %s j: %s image1: %s image2: %s" % (i,j,image1.size,image2.size))
print(same)

elif h_R<h_L:
[w,h]=[w_L,h_R]
i=0
for i in range(0,diff_w):
j=0
for j in range(0,diff_h):
image1=left_temp_np[j:h+j][0:w]
image2=right_temp_np[0:h][i:w+i]
same=mse(image1, image2)
print("loop 2 i: %s j: %s image1: %s image2: %s" % (i,j,image1.size,image2.size))
print(same)
return same, h, w

elif w_R<w_L:
if h_L<=h_R:
i=0
[w,h]=[w_R,h_L]
for i in range(0,diff_w):
j=0
for j in range(0,diff_h):
image1=left_temp_np[0:h][i:w+i]
image2=right_temp_np[j:h+j][0:w]
same=mse(image1, image2)
print("loop 3 i: %s j: %s image1: %s image2: %s" % (i,j,image1.size,image2.size))
print(same)

elif h_R<h_L:
i=0
[w,h]=[w_R,h_R]
for i in range(0,diff_w):
j=0
for j in range(0,diff_h):
print(j)
image1=left_temp_np[j:(h+j)][i:(w+i)]
image2=right_temp_np[0:h][0:w]
print("loop 4 i: %s j: %s image1: %s image2: %s" % (i,j,image1.size,image2.size))
same=mse(image1, image2)
print(same)
return same, h, w

return same, h, w ```

I have actually tried printing the i,j,w,h  to see if there is the problem.

`0 10 10 0 13 13`

The code should slice matrixes of dimensions 10x13 from both images. But I get 10x10 and 10x11.

If the slice is [0:10][0:13] and [1:11][1:14], both will return [10x13]. Correct me, if I'm wrong

3 Replies
Solution
Copilot Lvl 3
Message 2 of 4

## Re: Error with For loop and matrix multiplication in PYTHON

The shape of the image gets inversed when read as an array.

I later found that out by printing the shapes of images and image arrays.

@ramkarumugam wrote:
```ValueError                                Traceback (most recent call last)
<ipython-input-35-1ecd9c5afb6f> in <module>
19         print(right_temp.size)
20         print(coordinates_left, coordinates_right)
---> 21         same, h, w=if_same(left_temp, right_temp)
22         #plt.figure(figsize=[h,w])
23         #plt.imshow(out1)

<ipython-input-33-27346e0a4cef> in if_same(left_temp, right_temp)
32                     image1=left_temp_np[0:h][0:w]
33                     image2=right_temp_np[j:h+j][i:w+i]
---> 34                     same=mse(image1, image2)
35                     print("loop 1 i: %s j: %s image1: %s image2: %s" % (i,j,image1.size,image2.size))
36                     print(same)

<ipython-input-34-3ff65825a155> in mse(image1, image2)
1 def mse(image1, image2):
----> 2     diff=(image1.astype(float)-image2.astype(float))
3     error=diff**2
4     [w,h]=image1.shape
5     den=w*h

ValueError: operands could not be broadcast together with shapes (10,10) (10,11) ```

Above is the Error.

The code employs if conditions and for loop to scans unequal images from two cameras and checks for matches.

'if_same' : This is the main function which scans and checks.

'mse' calls another function which calculates squared error.

```def mse(image1, image2):
diff=(image1.astype(float)-image2.astype(float))
error=diff**2
[w,h]=image1.shape
den=w*h
mse=error/den

return mse
```

```def if_same(left_temp, right_temp):

h_L, w_L = left_temp.size
h_R, w_R = right_temp.size

left_temp_np=np.asarray(left_temp)
right_temp_np=np.asarray(right_temp)

diff_w=abs(w_L-w_R)+1
diff_h=abs(h_L-h_R)+1

image1=[]
image2=[]
#same= np.zeros([h,w], np.uint8)
same=0
h=0
w=0

if w_L<=w_R:
if h_L<=h_R:
[w,h]=[w_L,h_L]
#image1=np.zeros[h,w]
#image2=np.zeros[h,w]
i=0
for i in range(0,diff_w):
j=0
for j in range(0,diff_h):
print(j, h, h+j, i, w, w+i)
image1=left_temp_np[0:h][0:w]
image2=right_temp_np[j:h+j][i:w+i]
same=mse(image1, image2)
print("loop 1 i: %s j: %s image1: %s image2: %s" % (i,j,image1.size,image2.size))
print(same)

elif h_R<h_L:
[w,h]=[w_L,h_R]
i=0
for i in range(0,diff_w):
j=0
for j in range(0,diff_h):
image1=left_temp_np[j:h+j][0:w]
image2=right_temp_np[0:h][i:w+i]
same=mse(image1, image2)
print("loop 2 i: %s j: %s image1: %s image2: %s" % (i,j,image1.size,image2.size))
print(same)
return same, h, w

elif w_R<w_L:
if h_L<=h_R:
i=0
[w,h]=[w_R,h_L]
for i in range(0,diff_w):
j=0
for j in range(0,diff_h):
image1=left_temp_np[0:h][i:w+i]
image2=right_temp_np[j:h+j][0:w]
same=mse(image1, image2)
print("loop 3 i: %s j: %s image1: %s image2: %s" % (i,j,image1.size,image2.size))
print(same)

elif h_R<h_L:
i=0
[w,h]=[w_R,h_R]
for i in range(0,diff_w):
j=0
for j in range(0,diff_h):
print(j)
image1=left_temp_np[j:(h+j)][i:(w+i)]
image2=right_temp_np[0:h][0:w]
print("loop 4 i: %s j: %s image1: %s image2: %s" % (i,j,image1.size,image2.size))
same=mse(image1, image2)
print(same)
return same, h, w

return same, h, w ```

I have actually tried printing the i,j,w,h  to see if there is the problem.

`0 10 10 0 13 13`

The code should slice matrixes of dimensions 10x13 from both images. But I get 10x10 and 10x11.

If the slice is [0:10][0:13] and [1:11][1:14], both will return [10x13]. Correct me, if I'm wrong

Commander Lvl 3
Message 3 of 4

## Re: Error with For loop and matrix multiplication in PYTHON

Thanks so much for sharing your solution with the community. Is your problem fully resolved now?

- Mark
Copilot Lvl 3
Message 4 of 4

## Re: Error with For loop and matrix multiplication in PYTHON

Yeah.It's solved fully. Thanks for responding Mark.

--Ram K

New solutions