{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport cv2\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-09T18:54:40.920475Z","iopub.execute_input":"2022-03-09T18:54:40.921224Z","iopub.status.idle":"2022-03-09T18:54:41.275338Z","shell.execute_reply.started":"2022-03-09T18:54:40.921183Z","shell.execute_reply":"2022-03-09T18:54:41.274402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Whole Concept is base on color majority in 4x4 sectors, if white then switch colors.\n#### CORRECTION: there were large digits so the 2nd cleaner counts pixels on border and then switch colors","metadata":{}},{"cell_type":"code","source":"def sector_cleaner_1st(img):\n    img=img/255\n    for i in range(4):\n        for j in range(4):\n            if np.mean(img[1000*i:1000*(i+1),1000*j:1000*(j+1)])>0.5:\n                img[1000*i:1000*(i+1),1000*j:1000*(j+1)]=np.abs(img[1000*i:1000*(i+1),1000*j:1000*(j+1)]-1)              \n    return img\ndef sector_cleaner_2nd(img):\n    img=img/255\n    for i in range(4):\n        for j in range(4):\n            border = img[1000*i:1000*(i+1),min(1000*j,3999)].sum() +img[1000*i:1000*(i+1),1000*(j+1)-1].sum() +img[min(1000*i,3999),1000*j:1000*(j+1)].sum()+img[1000*(i+1)-1,1000*j:1000*(j+1)].sum()\n            if border>=2000:\n                img[1000*i:1000*(i+1),1000*j:1000*(j+1)]=np.abs(img[1000*i:1000*(i+1),1000*j:1000*(j+1)]-1)\n            \n    return img","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:25:30.528257Z","iopub.execute_input":"2022-03-09T19:25:30.528604Z","iopub.status.idle":"2022-03-09T19:25:30.543836Z","shell.execute_reply.started":"2022-03-09T19:25:30.528540Z","shell.execute_reply":"2022-03-09T19:25:30.542351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1st version - counting area","metadata":{}},{"cell_type":"code","source":"img_before0 = cv2.imread('/kaggle/input/ultra-mnist/train/aageiauxqt.jpeg', cv2.IMREAD_GRAYSCALE)\n\nimg_after0 = sector_cleaner_1st(img_before0)\n\nimg_before1 = cv2.imread('/kaggle/input/ultra-mnist/train/aayphxjdiw.jpeg', cv2.IMREAD_GRAYSCALE)\n\nimg_after1 = sector_cleaner_1st(img_before1)\n\nimg_before2 = cv2.imread('/kaggle/input/ultra-mnist/train/aejalacjek.jpeg', cv2.IMREAD_GRAYSCALE)\n\nimg_after2 = sector_cleaner_1st(img_before2)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:25:32.877056Z","iopub.execute_input":"2022-03-09T19:25:32.877598Z","iopub.status.idle":"2022-03-09T19:25:33.398545Z","shell.execute_reply.started":"2022-03-09T19:25:32.877529Z","shell.execute_reply":"2022-03-09T19:25:33.397635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(3, 2, figsize=(8, 8))  \n\nax[0,0].imshow(img_before0)\nax[0,1].imshow(img_after0)\nax[1,0].imshow(img_before1)\nax[1,1].imshow(img_after1)\nax[2,0].imshow(img_before2)\nax[2,1].imshow(img_after2)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:25:34.584876Z","iopub.execute_input":"2022-03-09T19:25:34.585170Z","iopub.status.idle":"2022-03-09T19:25:41.622954Z","shell.execute_reply.started":"2022-03-09T19:25:34.585135Z","shell.execute_reply":"2022-03-09T19:25:41.621904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2nd version - counting borders","metadata":{}},{"cell_type":"code","source":"img_before0 = cv2.imread('/kaggle/input/ultra-mnist/train/aageiauxqt.jpeg', cv2.IMREAD_GRAYSCALE)\n\nimg_after0 = sector_cleaner_2nd(img_before0)\n\nimg_before1 = cv2.imread('/kaggle/input/ultra-mnist/train/aayphxjdiw.jpeg', cv2.IMREAD_GRAYSCALE)\n\nimg_after1 = sector_cleaner_2nd(img_before1)\n\nimg_before2 = cv2.imread('/kaggle/input/ultra-mnist/train/aejalacjek.jpeg', cv2.IMREAD_GRAYSCALE)\n\nimg_after2 = sector_cleaner_2nd(img_before2)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:26:10.734539Z","iopub.execute_input":"2022-03-09T19:26:10.734917Z","iopub.status.idle":"2022-03-09T19:26:11.233492Z","shell.execute_reply.started":"2022-03-09T19:26:10.734884Z","shell.execute_reply":"2022-03-09T19:26:11.232410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(3, 2, figsize=(8, 8))  \n\nax[0,0].imshow(img_before0)\nax[0,1].imshow(img_after0)\nax[1,0].imshow(img_before1)\nax[1,1].imshow(img_after1)\nax[2,0].imshow(img_before2)\nax[2,1].imshow(img_after2)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T19:26:12.535583Z","iopub.execute_input":"2022-03-09T19:26:12.535871Z","iopub.status.idle":"2022-03-09T19:26:19.791600Z","shell.execute_reply.started":"2022-03-09T19:26:12.535839Z","shell.execute_reply":"2022-03-09T19:26:19.790888Z"},"trusted":true},"execution_count":null,"outputs":[]}]}