更正图像过滤器代码的算法(typeError:' numpy.ndarray' object nocalable)

发布于 2025-01-27 03:30:35 字数 2051 浏览 2 评论 0原文

我正在尝试写一个图像过滤器。这是算法:

“”

我是一排,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑寡妇 2025-02-03 03:30:35

我真的不知道该算法在做什么,但是这是我在Python/OpenCV中的解释,试图纠正您的代码。

输入:

在此处输入图像描述“

import cv2
import numpy as np

img = cv2.imread('red_cell.png')
imgshape = img.shape
print(imgshape)

gray = np.mean(img, axis=2)
grayshape = gray.shape
print(grayshape)

s = np.zeros(grayshape[:2])
print(s.shape)
b = np.zeros(grayshape[:2])
print(b.shape)
k = 0.7

for j in range(grayshape[0]):
    max = np.amax(gray, axis=1)[j]
    km = k * max
    for i in range(grayshape[1]):
        m = gray[j,i]
        s[j,i] = s[j,i] + m
        if s[j,i] >= km:
            b[j,i] = 1
            s[j,i] = s[j,i] - km
            s[j,i] = s[j,i] + m
        else: 
           #b[j,i] = 0 (since b is already initialized to zero)
           s[j,i] = s[j,i] + m

cv2.imwrite('red_cell_thresh.png', (255*b).clip(0,255).astype(np.uint8))
cv2.imshow('b',b)
cv2.waitKey()
cv2.destroyAllWindows()

结果:

​但是它运行没有错误。

我留给你纠正它。

实际上,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:

enter image description here

import cv2
import numpy as np

img = cv2.imread('red_cell.png')
imgshape = img.shape
print(imgshape)

gray = np.mean(img, axis=2)
grayshape = gray.shape
print(grayshape)

s = np.zeros(grayshape[:2])
print(s.shape)
b = np.zeros(grayshape[:2])
print(b.shape)
k = 0.7

for j in range(grayshape[0]):
    max = np.amax(gray, axis=1)[j]
    km = k * max
    for i in range(grayshape[1]):
        m = gray[j,i]
        s[j,i] = s[j,i] + m
        if s[j,i] >= km:
            b[j,i] = 1
            s[j,i] = s[j,i] - km
            s[j,i] = s[j,i] + m
        else: 
           #b[j,i] = 0 (since b is already initialized to zero)
           s[j,i] = s[j,i] + m

cv2.imwrite('red_cell_thresh.png', (255*b).clip(0,255).astype(np.uint8))
cv2.imshow('b',b)
cv2.waitKey()
cv2.destroyAllWindows()

Result:

enter image description here

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文