mirror of
https://github.com/NanjingForestryUniversity/supermachine--tomato-passion_fruit.git
synced 2025-11-08 22:34:00 +00:00
80 lines
2.1 KiB
Plaintext
80 lines
2.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"id": "initial_id",
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"ExecuteTime": {
|
|
"end_time": "2024-06-03T08:44:12.688855Z",
|
|
"start_time": "2024-06-03T08:44:07.661963Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"import cv2\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"# 读取彩色图像,使用原始字符串处理文件路径\n",
|
|
"image = cv2.imread(r\"D:\\project\\supermachine--tomato-passion_fruit\\20240529RGBtest3\\data\\bad\\36.bmp\")\n",
|
|
"\n",
|
|
"# 将RGB图像转换到HSV颜色空间\n",
|
|
"hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n",
|
|
"\n",
|
|
"# 将RGB图像转换到Lab颜色空间\n",
|
|
"lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)\n",
|
|
"\n",
|
|
"# 提取S分量\n",
|
|
"s_channel = hsv_image[:,:,1]\n",
|
|
"\n",
|
|
"# 提取L分量\n",
|
|
"l_channel = lab_image[:,:,0]\n",
|
|
"\n",
|
|
"# 计算S+L图像\n",
|
|
"sl_image = cv2.addWeighted(s_channel, 0.5, l_channel, 0.5, 0)\n",
|
|
"\n",
|
|
"# 使用Otsu阈值分割\n",
|
|
"_, otsu_segmentation = cv2.threshold(sl_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n",
|
|
"\n",
|
|
"# 显示原始图像和分割结果\n",
|
|
"cv2.imshow(\"Original Image\", image)\n",
|
|
"cv2.imshow(\"Otsu Segmentation\", otsu_segmentation)\n",
|
|
"cv2.waitKey(0)\n",
|
|
"cv2.destroyAllWindows()\n",
|
|
"#存图\n",
|
|
"# cv2.imwrite(r\"D:\\project\\supermachine--tomato-passion_fruit\\20240529RGBtest3\\33_otsu.bmp\", otsu_segmentation)\n"
|
|
],
|
|
"outputs": [],
|
|
"execution_count": 5
|
|
},
|
|
{
|
|
"metadata": {},
|
|
"cell_type": "code",
|
|
"outputs": [],
|
|
"execution_count": null,
|
|
"source": "",
|
|
"id": "29d27b11f43683db"
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 2
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"version": "2.7.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|