更正图像过滤器代码的算法(typeError:' numpy.ndarray' object nocalable)
我正在尝试写一个图像过滤器。这是算法:
我是一排,j是一列,m(i,j)是一个像素,s(i,j)是像素的总和,max(m(i,j))为连续的最大像素,K是系数(0.7),M是RGB平均值的数组。
在使用此算法之前,我首先需要将图像转换为灰度。这里是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)
for j in grayscale_img(rows):
for i in grayscale_img(cols):
max = np.amax(grayscale_img, axis=1)[j]
s[j,i] = s[j,i] + max
if s[j,i] >= (k*max):
s[j,i] = s[j,i] - (k*max)
s[j,i] = s[j,i] + max
b[j,i] = 1
else:
s[j,i] = s[j,i] + max
b[j,i] = 0
cv2.waitKey()
cv2.destroyAllWindows()
当我运行此代码时,我会收到一个错误:
在grayscale_img中的J(行): typeError:'numpy.ndarray'对象不是可叫
是什么原因?请帮助更正代码。
这里一个示例过滤器应如何过滤图像: 单击此处的映像
编辑:我已经纠正了代码,根据注释
...
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
:排队的下一个错误
s[j,i] = s[j,i] + m
indexError:数组的索引太多:数组是0维的,但索引为2
如何纠正它?
I'm trying to write an image filter. Here's the algorithm:
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)
for j in grayscale_img(rows):
for i in grayscale_img(cols):
max = np.amax(grayscale_img, axis=1)[j]
s[j,i] = s[j,i] + max
if s[j,i] >= (k*max):
s[j,i] = s[j,i] - (k*max)
s[j,i] = s[j,i] + max
b[j,i] = 1
else:
s[j,i] = s[j,i] + max
b[j,i] = 0
cv2.waitKey()
cv2.destroyAllWindows()
When I run this code I get an error:
for j in grayscale_img(rows):
TypeError: 'numpy.ndarray' object is not callable
What is the reason? Please help correct the code.
Here an example how the filter should filter an image:
click on image here
EDIT: I have corrected code, according to suggestions on comments:
...
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
But get the next error in line
s[j,i] = s[j,i] + m
IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed
How to correct it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的不知道该算法在做什么,但是这是我在Python/OpenCV中的解释,试图纠正您的代码。
输入:
结果:
但是它运行没有错误。
我留给你纠正它。
实际上,S的所有更新都没有使用。我怀疑在下一个像素之前纠正了s的值,并用于增加其值,而不是将其重置为m。因此,您可能需要使用s [j,i] = s [j,i-1] + m。但是,如果没有提及这应该做的事情,我只能猜测。
I really have no idea what this algorithm is doing, but here is my interpretation in Python/OpenCV trying to correct your code.
Input:
Result:
It is not the same as what you show in your example. But it runs without error.
I leave it to you to correct it.
As it is, all the updates of s go unused. I suspect that the value of s is corrected before the next pixel and used to increment its value rather than resetting it to m. So you may want to use s[j,i] = s[j,i-1] + m. But without a reference to what this should be doing, I can only guess.