Issue
I managed to blur an image using only a NP matrix, but for some reason can't restore the normal color channels to it. If someone can give me guidance on how to fix this without calling me an idiot, that would be appreciated.
def convolve(image,kernel):
image_copy = image.copy()
height=image_copy.shape[0]
width=image_copy.shape[1]
width_kernel=len(kernel)
final_img = [ [0]*width for i in range(height)]
# print(outAr)
for i in range(width_kernel,image_copy.shape[0]-width_kernel):
for j in range(width_kernel,image_copy.shape[1]-width_kernel):
avg_square = image_copy[i-width_kernel:i+width_kernel+1, j-width_kernel:j+width_kernel+1]
avg = np.mean(avg_square,dtype=np.float32)
final_img[i][j] = int(avg)
return final_img
kernel=np.ones((9,9))*1/9
plt.imshow(convolve(image,kernel))
Solution
First of all, you are not an idiot and don't take anyone seriously who calls you that. You made a strong first attempt at blurring an image and did a good job for the first try. You are almost there so be proud. To fix this, you should separate the Red, Green, and Blue channels before you do any blurring. Blur them independently and then recombine. I would recommend a few changes to make it easier.
Firstly, it will make life easier to make the image a numpy array right after you read the image in. I'm assuming you already did this, but it isn't shown.
image_rgb = np.array(image)
Second, separate the R,G,B channels.
r = image_rgb[:,:,0]
g = image_rgb[:,:,1]
b = image_rgb[:,:,2]
From here, perform your convolution function on each channel. Then recombine the blurred channels and you have a blurred RGB image.
blurred_r = convolve(r, kernel)
blurred_g = convolve(g, kernel)
blurred_b = convolve(b, kernel)
blurred_image_rgb = np.dstack([blurred_r, blurred_g, blurred_b])
There are some edge effects from your convolution, I'm not sure if that's intended. However, you seem like the type that will be able to figure that out if you need to.
Answered By - greenerpastures
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.