0% found this document useful (0 votes)
3 views

Untitled6

The document contains a Python code snippet that utilizes OpenCV and a segmentation model to process an image and count trees based on their green color. An error occurs during the execution due to an empty source image, which prevents the color conversion function from processing the image. The code initializes a mask generator and attempts to display the number of detected trees in the image, but fails at the image loading step.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Untitled6

The document contains a Python code snippet that utilizes OpenCV and a segmentation model to process an image and count trees based on their green color. An error occurs during the execution due to an empty source image, which prevents the color conversion function from processing the image. The code initializes a mask generator and attempts to display the number of detected trees in the image, but fails at the image loading step.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

{

"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "10607108-1314-48c8-954b-5676b4db0245",
"metadata": {},
"outputs": [
{
"ename": "error",
"evalue": "OpenCV(4.9.0) D:\\a\\opencv-python\\opencv-python\\opencv\\
modules\\imgproc\\src\\color.cpp:196: error: (-215:Assertion failed) !_src.empty()
in function 'cv::cvtColor'\n",
"output_type": "error",
"traceback": [
"\
u001b[1;31m------------------------------------------------------------------------
---\u001b[0m",
"\u001b[1;31merror\u001b[0m Traceback
(most recent call last)",
"Cell \u001b[1;32mIn[3], line 37\u001b[0m\n\u001b[0;32m 35\u001b[0m
image_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mr\u001b[39m\
u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mC:\u001b[39m\u001b[38;5;124m\\\
u001b[39m\u001b[38;5;124mUsers\u001b[39m\u001b[38;5;124m\\\u001b[39m\
u001b[38;5;124mRangga\u001b[39m\u001b[38;5;124m\\\u001b[39m\
u001b[38;5;124mDocuments\u001b[39m\u001b[38;5;124m\\\u001b[39m\
u001b[38;5;124mPROJECT_UPDATE\u001b[39m\u001b[38;5;124m\\\u001b[39m\
u001b[38;5;124mNEW_POTO_CROP_100X100.tif\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\
u001b[0;32m 36\u001b[0m image \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\
u001b[39mimread(image_path)\n\u001b[1;32m---> 37\u001b[0m image \u001b[38;5;241m=\
u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\
u001b[43mcvtColor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimage\u001b[49m\
u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcv2\u001b[49m\
u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCOLOR_BGR2RGB\u001b[49m\u001b[43m)\
u001b[49m\n\u001b[0;32m 39\u001b[0m \u001b[38;5;66;03m# Mendapatkan mask dari
model\u001b[39;00m\n\u001b[0;32m 40\u001b[0m masks \u001b[38;5;241m=\u001b[39m
mask_generator\u001b[38;5;241m.\u001b[39mgenerate(image)\n",
"\u001b[1;31merror\u001b[0m: OpenCV(4.9.0) D:\\a\\opencv-python\\opencv-
python\\opencv\\modules\\imgproc\\src\\color.cpp:196: error: (-215:Assertion
failed) !_src.empty() in function 'cv::cvtColor'\n"
]
}
],
"source": [
"import cv2\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import torch\n",
"import sys\n",
"import os\n",
"\n",
"# current_directory = os.getcwd()\n",
"\n",
"# Mengatur library kustom dan model dari SAM\n",
"sys.path.append(\"..\") # Sesuaikan dengan struktur folder Anda\n",
"from segment_anything import sam_model_registry, SamAutomaticMaskGenerator\n",
"\n",
"# Mengkonfigurasi model dan checkpoint\n",
"model_type = \"vit_h\"\n",
"checkpoint_path = r\"C:\\Users\\Rangga\\Documents\\PROJECT_UPDATE\\
sam_vit_h_4b8939.pth\" # Ganti dengan path yang sesuai\n",
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"\n",
"# Inisialisasi model SAM\n",
"sam = sam_model_registry[model_type](checkpoint=checkpoint_path)\n",
"sam.to(device=device)\n",
"\n",
"# Membuat mask generator\n",
"mask_generator = SamAutomaticMaskGenerator(\n",
" model=sam,\n",
" points_per_side=32,\n",
" pred_iou_thresh=0.9,\n",
" stability_score_thresh=0.20,\n",
" crop_n_layers=1,\n",
" crop_n_points_downscale_factor=2,\n",
" min_mask_region_area=10\n",
")\n",
"\n",
"# Memuat dan memproses gambar\n",
"image_path = r\"C:\\Users\\Rangga\\Documents\\PROJECT_UPDATE\\
NEW_POTO_CROP_100X100.tif\"\n",
"image = cv2.imread(image_path)\n",
"image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
"\n",
"# Mendapatkan mask dari model\n",
"masks = mask_generator.generate(image)\n",
"\n",
"# Fungsi untuk memfilter dan menghitung pohon berdasarkan warna hijau dominan\
n",
"def count_trees_based_on_color(masks, image):\n",
" tree_count = 0\n",
" for mask in masks:\n",
" segmented_image = cv2.bitwise_and(image, image,
mask=mask['segmentation'].astype(np.uint8))\n",
" green_channel = segmented_image[:, :, 1] # Mengambil hijau channel
hijau\n",
" greenness_ratio = np.sum(green_channel > 100) /
np.sum(mask['segmentation'])\n",
" \n",
" # Asumsikan jika rasio hijau cukup tinggi, itu adalah pohon\n",
" if greenness_ratio < 0.01:\n",
" tree_count += 1\n",
"\n",
" return tree_count\n",
"\n",
"# Menghitung pohon\n",
"tree_count = count_trees_based_on_color(masks, image)\n",
"print(f\"Jumlah pohon: {tree_count}\")\n",
"\n",
"# Menampilkan gambar\n",
"plt.figure(figsize=(10, 10))\n",
"plt.imshow(image)\n",
"plt.title(f\"Total Dead Trees: {tree_count}\")\n",
"plt.axis('off')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c75ebad-9215-4406-901e-78162f3a02ee",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

You might also like