{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom PIL import Image\nimport cv2\n\nfrom random import randint\nimport torch\nimport torch.nn as nn\nimport torchvision\nimport torchvision.transforms as transforms\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.optim.lr_scheduler import ReduceLROnPlateau\n\nimport json","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-11T10:25:53.239025Z","iopub.execute_input":"2021-07-11T10:25:53.239572Z","iopub.status.idle":"2021-07-11T10:25:53.726099Z","shell.execute_reply.started":"2021-07-11T10:25:53.239512Z","shell.execute_reply":"2021-07-11T10:25:53.725222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"../input/cassava-leaf-disease-classification/train_images/1000015157.jpg\"\n#path = \"\"../input/tiny-imagenet/tiny-imagenet-200/train/n01443537/images/n01443537_0.JPEG\"\"","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:25:53.731462Z","iopub.execute_input":"2021-07-11T10:25:53.731900Z","iopub.status.idle":"2021-07-11T10:25:53.736381Z","shell.execute_reply.started":"2021-07-11T10:25:53.731857Z","shell.execute_reply":"2021-07-11T10:25:53.735316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(path)","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:25:53.737830Z","iopub.execute_input":"2021-07-11T10:25:53.738088Z","iopub.status.idle":"2021-07-11T10:25:53.944887Z","shell.execute_reply.started":"2021-07-11T10:25:53.738064Z","shell.execute_reply":"2021-07-11T10:25:53.944108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = cv2.imread(path)\nprint(im)","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:25:53.946119Z","iopub.execute_input":"2021-07-11T10:25:53.946406Z","iopub.status.idle":"2021-07-11T10:25:53.962244Z","shell.execute_reply.started":"2021-07-11T10:25:53.946378Z","shell.execute_reply":"2021-07-11T10:25:53.961278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(im[0][0][0])","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:25:53.963445Z","iopub.execute_input":"2021-07-11T10:25:53.963769Z","iopub.status.idle":"2021-07-11T10:25:53.968830Z","shell.execute_reply.started":"2021-07-11T10:25:53.963739Z","shell.execute_reply":"2021-07-11T10:25:53.967912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#2 filters 3x3\nf1 = [[1,0,-1], \n      [1,0,-1], \n      [1,0,-1]]\nf2 = [[1,1,1], \n      [0,0,0], \n      [-1,-1,-1]]\n\n#2 filters of Sobel\nf_x = [[-1,0,1], \n      [-2,0,2], \n      [-1,0,1]]\n\nf_y = [[1,2,1], \n      [0,0,0], \n      [-1,-2,-1]]\n\nEDGE_ENHANCE_MORE = [[-1, -1, -1],\n                     [-1,  9, -1],\n                     [-1, -1, -1]]\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:25:53.969819Z","iopub.execute_input":"2021-07-11T10:25:53.970093Z","iopub.status.idle":"2021-07-11T10:25:53.980182Z","shell.execute_reply.started":"2021-07-11T10:25:53.970066Z","shell.execute_reply":"2021-07-11T10:25:53.978935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(path)\nimg = img.resize((600,600))\nimg.save(\"test.jpg\")\nImage.open(\"test.jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:25:53.981298Z","iopub.execute_input":"2021-07-11T10:25:53.981557Z","iopub.status.idle":"2021-07-11T10:25:54.162025Z","shell.execute_reply.started":"2021-07-11T10:25:53.981531Z","shell.execute_reply":"2021-07-11T10:25:54.161103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = cv2.imread(\"test.jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:25:54.164279Z","iopub.execute_input":"2021-07-11T10:25:54.164561Z","iopub.status.idle":"2021-07-11T10:25:54.174461Z","shell.execute_reply.started":"2021-07-11T10:25:54.164534Z","shell.execute_reply":"2021-07-11T10:25:54.173306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = 3\np = 0\ns = 1\n\nn = len(im)\n\nfinal_size = int((n + 2 * p - f)/s + 1) # in this case it's equal to n - f + 1 (cause of p = 0 and s = 1)","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:25:54.176012Z","iopub.execute_input":"2021-07-11T10:25:54.176293Z","iopub.status.idle":"2021-07-11T10:25:54.181204Z","shell.execute_reply.started":"2021-07-11T10:25:54.176266Z","shell.execute_reply":"2021-07-11T10:25:54.180213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n, m = final_size, final_size\narr = [[[0 for p in range(f)] for j in range(m)] for i in range(n)]\n\nu, g, summ = 0, 0, 0\n\ndef func(i):\n    for k in range(f):\n            for l in range(f):\n                summ += im[l + u][k + g][0] * f1[l][k]\n    return summ\n\nfor i in range(final_size):\n    for j in range(final_size):\n        #R range\n        for k in range(f):\n            for l in range(f):\n                summ += im[l + u][k + g][0] * f1[l][k]\n\n        arr[i][j][0] = summ \n        \n        summ = 0\n\n        #G range\n        for k in range(f):\n            for l in range(f):\n                summ += im[l + u][k + g][1] * f1[l][k]\n                \n        arr[i][j][1] = summ \n        \n        summ = 0\n        \n        #B range\n        for k in range(f):\n            for l in range(f):\n                summ += im[l + u][k + g][2] * f1[l][k]\n        \n        arr[i][j][2] = summ\n        \n        g += s\n        if(g >= final_size):\n            u += s\n            g = 0\n            if(u >= final_size):\n                u = 0\n        \n#print(arr)\n\ncv2.imwrite(\"test.jpg\", np.array(arr))","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:25:54.182366Z","iopub.execute_input":"2021-07-11T10:25:54.182661Z","iopub.status.idle":"2021-07-11T10:26:52.413759Z","shell.execute_reply.started":"2021-07-11T10:25:54.182622Z","shell.execute_reply":"2021-07-11T10:26:52.412862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(\"test.jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:26:52.414973Z","iopub.execute_input":"2021-07-11T10:26:52.415239Z","iopub.status.idle":"2021-07-11T10:26:52.582549Z","shell.execute_reply.started":"2021-07-11T10:26:52.415213Z","shell.execute_reply":"2021-07-11T10:26:52.581663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = cv2.imread(\"test.jpg\")\n\nn = len(im)\nfinal_size = int((n + 2 * p - f)/s + 1)\n\nu, g, best = 0, 0, 0\n\nfor i in range(final_size):\n    for j in range(final_size):\n        #R range\n        for k in range(f):\n            for l in range(f):\n                if(im[l + u][k + g][0] > best): best = im[l + u][k + g][0]\n\n        arr[i][j][0] = best \n        \n        best = 0\n\n        #G range\n        for k in range(f):\n            for l in range(f):\n                if(im[l + u][k + g][1] > best): best = im[l + u][k + g][1]\n                \n        arr[i][j][1] = best\n        \n        best = 0\n        \n        #B range\n        for k in range(f):\n            for l in range(f):\n                if(im[l + u][k + g][2] > best): best = im[l + u][k + g][2]\n        \n        arr[i][j][2] = best\n        \n        g += s\n        if(g >= final_size):\n            u += s\n            g = 0\n            if(u >= final_size):\n                u = 0\n        \n#print(arr)\n\ncv2.imwrite(\"test.jpg\", np.array(arr))","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:26:52.583809Z","iopub.execute_input":"2021-07-11T10:26:52.584090Z","iopub.status.idle":"2021-07-11T10:27:13.900984Z","shell.execute_reply.started":"2021-07-11T10:26:52.584064Z","shell.execute_reply":"2021-07-11T10:27:13.899908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(\"test.jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-07-11T10:27:13.902127Z","iopub.execute_input":"2021-07-11T10:27:13.902402Z","iopub.status.idle":"2021-07-11T10:27:14.047413Z","shell.execute_reply.started":"2021-07-11T10:27:13.902378Z","shell.execute_reply":"2021-07-11T10:27:14.046647Z"},"trusted":true},"execution_count":null,"outputs":[]}]}