{
  "id": 455282,
  "title": "Adjust the bounding box based on ROI of the image",
  "url": "/competitions/rsna-breast-cancer-detection/discussion/455282",
  "author_name": "Shantanu Ghosh",
  "post_date": "2023-11-14T04:27:22.418000",
  "votes": 2,
  "comment_count": 0,
  "views": 0,
  "content": "<p>I have an original image of size (3518, 2800) as shown in the uploaded figure original_image.png. Now i have cropped the breast from the image and resized it to (1520, 912) as shown in the uploaded figure cropped_image.png. Following is the code for this preprocessing:</p>\n<pre><code>def np_CountUpContinuingOnes(b_arr):\n    # indice continuing zeros from  side.\n    # : [,,,,,,,,,,] -&gt; [,,,,,,,,,,]\n     = np.arange((b_arr))\n    [b_arr &gt; ] = \n     = np.maximum.accumulate()\n    # from  side.\n    # : [,,,,,,,,,,] -&gt; [,,,,,,,,,,]\n    rev_arr = b_arr[::-]\n     = np.arange((rev_arr))\n    [rev_arr &gt; ] = \n     = np.maximum.accumulate()\n     = (rev_arr) -  - [::-]\n      -  - \n</code></pre>\n<pre><code>def ExtractBreast(img):\n    img_copy = img.()\n    img = .where(img &lt;= , , img)  # To detect backgrounds easily\n    ,  = img.shape\n\n    # whether each   non-  \n    y_a =  //  + int( * )\n    y_b =  //  - int( * )\n    b_arr = img[y_b:y_a].(axis=) != \n    continuing_ones = np_CountUpContinuingOnes(b_arr)\n    # longest should be the breast\n    col_ind = .where(continuing_ones == continuing_ones.())[]\n    img = img[:, col_ind]\n\n    # whether each   non-  \n    ,  = img.shape\n    x_a =  //  + int( * )\n    x_b =  //  - int( * )\n    b_arr = img[:, x_b:x_a].(axis=) != \n    continuing_ones = np_CountUpContinuingOnes(b_arr)\n    # longest should be the breast\n    row_ind = .where(continuing_ones == continuing_ones.())[]\n\n     img_copy[row_ind][:, col_ind]\n</code></pre>\n<pre><code> save_imgs(in_path, out_path, =(, )):\n    dicom = dicomsdl.open(in_path)\n    \n    \n     dicom.getPixelDataInfo()['] == :\n        \n\n    \n    \n    \n\n    img = (\n    img = cv2.resize(img, , interpolation=cv2.)\n    cv2.imwrite(out_path, img)\n\n    print(out_path)\n</code></pre>\n<p>I am calling the save_imgs function to convert the dicom image of (3518, 2800) to png image of (1520, 912). Now my groundtruth bouding boxes are not adjusted by this transformation. I am using the code to transform the bounding box:</p>\n<pre><code> =  / \n =  / \n\n\n = int(bounding_box[] * scale_x)\n = int(bounding_box[] * scale_y)\n = int(bounding_box[] * scale_x)\n = int(bounding_box[] * scale_y)\n</code></pre>\n<p>can someone please help to transform the bounding box accordingly?</p>\n<p>Here is original_image.png (extracted from dicom with size: (3518, 2800))<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4066763%2Fcf71c064ec2a7c31020fde467dfb5b8e%2Foriginal_image.png?generation=1699936113534261&amp;alt=media\" alt=\"![![\">](url to embed)](url to embed)</p>\n<p>Here is cropped_image.png (1520, 912)<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4066763%2F1364d78e48df9e0a07d471cfe8edf0b6%2Fcropped_image.png?generation=1699936125733184&amp;alt=media\" alt=\"\"></p>",
  "messages": [
    {
      "id": 2524207,
      "postDate": "2023-11-14T04:27:22.420Z",
      "content": "<p>I have an original image of size (3518, 2800) as shown in the uploaded figure original_image.png. Now i have cropped the breast from the image and resized it to (1520, 912) as shown in the uploaded figure cropped_image.png. Following is the code for this preprocessing:</p>\n<pre><code>def np_CountUpContinuingOnes(b_arr):\n    # indice continuing zeros from  side.\n    # : [,,,,,,,,,,] -&gt; [,,,,,,,,,,]\n     = np.arange((b_arr))\n    [b_arr &gt; ] = \n     = np.maximum.accumulate()\n    # from  side.\n    # : [,,,,,,,,,,] -&gt; [,,,,,,,,,,]\n    rev_arr = b_arr[::-]\n     = np.arange((rev_arr))\n    [rev_arr &gt; ] = \n     = np.maximum.accumulate()\n     = (rev_arr) -  - [::-]\n      -  - \n</code></pre>\n<pre><code>def ExtractBreast(img):\n    img_copy = img.()\n    img = .where(img &lt;= , , img)  # To detect backgrounds easily\n    ,  = img.shape\n\n    # whether each   non-  \n    y_a =  //  + int( * )\n    y_b =  //  - int( * )\n    b_arr = img[y_b:y_a].(axis=) != \n    continuing_ones = np_CountUpContinuingOnes(b_arr)\n    # longest should be the breast\n    col_ind = .where(continuing_ones == continuing_ones.())[]\n    img = img[:, col_ind]\n\n    # whether each   non-  \n    ,  = img.shape\n    x_a =  //  + int( * )\n    x_b =  //  - int( * )\n    b_arr = img[:, x_b:x_a].(axis=) != \n    continuing_ones = np_CountUpContinuingOnes(b_arr)\n    # longest should be the breast\n    row_ind = .where(continuing_ones == continuing_ones.())[]\n\n     img_copy[row_ind][:, col_ind]\n</code></pre>\n<pre><code> save_imgs(in_path, out_path, =(, )):\n    dicom = dicomsdl.open(in_path)\n    \n    \n     dicom.getPixelDataInfo()['] == :\n        \n\n    \n    \n    \n\n    img = (\n    img = cv2.resize(img, , interpolation=cv2.)\n    cv2.imwrite(out_path, img)\n\n    print(out_path)\n</code></pre>\n<p>I am calling the save_imgs function to convert the dicom image of (3518, 2800) to png image of (1520, 912). Now my groundtruth bouding boxes are not adjusted by this transformation. I am using the code to transform the bounding box:</p>\n<pre><code> =  / \n =  / \n\n\n = int(bounding_box[] * scale_x)\n = int(bounding_box[] * scale_y)\n = int(bounding_box[] * scale_x)\n = int(bounding_box[] * scale_y)\n</code></pre>\n<p>can someone please help to transform the bounding box accordingly?</p>\n<p>Here is original_image.png (extracted from dicom with size: (3518, 2800))<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4066763%2Fcf71c064ec2a7c31020fde467dfb5b8e%2Foriginal_image.png?generation=1699936113534261&amp;alt=media\" alt=\"![![\">](url to embed)](url to embed)</p>\n<p>Here is cropped_image.png (1520, 912)<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4066763%2F1364d78e48df9e0a07d471cfe8edf0b6%2Fcropped_image.png?generation=1699936125733184&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "I have an original image of size (3518, 2800) as shown in the uploaded figure original_image.png. Now i have cropped the breast from the image and resized it to (1520, 912) as shown in the uploaded figure cropped_image.png. Following is the code for this preprocessing:\n\n\n```\ndef np_CountUpContinuingOnes(b_arr):\n    # indice continuing zeros from left side.\n    # ex: [0,1,1,0,1,0,0,1,1,1,0] -> [0,0,0,3,3,5,6,6,6,6,10]\n    left = np.arange(len(b_arr))\n    left[b_arr > 0] = 0\n    left = np.maximum.accumulate(left)\n    # from right side.\n    # ex: [0,1,1,0,1,0,0,1,1,1,0] -> [0,3,3,3,5,5,6,10,10,10,10]\n    rev_arr = b_arr[::-1]\n    right = np.arange(len(rev_arr))\n    right[rev_arr > 0] = 0\n    right = np.maximum.accumulate(right)\n    right = len(rev_arr) - 1 - right[::-1]\n    return right - left - 1\n```\n\n```\ndef ExtractBreast(img):\n    img_copy = img.copy()\n    img = np.where(img <= 40, 0, img)  # To detect backgrounds easily\n    height, _ = img.shape\n\n    # whether each col is non-constant or not\n    y_a = height // 2 + int(height * 0.4)\n    y_b = height // 2 - int(height * 0.4)\n    b_arr = img[y_b:y_a].std(axis=0) != 0\n    continuing_ones = np_CountUpContinuingOnes(b_arr)\n    # longest should be the breast\n    col_ind = np.where(continuing_ones == continuing_ones.max())[0]\n    img = img[:, col_ind]\n\n    # whether each row is non-constant or not\n    _, width = img.shape\n    x_a = width // 2 + int(width * 0.4)\n    x_b = width // 2 - int(width * 0.4)\n    b_arr = img[:, x_b:x_a].std(axis=1) != 0\n    continuing_ones = np_CountUpContinuingOnes(b_arr)\n    # longest should be the breast\n    row_ind = np.where(continuing_ones == continuing_ones.max())[0]\n\n    return img_copy[row_ind][:, col_ind]\n\n```\n\n```\ndef save_imgs(in_path, out_path, SIZE=(912, 1520)):\n    dicom = dicomsdl.open(in_path)\n    data = dicom.pixelData()\n    data = data[5:-5, 5:-5]\n    if dicom.getPixelDataInfo()['PhotometricInterpretation'] == \"MONOCHROME1\":\n        data = np.amax(data) - data\n\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n\n    img = ExtractBreast(data)\n    img = cv2.resize(img, SIZE, interpolation=cv2.INTER_AREA)\n    cv2.imwrite(out_path, img)\n\n    print(out_path)\n```\n\nI am calling the save_imgs function to convert the dicom image of (3518, 2800) to png image of (1520, 912). Now my groundtruth bouding boxes are not adjusted by this transformation. I am using the code to transform the bounding box:\n```\nscale_x = 912 / 2800\nscale_y = 1520 / 3518\n\n# Resize the bounding box coordinates\nresized_xmin = int(bounding_box[0] * scale_x)\nresized_ymin = int(bounding_box[1] * scale_y)\nresized_xmax = int(bounding_box[2] * scale_x)\nresized_ymax = int(bounding_box[3] * scale_y)\n```\ncan someone please help to transform the bounding box accordingly?\n\nHere is original_image.png (extracted from dicom with size: (3518, 2800))\n![![![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4066763%2Fcf71c064ec2a7c31020fde467dfb5b8e%2Foriginal_image.png?generation=1699936113534261&alt=media)](url to embed)](url to embed)\n\nHere is cropped_image.png (1520, 912)\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4066763%2F1364d78e48df9e0a07d471cfe8edf0b6%2Fcropped_image.png?generation=1699936125733184&alt=media)",
      "votes": 2
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "2524207": "I have an original image of size (3518, 2800) as shown in the uploaded figure original_image.png. Now i have cropped the breast from the image and resized it to (1520, 912) as shown in the uploaded figure cropped_image.png. Following is the code for this preprocessing:\n\n\n```\ndef np_CountUpContinuingOnes(b_arr):\n    # indice continuing zeros from left side.\n    # ex: [0,1,1,0,1,0,0,1,1,1,0] -> [0,0,0,3,3,5,6,6,6,6,10]\n    left = np.arange(len(b_arr))\n    left[b_arr > 0] = 0\n    left = np.maximum.accumulate(left)\n    # from right side.\n    # ex: [0,1,1,0,1,0,0,1,1,1,0] -> [0,3,3,3,5,5,6,10,10,10,10]\n    rev_arr = b_arr[::-1]\n    right = np.arange(len(rev_arr))\n    right[rev_arr > 0] = 0\n    right = np.maximum.accumulate(right)\n    right = len(rev_arr) - 1 - right[::-1]\n    return right - left - 1\n```\n\n```\ndef ExtractBreast(img):\n    img_copy = img.copy()\n    img = np.where(img <= 40, 0, img)  # To detect backgrounds easily\n    height, _ = img.shape\n\n    # whether each col is non-constant or not\n    y_a = height // 2 + int(height * 0.4)\n    y_b = height // 2 - int(height * 0.4)\n    b_arr = img[y_b:y_a].std(axis=0) != 0\n    continuing_ones = np_CountUpContinuingOnes(b_arr)\n    # longest should be the breast\n    col_ind = np.where(continuing_ones == continuing_ones.max())[0]\n    img = img[:, col_ind]\n\n    # whether each row is non-constant or not\n    _, width = img.shape\n    x_a = width // 2 + int(width * 0.4)\n    x_b = width // 2 - int(width * 0.4)\n    b_arr = img[:, x_b:x_a].std(axis=1) != 0\n    continuing_ones = np_CountUpContinuingOnes(b_arr)\n    # longest should be the breast\n    row_ind = np.where(continuing_ones == continuing_ones.max())[0]\n\n    return img_copy[row_ind][:, col_ind]\n\n```\n\n```\ndef save_imgs(in_path, out_path, SIZE=(912, 1520)):\n    dicom = dicomsdl.open(in_path)\n    data = dicom.pixelData()\n    data = data[5:-5, 5:-5]\n    if dicom.getPixelDataInfo()['PhotometricInterpretation'] == \"MONOCHROME1\":\n        data = np.amax(data) - data\n\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n\n    img = ExtractBreast(data)\n    img = cv2.resize(img, SIZE, interpolation=cv2.INTER_AREA)\n    cv2.imwrite(out_path, img)\n\n    print(out_path)\n```\n\nI am calling the save_imgs function to convert the dicom image of (3518, 2800) to png image of (1520, 912). Now my groundtruth bouding boxes are not adjusted by this transformation. I am using the code to transform the bounding box:\n```\nscale_x = 912 / 2800\nscale_y = 1520 / 3518\n\n# Resize the bounding box coordinates\nresized_xmin = int(bounding_box[0] * scale_x)\nresized_ymin = int(bounding_box[1] * scale_y)\nresized_xmax = int(bounding_box[2] * scale_x)\nresized_ymax = int(bounding_box[3] * scale_y)\n```\ncan someone please help to transform the bounding box accordingly?\n\nHere is original_image.png (extracted from dicom with size: (3518, 2800))\n![![![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4066763%2Fcf71c064ec2a7c31020fde467dfb5b8e%2Foriginal_image.png?generation=1699936113534261&alt=media)](url to embed)](url to embed)\n\nHere is cropped_image.png (1520, 912)\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4066763%2F1364d78e48df9e0a07d471cfe8edf0b6%2Fcropped_image.png?generation=1699936125733184&alt=media)"
  }
}