Last active
October 18, 2019 14:00
-
-
Save connectwithprakash/eb94f43e75549de4ff8f622884b20f15 to your computer and use it in GitHub Desktop.
Apply custom filter for convolution on image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_filter_output(input_img_path, filter): | |
| img = plt.imread(input_img_path) | |
| filter_size = filter.shape[0] | |
| pad_width = (filter_size-1)//2 | |
| if len(img.shape)==3: | |
| filter_3D = np.stack((filter, filter, filter), axis=2) | |
| temp = np.pad(img, ((pad_width, pad_width), (pad_width, pad_width), (0, 0)), mode='constant', constant_values=0) | |
| temp = np.array([[np.sum(np.multiply(temp[i:i+filter_size, j:j+filter_size, :], filter_3D)) for j in range(temp.shape[1]-filter_size+1)] for i in range(temp.shape[0]-filter_size+1)]) | |
| else: | |
| temp = np.pad(img, ((pad_width, pad_width), (pad_width, pad_width)), mode='constant', constant_values=0) | |
| temp = np.array([[np.sum(np.multiply(temp[i:i+filter_size, j:j+filter_size], filter)) for j in range(temp.shape[1]-filter_size+1)] for i in range(temp.shape[0]-filter_size+1)]) | |
| temp = np.clip(temp, 0, 255) | |
| return temp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment