{"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":"markdown","source":"# Connected Component Analysis","metadata":{}},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport os,glob\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-07-02T15:54:48.552226Z","iopub.execute_input":"2023-07-02T15:54:48.552606Z","iopub.status.idle":"2023-07-02T15:54:48.557606Z","shell.execute_reply.started":"2023-07-02T15:54:48.552577Z","shell.execute_reply":"2023-07-02T15:54:48.55668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir=\"/kaggle/input/images/\"\nfor file in os.listdir(dir):\n    t1 = np.load(dir+file)\n    t = np.uint8( t1)\n    _, labels = cv2.connectedComponents(t)\n    #print(labels.max())\n    num_components = labels.max()\n    \n    min_component_size = 7*7\n\n    # Iterate through each connected component and label it with a different color\n    output = np.zeros_like(t, dtype=np.uint8)\n    for label in range(1, num_components + 1):\n        component_mask = labels == label\n        component_size = np.sum(component_mask)\n        if component_size >= min_component_size:\n            output[labels == label] = 1\n    plt.figure(figsize=(18, 6))\n    ax =plt.subplot(1, 2,1)\n    ax.imshow(t1)\n    ax.set_title(file)\n    ax =plt.subplot(1, 2,2)\n    ax.imshow(output)\n    ax.set_title(\"CCA\")\n    ","metadata":{"execution":{"iopub.status.busy":"2023-07-02T15:59:35.430444Z","iopub.execute_input":"2023-07-02T15:59:35.43128Z","iopub.status.idle":"2023-07-02T15:59:36.643062Z","shell.execute_reply.started":"2023-07-02T15:59:35.43124Z","shell.execute_reply":"2023-07-02T15:59:36.641582Z"},"trusted":true},"execution_count":null,"outputs":[]}]}