Python Forum
Correct the algorithm of image filter code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Correct the algorithm of image filter code
#1
Hi everyone. I'm new in python. Now I'm trying to write an image filter, according to the next algorithm:
[Image: 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 30

Quote: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: fJivZ.png]
Reply
#2
Does this work for you?
rows, cols = imgshape[:2] #(192, 184, 3)
s = np.zeros((rows, cols))
b = np.ones(imgshape[:2])
k = 0.7
Reply
#3
(May-08-2022, 01:12 PM)deanhystad Wrote: Does this work for you?
rows, cols = imgshape[:2] #(192, 184, 3)
s = np.zeros((rows, cols))
b = np.ones(imgshape[:2])
k = 0.7

Thanks, it helped a bit. But I still didn't get the needed result. I made additional corrections (changed indexes backwards in s, m, b). Here's an updated code:
...
rows, cols = imgshape[:2] #(192, 184, 3)
s = np.zeros((rows,cols))
b = np.ones(imgshape[:2])
k = 0.9

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[i,j]
        s[i,j] = s[i,j] + m
        if s[i,j] >= (k*max):
            s[i,j] = s[i,j] - (k*max)
            s[i,j] = s[i,j] + m
            b[i,j] = 1
        else: 
            s[i,j] = s[i,j] + m
            b[i,j] = 0

plt.imshow(b, cmap='gray')
plt.savefig('new_img.png')
And this is what I get (the second image is when k=0.7, the third is when k=0.9):
[Image: Screenshot-2022-05-08-at-16-56-04.png]

How to correct the code to make image impulse-look-like as in example?
Reply
#4
Why aren't you letting cv2 do the grayscale conversion?
grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
And why not use cv2.threshold to convert that to black and white?
Reply
#5
(May-08-2022, 03:18 PM)deanhystad Wrote: Why aren't you letting cv2 do the grayscale conversion?
grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
And why not use cv2.threshold to convert that to black and white?

It's a part of the task - not to use built-in grayscale filter
Reply
#6
You should display the grayscale image you produce and the greyscale image that is produced by cv2. That might help you figure out why the thresholding isn't producing the result you want. I don't think using mean is correct. Red and Blue are perceived to be dark colors, and Green a bright color, but in your equation all contribute equally to the brightness of the pixel.
Reply
#7
(May-08-2022, 04:29 PM)deanhystad Wrote: You should display the grayscale image you produce and the greyscale image that is produced by cv2. That might help you figure out why the thresholding isn't producing the result you want. I don't think using mean is correct. Red and Blue are perceived to be dark colors, and Green a bright color, but in your equation all contribute equally to the brightness of the pixel.

I don't think that the problem is in grayscale filter. For comparison,
this is a result of mean grayscale filter
[Image: Screenshot-2022-05-08-at-20-00-33.png]

And this is a result of cv2 build-in grayscale filter (cv2.COLOR_BGR2GRAY)
[Image: Screenshot-2022-05-08-at-20-00-53.png]

As you see, no so much difference
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm for extracting comments from Python source code Pavel1982 6 549 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 731 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  code decode, string, image ... teckow 2 2,071 Aug-20-2021, 07:02 PM
Last Post: teckow
  [split] Kera Getting errors when following code example Image classification from scratch hobbyist 3 4,651 Apr-13-2021, 01:26 PM
Last Post: amirian
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,137 Dec-06-2020, 10:10 PM
Last Post: Aizou
  my first code, please correct robboc91 4 2,699 Nov-16-2020, 06:32 PM
Last Post: DougBTX
  Help me get this image converter code working? NeTgHoSt 0 2,091 Jul-14-2020, 10:36 PM
Last Post: NeTgHoSt
  Getting the error like function not defined .. suggest correct code raghava 1 2,061 Feb-04-2020, 11:20 PM
Last Post: micseydel
  upload image with generated code from Postman does not work magic7598 0 1,700 Nov-30-2019, 10:32 AM
Last Post: magic7598
  The code seems correct but my files aren't getting deleted taffylim69 1 2,078 Feb-03-2019, 11:00 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020