Averaging Filter using python

2024/11/18 2:32:00

I am new in python and trying apply averaging filter on image as the way i understand averaging concept

summing up the neighboring elements including itself and divide it by number of elementstechnique

But the problem is getting dark image rather then blur image as result

image = cv2.imread('./images/hip-salt.jpg', 0);
width = image.shape[1]
height = image.shape[0]
result = np.zeros((image.shape[0], image.shape[1]), int)def meanFilter():for row in range(height):for col in range(width):  currentElement=0; left=0; right=0; top=0; bottom=0; topLeft=0; topRight=0; bottomLeft=0; bottomRight=0;counter = 1           currentElement = image[row][col]if not col-1 < 0:left = image[row][col-1]counter +=1                        if not col+1 > width-1:right = image[row][col+1]counter +=1 if not row-1 < 0:top = image[row-1][col]counter +=1 if not row+1 > height-1:bottom = image[row+1][col]counter +=1 if not row-1 < 0 and not col-1 < 0:topLeft = image[row-1][col-1]counter +=1 if not row-1 < 0 and not col+1 > width-1:topRight = image[row-1][col+1]counter +=1 if not row+1 > height-1 and not col-1 < 0:bottomLeft = image[row+1][col-1]counter +=1 if not row+1 > height-1 and not col+1 > width-1:bottomRight = image[row+1][col+1]counter +=1total = int(currentElement)+int(left)+int(right)+int(top)+int(bottom)+int(topLeft)+int(topRight)+int(bottomLeft)+int(bottomRight)avg = total/counterresult[row][col] = avgmeanFilter(); 
cv2.imshow('Averaging Filter', result);
cv2.waitKey(0)
cv2.destroyAllWindows()
Answer

I tested your code and the issue appears to be the type!

the values of result are int but they should be uint8
you can either fill it in correctly to begin with or recast it before displaying, like this:

result = result.astype('uint8')

why are you making this filter manually though? why not use the built in blur filter? you may want to read this

https://en.xdnf.cn/q/120124.html

Related Q&A

Click on Show more deals in webpage with Selenium

Id like to click on every Show 10 more deals on the following page: "https://www.uswitch.com/broadband/compare/deals_and_offers/" but it does not seem to work. Im stuck having the following e…

In Matplotlib, what axis attribute specifies the spacing between ticks? [duplicate]

This question already has answers here:Changing the tick frequency on the x or y axis(15 answers)Closed 6 years ago.When generating a Matplotlib line or scatter plot, what axis attribute specifies the …

How can I make a simple calculator in python 3?

Im making this calculator using python 3, and this is what I have so far:print("Welcome to Calculator!")class Calculator:def addition(self,x,y):added = x + yreturn addeddef subtraction(self,x…

python 3: lists dont change their values

So I am trying to change a bunch of list items by a random percentage using a for loop.import random as rdm list = [1000, 100, 50, 25] def change():for item in list:item = item + item*rdm.uniform(-10, …

Using zip_longest on unequal lists but repeat the last entry instead of returning None

There is an existing thread about this Zipping unequal lists in python in to a list which does not drop any element from longer list being zipped But its not quite Im after. Instead of returning None, …

python scrapy not crawling all urls in scraped list

I am trying to scrape information from the pages listed on this page. https://pardo.ch/pardo/program/archive/2017/catalog-films.htmlthe xpath selector:film_page_urls_startpage = sel.xpath(//article[@cl…

Python - Do (something) when event is near [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python script to find nth prime number

Im new to Python and I thought Id try to learn the ropes a bit by writing a function to find the nth prime number, however I cant get my code to work properly. No doubt this is due to me missing someth…

Printing values from list within an input range

I have an unordered list, lets say:lst = [12,23,35,54,43,29,65]and the program will prompt the user to input two numbers, where these two numbers will represent the range.input1 = 22input2 = 55therefor…

An issue with the tag add command of the ttk.Treeview widget - cant handle white space

I have noticed an issue with using the tag add command of a ttk.Treeview widget when activated with the tk.call() method. That is, it cant handle white space in the value of the str() elements of its i…