{
  "id": 126505,
  "title": "A new image preprocessing method",
  "url": "/competitions/bengaliai-cv19/discussion/126505",
  "author_name": "MachineLP",
  "post_date": "2020-01-18T02:03:53.821000",
  "votes": 5,
  "comment_count": 0,
  "views": 0,
  "content": "<p>```python\nimport os\nimport pandas as pd\nimport numpy as np\nimport cv2</p>\n\n<p>HEIGHT = 137\nWIDTH = 236\nTARGET_SIZE = 256\nINPUT_PATH = \"../input/data-science-bowl-2019/\"</p>\n\n<p>def add_img_padding(img):\n    h, w, _ = img.shape\n    width = np.max([h, w])\n    img_padding = np.zeros([width, width,3])\n    h1 = int(width/2-h/2)\n    h2 = int(width/2+h/2)\n    w1 = int(width/2-w/2)\n    w2 = int(width/2+w/2)\n    img_padding[h1:h2, w1:w2, :] = img[0:(h2-h1),0:(w2-w1),:]\n    return img_padding</p>\n\n<p>def detect(image):\n    im = cv2.GaussianBlur(image, (5, 5), 0)\n    im = cv2.Canny(im, 1, 130)\n    nonzero = np.nonzero(im)</p>\n\n<pre><code>if len(nonzero[0]) &amp;lt;= 4:\n    return None\n\nh_set = nonzero[0]\nw_set = nonzero[1]\nw_min = w_set[np.argmax(-w_set, axis=0)]\nw_max = w_set[np.argmax(w_set, axis=0)]\nh_min = h_set[np.argmax(-h_set, axis=0)]\nh_max = h_set[np.argmax(h_set, axis=0)]\n\nreturn [w_min,h_min,w_max-w_min+1,h_max-h_min+1]\n</code></pre>\n\n<p>tp = 10\ndef img_crop(img, box):\n    # y1, x1, y2, x2 = box[1]-20, box[0]-20, box[1]+box[3]+40, box[0]+box[2]+40\n    y1, x1, y2, x2 = box[1]-tp, box[0]-tp, box[1]+box[3]+tp, box[0]+box[2]+tp\n    img = img[y1:y2, x1:x2]\n    return img</p>\n\n<p>def make_square(img, target_size=256):\n    img = img[0:-1, :]\n    height, width = img.shape</p>\n\n<pre><code>x = target_size\ny = target_size\n\nsquare = np.ones((x, y), np.uint8) * 255\nsquare[(y - height) // 2:y - (y - height) // 2, (x - width) // 2:x - (x - width) // 2] = img\n\nreturn square\n</code></pre>\n\n<p>for idx in range(4):\n    parquet_file = INPUT_PATH + '/train_image_data_{}.parquet'.format(idx)\n    data = pd.read_parquet(parquet_file)\n    for idx in range( len(data) ):\n        tmp = data.iloc[idx, 1:].values.reshape(HEIGHT, WIDTH)\n        img = np.zeros((TARGET_SIZE, TARGET_SIZE, 3))\n        img[..., 0] = make_square(tmp, target_size=TARGET_SIZE)\n        img[..., 1] = make_square(tmp, target_size=TARGET_SIZE) #img[..., 0]\n        img[..., 2] = make_square(tmp, target_size=TARGET_SIZE) #img[..., 0]\n        image_id = data.iloc[idx, 0]\n        label1 = np.array( train_label[train_label['image_id']==image_id]['vowel_diacritic'] )[0]\n        label2 = np.array( train_label[train_label['image_id']==image_id]['grapheme_root'] )[0]\n        label3 = np.array( train_label[train_label['image_id']==image_id]['consonant_diacritic'] )[0]\n        print ( image_id, label1, label2, label3 )\n        img0 = np.array(img, dtype=np.uint8)</p>\n\n<pre><code>    gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)\n    box = detect(gray)\n    print('=======&amp;gt;', box)\n    img = img_crop(img0, box)\n    #print ('=======&amp;gt;',img.shape)\n    img = 255 - img\n    img = np.array(img, dtype=np.uint8)\n    img = add_img_padding(img)\n\n    return img\n</code></pre>\n\n<p>```\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F649893%2F5e803b72a5c1bb2750d3d6d25c66dd98%2FTrain_190374.jpg?generation=1579313192611295&amp;alt=media\" alt=\"\"></p>",
  "messages": [
    {
      "id": 722016,
      "postDate": "2020-01-18T02:03:53.820Z",
      "content": "<p>```python\nimport os\nimport pandas as pd\nimport numpy as np\nimport cv2</p>\n\n<p>HEIGHT = 137\nWIDTH = 236\nTARGET_SIZE = 256\nINPUT_PATH = \"../input/data-science-bowl-2019/\"</p>\n\n<p>def add_img_padding(img):\n    h, w, _ = img.shape\n    width = np.max([h, w])\n    img_padding = np.zeros([width, width,3])\n    h1 = int(width/2-h/2)\n    h2 = int(width/2+h/2)\n    w1 = int(width/2-w/2)\n    w2 = int(width/2+w/2)\n    img_padding[h1:h2, w1:w2, :] = img[0:(h2-h1),0:(w2-w1),:]\n    return img_padding</p>\n\n<p>def detect(image):\n    im = cv2.GaussianBlur(image, (5, 5), 0)\n    im = cv2.Canny(im, 1, 130)\n    nonzero = np.nonzero(im)</p>\n\n<pre><code>if len(nonzero[0]) &amp;lt;= 4:\n    return None\n\nh_set = nonzero[0]\nw_set = nonzero[1]\nw_min = w_set[np.argmax(-w_set, axis=0)]\nw_max = w_set[np.argmax(w_set, axis=0)]\nh_min = h_set[np.argmax(-h_set, axis=0)]\nh_max = h_set[np.argmax(h_set, axis=0)]\n\nreturn [w_min,h_min,w_max-w_min+1,h_max-h_min+1]\n</code></pre>\n\n<p>tp = 10\ndef img_crop(img, box):\n    # y1, x1, y2, x2 = box[1]-20, box[0]-20, box[1]+box[3]+40, box[0]+box[2]+40\n    y1, x1, y2, x2 = box[1]-tp, box[0]-tp, box[1]+box[3]+tp, box[0]+box[2]+tp\n    img = img[y1:y2, x1:x2]\n    return img</p>\n\n<p>def make_square(img, target_size=256):\n    img = img[0:-1, :]\n    height, width = img.shape</p>\n\n<pre><code>x = target_size\ny = target_size\n\nsquare = np.ones((x, y), np.uint8) * 255\nsquare[(y - height) // 2:y - (y - height) // 2, (x - width) // 2:x - (x - width) // 2] = img\n\nreturn square\n</code></pre>\n\n<p>for idx in range(4):\n    parquet_file = INPUT_PATH + '/train_image_data_{}.parquet'.format(idx)\n    data = pd.read_parquet(parquet_file)\n    for idx in range( len(data) ):\n        tmp = data.iloc[idx, 1:].values.reshape(HEIGHT, WIDTH)\n        img = np.zeros((TARGET_SIZE, TARGET_SIZE, 3))\n        img[..., 0] = make_square(tmp, target_size=TARGET_SIZE)\n        img[..., 1] = make_square(tmp, target_size=TARGET_SIZE) #img[..., 0]\n        img[..., 2] = make_square(tmp, target_size=TARGET_SIZE) #img[..., 0]\n        image_id = data.iloc[idx, 0]\n        label1 = np.array( train_label[train_label['image_id']==image_id]['vowel_diacritic'] )[0]\n        label2 = np.array( train_label[train_label['image_id']==image_id]['grapheme_root'] )[0]\n        label3 = np.array( train_label[train_label['image_id']==image_id]['consonant_diacritic'] )[0]\n        print ( image_id, label1, label2, label3 )\n        img0 = np.array(img, dtype=np.uint8)</p>\n\n<pre><code>    gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)\n    box = detect(gray)\n    print('=======&amp;gt;', box)\n    img = img_crop(img0, box)\n    #print ('=======&amp;gt;',img.shape)\n    img = 255 - img\n    img = np.array(img, dtype=np.uint8)\n    img = add_img_padding(img)\n\n    return img\n</code></pre>\n\n<p>```\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F649893%2F5e803b72a5c1bb2750d3d6d25c66dd98%2FTrain_190374.jpg?generation=1579313192611295&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "```python\nimport os\nimport pandas as pd\nimport numpy as np\nimport cv2\n\nHEIGHT = 137\nWIDTH = 236\nTARGET_SIZE = 256\nINPUT_PATH = \"../input/data-science-bowl-2019/\"\n\n\ndef add_img_padding(img):\n    h, w, _ = img.shape\n    width = np.max([h, w])\n    img_padding = np.zeros([width, width,3])\n    h1 = int(width/2-h/2)\n    h2 = int(width/2+h/2)\n    w1 = int(width/2-w/2)\n    w2 = int(width/2+w/2)\n    img_padding[h1:h2, w1:w2, :] = img[0:(h2-h1),0:(w2-w1),:]\n    return img_padding\n\ndef detect(image):\n    im = cv2.GaussianBlur(image, (5, 5), 0)\n    im = cv2.Canny(im, 1, 130)\n    nonzero = np.nonzero(im)\n\n    if len(nonzero[0]) &lt;= 4:\n        return None\n\n    h_set = nonzero[0]\n    w_set = nonzero[1]\n    w_min = w_set[np.argmax(-w_set, axis=0)]\n    w_max = w_set[np.argmax(w_set, axis=0)]\n    h_min = h_set[np.argmax(-h_set, axis=0)]\n    h_max = h_set[np.argmax(h_set, axis=0)]\n\n    return [w_min,h_min,w_max-w_min+1,h_max-h_min+1]\n\ntp = 10\ndef img_crop(img, box):\n    # y1, x1, y2, x2 = box[1]-20, box[0]-20, box[1]+box[3]+40, box[0]+box[2]+40\n    y1, x1, y2, x2 = box[1]-tp, box[0]-tp, box[1]+box[3]+tp, box[0]+box[2]+tp\n    img = img[y1:y2, x1:x2]\n    return img\n\ndef make_square(img, target_size=256):\n    img = img[0:-1, :]\n    height, width = img.shape\n\n    x = target_size\n    y = target_size\n\n    square = np.ones((x, y), np.uint8) * 255\n    square[(y - height) // 2:y - (y - height) // 2, (x - width) // 2:x - (x - width) // 2] = img\n\n    return square\n\nfor idx in range(4):\n    parquet_file = INPUT_PATH + '/train_image_data_{}.parquet'.format(idx)\n    data = pd.read_parquet(parquet_file)\n    for idx in range( len(data) ):\n        tmp = data.iloc[idx, 1:].values.reshape(HEIGHT, WIDTH)\n        img = np.zeros((TARGET_SIZE, TARGET_SIZE, 3))\n        img[..., 0] = make_square(tmp, target_size=TARGET_SIZE)\n        img[..., 1] = make_square(tmp, target_size=TARGET_SIZE) #img[..., 0]\n        img[..., 2] = make_square(tmp, target_size=TARGET_SIZE) #img[..., 0]\n        image_id = data.iloc[idx, 0]\n        label1 = np.array( train_label[train_label['image_id']==image_id]['vowel_diacritic'] )[0]\n        label2 = np.array( train_label[train_label['image_id']==image_id]['grapheme_root'] )[0]\n        label3 = np.array( train_label[train_label['image_id']==image_id]['consonant_diacritic'] )[0]\n        print ( image_id, label1, label2, label3 )\n        img0 = np.array(img, dtype=np.uint8)\n\n        gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)\n        box = detect(gray)\n        print('=======&gt;', box)\n        img = img_crop(img0, box)\n        #print ('=======&gt;',img.shape)\n        img = 255 - img\n        img = np.array(img, dtype=np.uint8)\n        img = add_img_padding(img)\n\n        return img\n```\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F649893%2F5e803b72a5c1bb2750d3d6d25c66dd98%2FTrain_190374.jpg?generation=1579313192611295&amp;alt=media)\n\n",
      "votes": 5
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "722016": "```python\nimport os\nimport pandas as pd\nimport numpy as np\nimport cv2\n\nHEIGHT = 137\nWIDTH = 236\nTARGET_SIZE = 256\nINPUT_PATH = \"../input/data-science-bowl-2019/\"\n\n\ndef add_img_padding(img):\n    h, w, _ = img.shape\n    width = np.max([h, w])\n    img_padding = np.zeros([width, width,3])\n    h1 = int(width/2-h/2)\n    h2 = int(width/2+h/2)\n    w1 = int(width/2-w/2)\n    w2 = int(width/2+w/2)\n    img_padding[h1:h2, w1:w2, :] = img[0:(h2-h1),0:(w2-w1),:]\n    return img_padding\n\ndef detect(image):\n    im = cv2.GaussianBlur(image, (5, 5), 0)\n    im = cv2.Canny(im, 1, 130)\n    nonzero = np.nonzero(im)\n\n    if len(nonzero[0]) &lt;= 4:\n        return None\n\n    h_set = nonzero[0]\n    w_set = nonzero[1]\n    w_min = w_set[np.argmax(-w_set, axis=0)]\n    w_max = w_set[np.argmax(w_set, axis=0)]\n    h_min = h_set[np.argmax(-h_set, axis=0)]\n    h_max = h_set[np.argmax(h_set, axis=0)]\n\n    return [w_min,h_min,w_max-w_min+1,h_max-h_min+1]\n\ntp = 10\ndef img_crop(img, box):\n    # y1, x1, y2, x2 = box[1]-20, box[0]-20, box[1]+box[3]+40, box[0]+box[2]+40\n    y1, x1, y2, x2 = box[1]-tp, box[0]-tp, box[1]+box[3]+tp, box[0]+box[2]+tp\n    img = img[y1:y2, x1:x2]\n    return img\n\ndef make_square(img, target_size=256):\n    img = img[0:-1, :]\n    height, width = img.shape\n\n    x = target_size\n    y = target_size\n\n    square = np.ones((x, y), np.uint8) * 255\n    square[(y - height) // 2:y - (y - height) // 2, (x - width) // 2:x - (x - width) // 2] = img\n\n    return square\n\nfor idx in range(4):\n    parquet_file = INPUT_PATH + '/train_image_data_{}.parquet'.format(idx)\n    data = pd.read_parquet(parquet_file)\n    for idx in range( len(data) ):\n        tmp = data.iloc[idx, 1:].values.reshape(HEIGHT, WIDTH)\n        img = np.zeros((TARGET_SIZE, TARGET_SIZE, 3))\n        img[..., 0] = make_square(tmp, target_size=TARGET_SIZE)\n        img[..., 1] = make_square(tmp, target_size=TARGET_SIZE) #img[..., 0]\n        img[..., 2] = make_square(tmp, target_size=TARGET_SIZE) #img[..., 0]\n        image_id = data.iloc[idx, 0]\n        label1 = np.array( train_label[train_label['image_id']==image_id]['vowel_diacritic'] )[0]\n        label2 = np.array( train_label[train_label['image_id']==image_id]['grapheme_root'] )[0]\n        label3 = np.array( train_label[train_label['image_id']==image_id]['consonant_diacritic'] )[0]\n        print ( image_id, label1, label2, label3 )\n        img0 = np.array(img, dtype=np.uint8)\n\n        gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)\n        box = detect(gray)\n        print('=======&gt;', box)\n        img = img_crop(img0, box)\n        #print ('=======&gt;',img.shape)\n        img = 255 - img\n        img = np.array(img, dtype=np.uint8)\n        img = add_img_padding(img)\n\n        return img\n```\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F649893%2F5e803b72a5c1bb2750d3d6d25c66dd98%2FTrain_190374.jpg?generation=1579313192611295&amp;alt=media)\n\n"
  }
}