{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom skimage.morphology import erosion\nfrom skimage.morphology import dilation\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport skimage\nfrom skimage import io, filters\n\n#Reading image\nimage = skimage.io.imread('/kaggle/input/assignment-2-computer-vision/chemical/inchi10.png', as_gray = True)\n\nplt.imshow(image, cmap=plt.get_cmap('gray'))","metadata":{"execution":{"iopub.status.busy":"2023-04-14T16:23:34.466026Z","iopub.execute_input":"2023-04-14T16:23:34.467463Z","iopub.status.idle":"2023-04-14T16:23:36.917794Z","shell.execute_reply.started":"2023-04-14T16:23:34.467382Z","shell.execute_reply":"2023-04-14T16:23:36.916371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Apply filter that removes single standing points\nkernel = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])\nkernel2 = np.array([[1, 1], [1, 1]])\nplt.imshow(dilation(dilation(erosion(image, kernel), kernel2), kernel2), cmap=plt.get_cmap('gray'))","metadata":{"execution":{"iopub.status.busy":"2023-04-08T11:24:03.252812Z","iopub.execute_input":"2023-04-08T11:24:03.253207Z","iopub.status.idle":"2023-04-08T11:24:03.524728Z","shell.execute_reply.started":"2023-04-08T11:24:03.253171Z","shell.execute_reply":"2023-04-08T11:24:03.523105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('/kaggle/input/assignment-2-computer-vision/chemical/inchi10.png', cv2.IMREAD_GRAYSCALE)\n\n# Define mean filter\nmean_kernel = np.ones((3,3), np.float32) / 9\n\n# Apply filters\nmean_filtered = cv2.filter2D(img, -1, mean_kernel)\ngaussian_filtered = cv2.GaussianBlur(img, (3, 3), 1)\nmedian_filtered = cv2.medianBlur(img, 3)\nbilateral_filtered = cv2.bilateralFilter(img, 15, 150, 150)\n\n\n# Display images\nfig, axs = plt.subplots(2, 3, figsize=(100, 70))\naxs[0][0].imshow(img, cmap='gray')\naxs[0][0].set_title('Original Image')\naxs[0][1].imshow(mean_filtered, cmap='gray')\naxs[0][1].set_title('Mean Filtered Image')\naxs[1][0].imshow(gaussian_filtered, cmap='gray')\naxs[1][0].set_title('Gaussian Filtered Image')\naxs[1][1].imshow(median_filtered, cmap='gray')\naxs[1][1].set_title('Median Filtered Image')\naxs[0][2].imshow(bilateral_filtered, cmap='gray')\naxs[0][2].set_title('Bilateral Filtered Image')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-14T16:24:28.563544Z","iopub.execute_input":"2023-04-14T16:24:28.565014Z","iopub.status.idle":"2023-04-14T16:24:33.435890Z","shell.execute_reply.started":"2023-04-14T16:24:28.564948Z","shell.execute_reply":"2023-04-14T16:24:33.434247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('/kaggle/input/assignment-2-computer-vision/chemical/inchi10.png', cv2.IMREAD_GRAYSCALE)\n\n#Running fast Fourier transform\nimg_fft = np.fft.fft2(img)\nimg_fftshift = np.fft.fftshift(img_fft)\nimg_ifftshit = np.fft.ifftshift(img_fftshift)\nimg_ifft = np.fft.ifft2(img_ifftshit)\n\n# Display images\nfig, axs = plt.subplots(2, 3, figsize=(10, 7))\naxs[0][0].imshow(img, cmap='gray')\naxs[0][0].set_title('Original Image')\naxs[0][1].imshow(np.log(1+np.abs(img_fft)), cmap='gray')\naxs[0][1].set_title('Spectrum, FFT')\naxs[0][2].imshow(np.log(1+np.abs(img_fftshift)), cmap='gray')\naxs[0][2].set_title('Centered Spectrum')\naxs[1][0].imshow(np.log(1+np.abs(img_ifftshit)), cmap='gray')\naxs[1][0].set_title('Decentralized IFFT')\naxs[1][1].imshow(np.abs(img_ifft), cmap='gray')\naxs[1][1].set_title('Reversed Image')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-08T11:24:04.264369Z","iopub.execute_input":"2023-04-08T11:24:04.264710Z","iopub.status.idle":"2023-04-08T11:24:05.000725Z","shell.execute_reply.started":"2023-04-08T11:24:04.264677Z","shell.execute_reply":"2023-04-08T11:24:04.999752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from math import exp, sqrt\n\n#Finding distance between points\ndef distance(point1,point2):\n    return sqrt((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2)\n\n#Applying gaussian blur\ndef gaussian(D0,imgShape):\n    base = np.zeros(imgShape[:2])\n    rows, cols = imgShape[:2]\n    center = (rows/2,cols/2)\n    for x in range(cols):\n        for y in range(rows):\n            base[y,x] = exp(((-distance((y,x),center)**2)/(2*(D0**2))))\n    return base","metadata":{"execution":{"iopub.status.busy":"2023-04-08T11:24:05.002343Z","iopub.execute_input":"2023-04-08T11:24:05.003038Z","iopub.status.idle":"2023-04-08T11:24:05.012368Z","shell.execute_reply.started":"2023-04-08T11:24:05.002996Z","shell.execute_reply":"2023-04-08T11:24:05.010713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# calculate low pass for image\ndef try_d0s_lp(d0):\n    fig, axs = plt.subplots(1, 2, figsize=(10, 3))\n    axs[0].imshow(img, cmap='gray')\n    axs[0].set_title('Original Image')\n    original = np.fft.fft2(img)\n    center = np.fft.fftshift(original)\n    LowPassCenter = center * gaussian(d0,img.shape)\n    LowPass = np.fft.ifftshift(LowPassCenter)\n    inverse_LowPass = np.fft.ifft2(LowPass)\n    axs[1].imshow(np.abs(inverse_LowPass), cmap='gray')\n    axs[1].set_title('Processed Image')\n    plt.suptitle(\"D0:\"+str(d0),fontweight=\"bold\")\n    plt.show()\n    \nfor i in [100,50,30,20,10]:\n    try_d0s_lp(i)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T11:24:05.014477Z","iopub.execute_input":"2023-04-08T11:24:05.015991Z","iopub.status.idle":"2023-04-08T11:24:07.476950Z","shell.execute_reply.started":"2023-04-08T11:24:05.015923Z","shell.execute_reply":"2023-04-08T11:24:07.475312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# calculate high pass for image\ndef try_d0s_hp(d0):\n    fig, axs = plt.subplots(1, 2, figsize=(10, 3))\n    axs[0].imshow(img, cmap='gray')\n    axs[0].set_title('Original Image')\n    original = np.fft.fft2(img)\n    center = np.fft.fftshift(original)\n    HighPassCenter = center * gaussian(d0,img.shape)\n    HighPass = np.fft.ifftshift(HighPassCenter)\n    inverse_HighPass = np.fft.ifft2(HighPass)\n    axs[1].imshow(np.abs(inverse_HighPass), cmap='gray')\n    axs[1].set_title('Processed Image')\n    plt.suptitle(\"D0:\"+str(d0),fontweight=\"bold\")\n    plt.show()\n    \n    \nfor i in [100,50,30,20,10]:\n    try_d0s_hp(i)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T11:24:07.478738Z","iopub.execute_input":"2023-04-08T11:24:07.479108Z","iopub.status.idle":"2023-04-08T11:24:09.456746Z","shell.execute_reply.started":"2023-04-08T11:24:07.479074Z","shell.execute_reply":"2023-04-08T11:24:09.455350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/assignment-2-computer-vision/speckle/'\n\n# List all files in the directory\nfiles = os.listdir(path)\n\n# Create an empty list to store the images\nimages = []\n\n\nfig, axs = plt.subplots(1, 6, figsize=(15, 15))\ncnt = 0\n# Loop through each file in the directory\nfor file in files:\n    img = cv2.imread(path + file, cv2.IMREAD_GRAYSCALE)\n    # Append the image to the list\n    images.append(img)\n    axs[cnt].imshow(img, cmap='gray')\n    cnt = cnt + 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-08T11:24:09.459071Z","iopub.execute_input":"2023-04-08T11:24:09.459467Z","iopub.status.idle":"2023-04-08T11:24:10.326493Z","shell.execute_reply.started":"2023-04-08T11:24:09.459431Z","shell.execute_reply":"2023-04-08T11:24:10.324935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(1, 6, figsize=(15, 15))\ncnt = 0\n# apply gaussian blur for all images\nfor img in images:\n    img_filtered = cv2.GaussianBlur(img, (9, 9), 25)\n    axs[cnt].imshow(img_filtered, cmap='gray')\n    cnt = cnt + 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-08T11:25:40.799477Z","iopub.execute_input":"2023-04-08T11:25:40.800150Z","iopub.status.idle":"2023-04-08T11:25:41.957857Z","shell.execute_reply.started":"2023-04-08T11:25:40.800090Z","shell.execute_reply":"2023-04-08T11:25:41.956333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom as dicom\nimport matplotlib.pylab as plt\nimport imageio\nimport re\n\n\n# sorting image so the go incremental by number 1, 2, ... n\ndef sort_images(image_list):\n    regex_pattern = r'Image-(\\d+)\\.dcm'\n    return sorted(image_list, key=lambda x: int(re.search(regex_pattern, x).group(1)))\n\n# function for gathering all images and creating gif from them\ndef gatherFolderImageToGif(patientId, screenType):\n    folder_path = f'/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/test/{patientId}/{screenType}'\n    files = os.listdir(folder_path)\n    sorted_list = sort_images(files)\n    files = sorted_list\n    images = []\n    for file in files:\n        ds = dicom.dcmread(os.path.join(folder_path, file))\n        images.append(ds.pixel_array)\n    \n    output_file = f'output-{patientId}-{screenType}.gif'  # or 'output.mp4' for video\n    imageio.mimsave(output_file, images)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-08T11:24:11.142477Z","iopub.execute_input":"2023-04-08T11:24:11.142995Z","iopub.status.idle":"2023-04-08T11:24:11.154172Z","shell.execute_reply.started":"2023-04-08T11:24:11.142951Z","shell.execute_reply":"2023-04-08T11:24:11.152612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import HTML\n\n#calling the gif creating function with passing patient id and type of images\ngatherFolderImageToGif('00001', 'FLAIR')\ngatherFolderImageToGif('00001', 'T1w')\ngatherFolderImageToGif('00001', 'T1wCE')\ngatherFolderImageToGif('00001', 'T2w')\n\n#getting gifs\ngif_path1 = 'output-00001-FLAIR.gif'\ngif_path2 = 'output-00001-T1w.gif'\ngif_path3 = 'output-00001-T1wCE.gif'\ngif_path4 = 'output-00001-T2w.gif'\n\n#put images to html code with some styling to show them\nhtml_code = f'<div style=\"display: flex; flex-wrap: wrap;\"><img src=\"{gif_path1}\"> <img src=\"{gif_path2}\"> <img src=\"{gif_path3}\"> <img src=\"{gif_path4}\"></div>'\n\nHTML(html_code)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T11:24:11.156032Z","iopub.execute_input":"2023-04-08T11:24:11.156495Z","iopub.status.idle":"2023-04-08T11:24:26.760246Z","shell.execute_reply.started":"2023-04-08T11:24:11.156456Z","shell.execute_reply":"2023-04-08T11:24:26.758652Z"},"trusted":true},"execution_count":null,"outputs":[]}]}