0

libraries such as PIL, Skimage and matplotlib mping all produced the same result

img1 = io.imread("1.3/blurry-moon.tif")

img1 = img1.astype('int32')

img2 = io.imread("1.3/blurry-moon.tif")

imageArray = np.copy(img1)

image1 = np.copy(img1)

image2 = np.copy(img2)

applying a fir filter to these two images produces different results

FIR = [[0, -1, 0], [-1, 5 , -1], [0, -1, 0]]

img1 produces an enhanced image while img2 loses some detail in darker regions

output of the two processes

I don't understand why the imported image type by default produces such bad results, any ideas on what is going on?

Thank you.

complete code:

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.image as mpimg
import math
from skimage import io

# if current row of image exceeds row of convolutional filter, find the right row of the filter to apply
def convolve(image,filter, i, e):
  tot = 0
  for x in range(3):
      for v in range(3):   
        tot += image[i+x][e+v]*filter[x][v]
  return tot



# let img1 be an image with no features 
img1 = io.imread("1.3/blurry-moon.tif")
# change image encoding, keeps the source image as it is. What conversion happened when using the Library? 
# ***********************************
img1 = img1.astype('int32')


img2 = io.imread("1.3/blurry-moon.tif")

# These are their own individual new images
imageArray = np.copy(img1)
image1 = np.copy(img1)
image2 = np.copy(img2)

# gets the size of the image array
imageSize = imageArray.shape

FIR = [[0, -1, 0], 
      [-1, 5 , -1], 
      [0, -1, 0]]


for i, row in enumerate(imageArray):
      for e, value in enumerate(row):
        #   make sure we done apply the filter outside the image boundary
          if i < (imageSize[0]-3) and e < (imageSize[1]-3):
            image1[i+1][e+1] = convolve(imageArray, FIR, i ,e)

for i, row in enumerate(imageArray):
      for e, value in enumerate(row):
          if i < (imageSize[0]-3) and e < (imageSize[1]-3):
            image2[i+1][e+1] = convolve(imageArray, FIR, i ,e)


plt.imshow(img1, cmap='gray') 
plt.axis('off') 
plt.title('original') 
plt.show() 

plt.imshow(image1, cmap='gray') 
plt.axis('off') 
plt.title('Laplacian filter') 
plt.show() 

plt.imshow(image2, cmap='gray') 
plt.axis('off') 
plt.title('FIR filter') 
plt.show() ```
Michael
  • 11
  • 3
  • Looks like overflow/underflow to me. E.g. if your type is unsigned, a filter that could result in negative values will cause problems like this. But for a better answer, consider posting more complete code so we can reproduce your results exactly. – bogovicj Aug 26 '20 at 00:00
  • So the difference is that one image is int32 (signed) and the other is uint8 (unsigned, and small). In the uint8 image you get strange results when writing a negative value or a value larger than 255. – Cris Luengo Aug 26 '20 at 01:03
  • it is greyscale so values, negative or over 255 shouldn't be an issue – Michael Aug 27 '20 at 01:43

0 Answers0