May-08-2022, 11:10 AM
Hi everyone. I'm new in python. Now I'm trying to write an image filter, according to the next algorithm:
![[Image: xn4U8.jpg]](https://i.stack.imgur.com/xn4U8.jpg)
i is a row, j is a column, m(i,j) is a pixel, s(i,j) is a sum of pixels, max(m(i,j)) is a max pixel in a row, k is a coefficient (0.7), m is an array of RGB average.
Before using this algorithm I firstly need to convert the image to grayscale. Here a code in python:
What is the reason of an error. Could you please help to correct this code?
Thanks!
Here's an example of image filter work:
![[Image: xn4U8.jpg]](https://i.stack.imgur.com/xn4U8.jpg)
i is a row, j is a column, m(i,j) is a pixel, s(i,j) is a sum of pixels, max(m(i,j)) is a max pixel in a row, k is a coefficient (0.7), m is an array of RGB average.
Before using this algorithm I firstly need to convert the image to grayscale. Here a code in python:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img_path = 'image.jpg'
img = cv2.imread(img_path)
imgshape = img.shape
fix_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
R, G, B = fix_img[:,:,0], fix_img[:,:,1], fix_img[:,:,2]
grayscale_img = np.mean(fix_img, axis=2)
s = np.array([0][0])
b = np.ones(imgshape[:2])
k = 0.7
rows, cols = imgshape[:2] #(192, 184, 3)
s = np.array([0][0])
b = np.ones(imgshape[:2])
k = 0.7
rows, cols = imgshape[:2] #(192, 184, 3)
for j in range(grayscale_img.shape[1]):
for i in range(grayscale_img.shape[0]):
max = np.amax(grayscale_img, axis=1)[j]
m = grayscale_img[j,i]
s[j,i] = s[j,i] + m
if s[j,i] >= (k*max):
s[j,i] = s[j,i] - (k*max)
s[j,i] = s[j,i] + m
b[j,i] = 1
else:
s[j,i] = s[j,i] + m
b[j,i] = 0
cv2.waitKey()
cv2.destroyAllWindows()While running this code I get the error in line 30Quote:IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed
What is the reason of an error. Could you please help to correct this code?
Thanks!
Here's an example of image filter work:

![[Image: Screenshot-2022-05-08-at-16-56-04.png]](https://i.ibb.co/51zM18r/Screenshot-2022-05-08-at-16-56-04.png)
![[Image: Screenshot-2022-05-08-at-20-00-33.png]](https://i.ibb.co/Sc9kT7J/Screenshot-2022-05-08-at-20-00-33.png)
![[Image: Screenshot-2022-05-08-at-20-00-53.png]](https://i.ibb.co/tbJWWPf/Screenshot-2022-05-08-at-20-00-53.png)