{"cells":[{"metadata":{},"cell_type":"markdown","source":"## I tried image resizing using OpenCV function.\n## Please comment anything for improvement.\n\n### In & out\n- input : 137 x 236 size image\n- output : IMG_size x IMG_size (default IMG_size is 128)\n\n### Applying method\n- 1) Binary thresholding (cv2.threshold)\n- 2) Resizing with interpolation (cv2.resize)\n- 3) ROI centering (cv2.boundingRect)\n\n#### reference\n  - ref1 : https://opencv-python.readthedocs.io/en/latest/doc/09.imageThresholding/imageThresholding.html\n  - ref2 : https://www.kaggle.com/plsjlq/copy-bengali-ai-starter-eda-multi-output-cnn"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tqdm.auto import tqdm \nimport time, gc\nimport cv2\n\nimport matplotlib.pyplot as plt\n\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = ['/kaggle/input/bengaliai-cv19/train_image_data_0.parquet',\n         '/kaggle/input/bengaliai-cv19/train_image_data_1.parquet',\n         '/kaggle/input/bengaliai-cv19/train_image_data_2.parquet',\n         '/kaggle/input/bengaliai-cv19/train_image_data_3.parquet']\n\nIMG_width = 236\nIMG_height = 137\nIMG_size = 128","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train0 = pd.read_parquet(train[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train0.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"idx = np.random.randint(50410) # set random index\n\nimg = train0.iloc[idx, 1:].values\nprint(f\"data type is '{img.dtype}' and item size is {img.itemsize}\")\nimg = train0.iloc[idx, 1:].values.astype(np.uint8)\n\nprint(f\"data type is '{img.dtype}' and item size is {img.itemsize}\")\nimg = img.reshape(137,236)\nplt.imshow(img, cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pd.DataFrame(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def img_resize(img, size=IMG_size):\n    plt.figure(figsize=(20, 20))\n    plt.subplot(4,1,1)\n    plt.imshow(img, cmap='gray')\n    \"\"\"\n    <In & out>\n    input : 137 x 236 size image\n    output : IMG_size x IMG_size \n    \n    <Applying method>\n    # 1) Binary thresholding (cv2.threshold)\n    # 2) Resizing with interpolation (cv2.resize)\n    # 3) ROI centering (cv2.boundingRect)\n    \"\"\"\n    # 1) Binary thresholding (cv2.threshold)\n    \n    thr = cv2.THRESH_OTSU\n    #thr = cv2.THRESH_BINARY\n    thr_val, img1 = cv2.threshold(img, 240, 255, thr) # set a threshold : 15\n    img1 = 255 - img1\n    \n    plt.subplot(4,1,2)\n    plt.imshow(img1, cmap='gray')\n    \n    # 2) resizing with interpolation (cv2.resize)\n    \n    #interpol = cv2.INTER_NEAREST # it preferred for img zoom\n    interpol = cv2.INTER_AREA # it preferred for img decimation\n    img2 = cv2.resize(img1, (size,size), interpolation=interpol)\n        \n    plt.subplot(4,1,3)\n    plt.imshow(img2, cmap='gray')\n    \n        \n    # 3) ROI centering \n    \n    x,y,w,h = cv2.boundingRect(img2) # find bounding box of the character\n    ROI = img2[y:y+h, x:x+w]\n    img3 = np.zeros((size, size)) # make a canvas for new_img\n    center_x, center_y = x + w//2, y + h//2 # find center point of the rectangle\n    moving_x, moving_y = (size//2 - center_x), (size//2 - center_y)\n    new_x, new_y = x + moving_x, y + moving_y\n    \n    img3[new_y:new_y+h, new_x:new_x+w] = ROI\n        \n    plt.subplot(4,1,4)\n    plt.imshow(img3, cmap='gray')\n    \n    new_img = img3.reshape(-1) # reshape 2D image array into 1D array\n    new_img = pd.DataFrame(new_img).T # change array into DataFrame \n    \n    return new_img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_resize(img)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}