{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Filtreler\n", "\n", "\n", "\n", "[resim kaynağı](https://docs.gimp.org/en/images/filters/examples/convolution-calculate.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:49.938704Z", "start_time": "2019-08-22T09:25:49.235218Z" } }, "outputs": [], "source": [ "from PIL import Image\n", "from matplotlib.pyplot import imshow\n", "import numpy as np\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:50.186495Z", "start_time": "2019-08-22T09:25:49.942483Z" } }, "outputs": [], "source": [ "img_noisy = Image.open(\"images/noisy_test.jpg\")\n", "imshow(img_noisy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Median(Ortanca Değer) Filtresi\n", "\n", "\n", "\n", "### Tuz-buz gürültüsünden temizleme\n", "Bir pencere boyutu belirlenir ve resim üzerinde gezdirilen pencere içerisindeki piksel değerleri sıralanarak ortadaki değer alınır." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:51.741249Z", "start_time": "2019-08-22T09:25:51.738695Z" } }, "outputs": [], "source": [ "img_noisy_array = np.array(img_noisy)\n", "new_img_array = np.array(img_noisy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3x3 Filtre" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:54.202873Z", "start_time": "2019-08-22T09:25:54.200374Z" } }, "outputs": [], "source": [ "def getMedian(li):\n", " li.sort()\n", " return li[int(len(li)/2)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:55.727097Z", "start_time": "2019-08-22T09:25:55.227480Z" } }, "outputs": [], "source": [ "############### 3x3 window ###############\n", "for i in range(1, img_noisy_array.shape[0] - 1):\n", " for j in range(1, img_noisy_array.shape[1] - 1):\n", " win = []\n", " for x in range(i - 1, i + 2):\n", " for y in range(j - 1, j + 2):\n", " win.append(img_noisy_array[x][y])\n", "\n", " new_img_array[i][j] = getMedian(win)\n", "\n", "imshow(new_img_array)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5x5 Filtre" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:25:58.303805Z", "start_time": "2019-08-22T09:25:57.481663Z" } }, "outputs": [], "source": [ "############### 5x5 window ###############\n", "img_noisy_array = np.array(img_noisy)\n", "new_img_array = np.array(img_noisy)\n", "\n", "for i in range(1, img_noisy_array.shape[0] - 2):\n", " for j in range(1, img_noisy_array.shape[1] - 2):\n", " win = []\n", " for x in range(i - 2, i + 3):\n", " for y in range(j - 2, j + 3):\n", " win.append(img_noisy_array[x][y])\n", " #sort the values\n", " win.sort()\n", " \n", " new_img_array[i][j] = getMedian(win)\n", "\n", "imshow(new_img_array)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mean (Ortalama) Filtresi\n", "Resim üzerinde gezdirilen pencere içerisindeki piksel değerlerinin ortalaması alınır." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2019-08-22T09:26:09.338443Z", "start_time": "2019-08-22T09:26:08.312591Z" } }, "outputs": [], "source": [ "############### 5x5 window ###############\n", "img_noisy_array = np.array(img_noisy)\n", "new_img_array = np.array(img_noisy)\n", "\n", "\n", "for i in range(1, img_noisy_array.shape[0] - 2):\n", " for j in range(1, img_noisy_array.shape[1] - 2):\n", " win = []\n", " for x in range(i - 2, i + 3):\n", " for y in range(j - 2, j + 3):\n", " win.append(img_noisy_array[x][y])\n", " \n", " new_img_array[i][j] = (np.sum(win))/(5*5)\n", "\n", "imshow(new_img_array)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "