function not defined. how to call a function that is defined inside a class?
Question:
class Image_Filter:
def __init__(self, image, filter):
self._imageRows = image.shape[0]
self._imageColums = image.shape[1]
self._filterRows = filter.shape[0]
self._filterColums = filter.shape[1]
def __filtering__(self, _imageRows, _imageColums, _filterRows, _filterColums, image, filter):
filtered_image = np.zeros(shape = (rows-2, colums-2))
for m in range(_imageRows):
for n in range(_imageColums):
for k in range(_filterRows):
for l in range(_filterColums):
filtered_image[m][n] = np.sum(filter[k][l]*image[m+k][n+l])
return filtered_image`
`This is an implementation of mean filters for images.
I am trying to call the function filtering and it throws an error.
`---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/home/adarsh/Downloads/Semester 2/Computer vision/Snippets/filters.ipynb Cell 4 in <cell line: 1>()
----> 1 filtered_image = __filtering__(image, filter)
NameError: name '__filtering__' is not defined`
Answers:
The simple answer would be:
image_filter = Image_Filter(some_image, some_filter)
image_filter.__filtering__(...)
The long answer:
There are a few problems with the code that you provided.
The function filtering has parameters _imageRows, _imageColums, _filterRows, and _filterColums, but these variables are not defined in the scope of the function. You can fix this by using the instance variables self._imageRows, self._imageColums, self._filterRows, and self._filterColums instead.
The filtered_image array is being initialized with shape (rows-2, colums-2), but rows and colums are not defined in the scope of the function. You can fix this by using _imageRows-2 and _imageColums-2 instead.
The filtered_image array is being updated inside the loop using filtered_image[m][n], but this will overwrite the value of filtered_image[m][n] on each iteration. To fix this, you should update filtered_image[m][n] using += instead of =.
Also, there is no need to wrap your function names with __,
this is reserved for python’s magic methods.
class Image_Filter:
def __init__(self, image, filter):
self._imageRows = image.shape[0]
self._imageColums = image.shape[1]
self._filterRows = filter.shape[0]
self._filterColums = filter.shape[1]
def __filtering__(self, _imageRows, _imageColums, _filterRows, _filterColums, image, filter):
filtered_image = np.zeros(shape = (rows-2, colums-2))
for m in range(_imageRows):
for n in range(_imageColums):
for k in range(_filterRows):
for l in range(_filterColums):
filtered_image[m][n] = np.sum(filter[k][l]*image[m+k][n+l])
return filtered_image`
`This is an implementation of mean filters for images.
I am trying to call the function filtering and it throws an error.
`---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/home/adarsh/Downloads/Semester 2/Computer vision/Snippets/filters.ipynb Cell 4 in <cell line: 1>()
----> 1 filtered_image = __filtering__(image, filter)
NameError: name '__filtering__' is not defined`
The simple answer would be:
image_filter = Image_Filter(some_image, some_filter)
image_filter.__filtering__(...)
The long answer:
There are a few problems with the code that you provided.
The function filtering has parameters _imageRows, _imageColums, _filterRows, and _filterColums, but these variables are not defined in the scope of the function. You can fix this by using the instance variables self._imageRows, self._imageColums, self._filterRows, and self._filterColums instead.
The filtered_image array is being initialized with shape (rows-2, colums-2), but rows and colums are not defined in the scope of the function. You can fix this by using _imageRows-2 and _imageColums-2 instead.
The filtered_image array is being updated inside the loop using filtered_image[m][n], but this will overwrite the value of filtered_image[m][n] on each iteration. To fix this, you should update filtered_image[m][n] using += instead of =.
Also, there is no need to wrap your function names with __,
this is reserved for python’s magic methods.