{"id":18995128,"url":"https://github.com/fitushar/automatic_breast_region_extraction_using_python","last_synced_at":"2026-05-01T12:32:19.985Z","repository":{"id":109982418,"uuid":"281278795","full_name":"fitushar/Automatic_Breast_Region_Extraction_using_python","owner":"fitushar","description":"This Repo will contain Python Implementation of an Automatic approach to Extraction Breast Region from Memogram","archived":false,"fork":false,"pushed_at":"2020-07-21T05:30:07.000Z","size":54495,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-10T11:14:26.756Z","etag":null,"topics":["breast-region-extraction","cv","cv2","image-processing","medical-image-analysis","medical-image-processing","medical-image-segmentation","python","tomography"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fitushar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-21T02:50:22.000Z","updated_at":"2024-05-03T19:14:24.000Z","dependencies_parsed_at":"2023-04-21T06:35:55.409Z","dependency_job_id":null,"html_url":"https://github.com/fitushar/Automatic_Breast_Region_Extraction_using_python","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fitushar/Automatic_Breast_Region_Extraction_using_python","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitushar%2FAutomatic_Breast_Region_Extraction_using_python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitushar%2FAutomatic_Breast_Region_Extraction_using_python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitushar%2FAutomatic_Breast_Region_Extraction_using_python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitushar%2FAutomatic_Breast_Region_Extraction_using_python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fitushar","download_url":"https://codeload.github.com/fitushar/Automatic_Breast_Region_Extraction_using_python/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fitushar%2FAutomatic_Breast_Region_Extraction_using_python/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32497811,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"online","status_checked_at":"2026-05-01T02:00:05.856Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["breast-region-extraction","cv","cv2","image-processing","medical-image-analysis","medical-image-processing","medical-image-segmentation","python","tomography"],"created_at":"2024-11-08T17:28:34.987Z","updated_at":"2026-05-01T12:32:19.958Z","avatar_url":"https://github.com/fitushar.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Automatic_Breast_Region_Extraction_using_python_OpenCV\nBraest region extraction from the memogram often the initaial stage to do image analysis or dignosis. This Repo contains Python Implementation of an Automatic approach to Extraction Breast Region from Memogram. All you need to do is to set the threshold based on your memogram vendor, Different vendors could have different ranges\n\nHere is public dataset to try the code: https://www.dclunie.com/pixelmedimagearchive/upmcdigitalmammotomocollection/index.html\n\n\n### Breast Region Extraction Algorithm works as following: Jupyter notebook have all the codes\n```ruby  \n   i) Load Dicom and Threshold based on your vendor\n  ii) Connected Component Analaysis and Keep the Largest Mask\n iii) Erode to remove unwanted object in memo.\n  iv) Label Erode Image and Replace all Non-ROI with background pixel\n   v) Finally dilat the mask to recover the eroded Breast Region\n```  \n\n![Simple Preocess](https://github.com/fitushar/Automatic_Breast_Region_Extraction_using_python/blob/master/Figure/Process.png)\n\n## Code is below\n```ruby\n## import Libraries\nimport cv2\nimport numpy as np\nimport os, glob\nimport sys\nimport pydicom\nfrom scipy.misc import imsave,imread\nimport pandas as pd\nfrom skimage import measure\nfrom skimage.measure import label\nimport matplotlib.pyplot as plt\n\n## Getting The Brest_Refion_using_Connected_Component_Analaysis\ndef select_largest_obj(img_bin, lab_val=255, smooth_boundary=False, kernel_size=15):\n    n_labels, img_labeled, lab_stats, _ = cv2.connectedComponentsWithStats(img_bin, connectivity=8, ltype=cv2.CV_32S)\n    largest_obj_lab_roi = np.argmax(lab_stats[1:, 4]) + 1\n    largest_mask_roi = np.zeros(img_bin.shape, dtype=np.uint8)\n    largest_mask_roi[img_labeled == largest_obj_lab_roi] = lab_val\n    if smooth_boundary:\n        kernel_ = np.ones((kernel_size, kernel_size), dtype=np.uint8)\n        largest_mask_roi = cv2.morphologyEx(largest_mask_roi, cv2.MORPH_OPEN, kernel_)\n    return largest_mask_roi\n\n## Dicom Fliping_name\ndef Flip_memo(dicom_file):\n    img=dicom_file.pixel_array\n    try:\n        flip=dicom_file[0x0018,0x7034].value.lower()\n        if 'y' in flip:\n            img=np.fliplr(img)\n    except:\n        pass\n    return img\n\n## Generate Breast Region Bases on Threshold Image\ndef Generate_Brest_roi_mask(dcm_img):\n    Threshold_img = np.array(dcm_img, dtype=np.uint8)\n    mask = select_largest_obj(Threshold_img , lab_val=255, smooth_boundary=True, kernel_size=15)\n    #Erosion\n    Erosion_kernel = np.ones((125,125),np.uint8)\n    Erosion = cv2.erode(mask,Erosion_kernel,iterations =1)\n    #Label-The Image\n    lab_mask_init = label(Erosion,background=0)\n    lab_ind,counts_ind = np.unique(lab_mask_init,return_counts  = True)\n    #-Shorting the label\n    uni_feat,counts_feat = np.unique(lab_mask_init,return_counts = True)\n    sorted_feat = uni_feat[np.flipud(np.argsort(counts_feat))]\n    sorted_counts = np.flipud(np.sort(counts_feat))\n    \n    if sorted_feat[0]==0:\n        memo_region_index=sorted_feat[1]\n    else:\n        memo_region_index=sorted_feat[0]\n    black=np.where((lab_mask_init!=memo_region_index))\n    #black=np.where((lab_mask_init!=1))\n    Erosion[black]=0\n    # Finally Filling some regions\n    Erosion=Erosion.astype('bool').astype('uint8')\n    Dilat_kernel= np.ones((125,125),np.uint8)\n    dilat = cv2.dilate(Erosion,Dilat_kernel,iterations =1)\n    # if you want to refine the mask more than do one more step dilation\n    #Dilat_kernel2= np.ones((10,10),np.uint8)\n    #Dilat_kernel2=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(25,25))\n    #dilat = cv2.dilate(dilat,Dilat_kernel2,iterations =1)\n    return dilat\n#plot    \ndef Ploting_function(dcm,dcm_roi):\n\n    f, axarr = plt.subplots(1,3,figsize=(30,30));\n    axarr[0].imshow(dcm, cmap=plt.cm.gray);\n    axarr[0].axis('off')\n    axarr[0].set_title('Memo',fontsize = 30)\n\n    axarr[1].imshow(dcm_roi, cmap='gray',alpha=1);\n    axarr[1].axis('off')\n    axarr[1].set_title('Breast Region mask',fontsize = 30)\n\n    axarr[2].imshow(dcm, cmap='gray',alpha=1);\n    axarr[2].imshow(dcm_roi, cmap='copper',alpha=0.5);\n    axarr[2].axis('off')\n    axarr[2].set_title('Breast ROI Overlay',fontsize = 30)\n\n    plt.tight_layout()\n    #plt.close()\n    return\n```\n\n### Sample Run:\n```ruby\none_dcm='1.3.6.1.4.1.5962.99.1.2280943358.716200484.1363785608958.532.0.dcm'\nds  =pydicom.read_file(one_dcm)\ndcm = Flip_memo(ds)\nret,thresh = cv2.threshold(dcm,60,255,0)\nBreast_roi=Generate_Brest_roi_mask(thresh)\nPloting_function(dcm,Breast_roi)\n```\n![Simple Preocess](https://github.com/fitushar/Automatic_Breast_Region_Extraction_using_python/blob/master/Figure/demo1.png)\n![Simple Preocess](https://github.com/fitushar/Automatic_Breast_Region_Extraction_using_python/blob/master/Figure/demo2.png)\n![Simple Preocess](https://github.com/fitushar/Automatic_Breast_Region_Extraction_using_python/blob/master/Figure/demo3.png)\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffitushar%2Fautomatic_breast_region_extraction_using_python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffitushar%2Fautomatic_breast_region_extraction_using_python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffitushar%2Fautomatic_breast_region_extraction_using_python/lists"}