官术网_书友最值得收藏!

Smoothing a photo 

Applying a box filter with hard edges doesn't result in a smooth blur on the output photo.

To improve this, the filter can be made smoother around the edges. One of the popular such filters is a Gaussian filter. This is a non-linear filter which enhances the effect of the center pixel and gradually reduces the effects as the pixel gets farther from the center. Mathematically, a Gaussian function is given as:

where μ is mean and σ is variance. 

An example kernel matrix for this kind of filter in a two-dimensional discrete domain is given as follows:

This two-dimensional array is used in normalized form and effect of this filter also depends on its width by changing the kernel width has varying effects on the output as discussed in further section. Applying Gaussian kernel as filter removes high-frequency components which results in removing strong edges and hence a blurred photo:

While this filter performs better blurring than a box filter, the implementation is also quite simple with OpenCV:

def plot_cv_img(input_image, output_image): 
"""
Converts an image from BGR to RGB and plots
"""
fig, ax = plt.subplots(nrows=1, ncols=2)

ax[0].imshow(cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB))
ax[0].set_title('Input Image')
ax[0].axis('off')

ax[1].imshow(cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB))
ax[1].set_title('Gaussian Blurred')
ax[1].axis('off')
plt.show()


def main():
# read an image
img = cv2.imread('../figures/flower.png')

# apply gaussian blur,

# kernel of size 5x5,
# change here for other sizes
kernel_size = (5,5)
# sigma values are same in both direction
blur = cv2.GaussianBlur(img,(5,5),0)

plot_cv_img(img, blur)

if __name__ == '__main__':
main()
主站蜘蛛池模板: 应用必备| 名山县| 七台河市| 兰考县| 金华市| 车致| 灌云县| 武功县| 江都市| 静宁县| 临沂市| 保亭| 石首市| 锡林郭勒盟| 阿鲁科尔沁旗| 沙坪坝区| 关岭| 全椒县| 商河县| 双峰县| 曲靖市| 古田县| 赤峰市| 高雄市| 阿合奇县| 镇沅| 赫章县| 镇原县| 老河口市| 潮州市| 丹江口市| 济阳县| 彰化县| 米易县| 漳州市| 三河市| 九江县| 宁武县| 扶风县| 铜山县| 洛隆县|