supermachine--tomato-passio.../spectrum/01test.ipynb

293 lines
14 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2023-11-20T11:13:32.538976100Z",
"start_time": "2023-11-20T11:13:31.425375100Z"
}
},
"outputs": [
{
"ename": "AxisError",
"evalue": "axis 2 is out of bounds for array of dimension 1",
"output_type": "error",
"traceback": [
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[1;31mAxisError\u001B[0m Traceback (most recent call last)",
"Cell \u001B[1;32mIn[8], line 49\u001B[0m\n\u001B[0;32m 46\u001B[0m tomato_spectrum_image \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39mzeros_like(spectrum_image)\n\u001B[0;32m 48\u001B[0m \u001B[38;5;66;03m# 将掩码图像扩展到和光谱图像形状相同,以便进行按位与操作\u001B[39;00m\n\u001B[1;32m---> 49\u001B[0m mask \u001B[38;5;241m=\u001B[39m \u001B[43mnp\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mstack\u001B[49m\u001B[43m(\u001B[49m\u001B[43m[\u001B[49m\u001B[43mmask\u001B[49m\u001B[43m]\u001B[49m\u001B[38;5;241;43m*\u001B[39;49m\u001B[43mbands\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43maxis\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;241;43m2\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[0;32m 51\u001B[0m \u001B[38;5;66;03m# 使用掩码提取西红柿区域的光谱图像\u001B[39;00m\n\u001B[0;32m 52\u001B[0m tomato_spectrum_image \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39mwhere(mask \u001B[38;5;241m==\u001B[39m \u001B[38;5;241m255\u001B[39m, spectrum_image, \u001B[38;5;241m0\u001B[39m)\n",
"File \u001B[1;32mD:\\TG\\Miniconda3\\envs\\tengg\\lib\\site-packages\\numpy\\core\\shape_base.py:452\u001B[0m, in \u001B[0;36mstack\u001B[1;34m(arrays, axis, out, dtype, casting)\u001B[0m\n\u001B[0;32m 449\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mall input arrays must have the same shape\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m 451\u001B[0m result_ndim \u001B[38;5;241m=\u001B[39m arrays[\u001B[38;5;241m0\u001B[39m]\u001B[38;5;241m.\u001B[39mndim \u001B[38;5;241m+\u001B[39m \u001B[38;5;241m1\u001B[39m\n\u001B[1;32m--> 452\u001B[0m axis \u001B[38;5;241m=\u001B[39m \u001B[43mnormalize_axis_index\u001B[49m\u001B[43m(\u001B[49m\u001B[43maxis\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mresult_ndim\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 454\u001B[0m sl \u001B[38;5;241m=\u001B[39m (\u001B[38;5;28mslice\u001B[39m(\u001B[38;5;28;01mNone\u001B[39;00m),) \u001B[38;5;241m*\u001B[39m axis \u001B[38;5;241m+\u001B[39m (_nx\u001B[38;5;241m.\u001B[39mnewaxis,)\n\u001B[0;32m 455\u001B[0m expanded_arrays \u001B[38;5;241m=\u001B[39m [arr[sl] \u001B[38;5;28;01mfor\u001B[39;00m arr \u001B[38;5;129;01min\u001B[39;00m arrays]\n",
"\u001B[1;31mAxisError\u001B[0m: axis 2 is out of bounds for array of dimension 1"
]
}
],
"source": [
"import numpy as np\n",
"import cv2\n",
"import os\n",
"\n",
"# 指定文件夹路径\n",
"folder_path = 'data' # 替换成你的文件夹路径\n",
"mask_folder_path = 'result' # 替换成你的掩码图像文件夹路径\n",
"\n",
"# 获取文件夹中的所有文件\n",
"files = os.listdir(folder_path)\n",
"\n",
"# 遍历每个文件\n",
"for file in files:\n",
" # 检查文件是否是hdr文件\n",
" if file.endswith('.hdr'):\n",
" # 构建完整的hdr文件路径\n",
" hdr_file_path = os.path.join(folder_path, file)\n",
"\n",
" # 读取hdr文件\n",
" with open(hdr_file_path, 'r') as hdr_file:\n",
" lines = hdr_file.readlines()\n",
" for line in lines:\n",
" if line.startswith('lines'):\n",
" height = int(line.split()[-1])\n",
" elif line.startswith('samples'):\n",
" width = int(line.split()[-1])\n",
" elif line.startswith('bands'):\n",
" bands = int(line.split()[-1])\n",
"\n",
" # 构建对应的raw文件路径\n",
" raw_file_path = os.path.splitext(hdr_file_path)[0] + '.raw'\n",
"\n",
" # 读取raw光谱图像\n",
" raw_image = np.fromfile(raw_file_path, dtype='float32')\n",
"\n",
" # 使用从hdr文件中读取的宽度、高度和波段数来重塑raw光谱图像的形状\n",
" spectrum_image = raw_image.reshape((height, width, bands))\n",
"\n",
" # 构建对应的掩码图像路径\n",
" mask_file_path = os.path.join(mask_folder_path, os.path.splitext(file)[0] + '.tiff')\n",
"\n",
" # 读取掩码图像假设掩码图像是一个二值图像西红柿的区域为255其他区域为0\n",
" mask = cv2.imread(mask_file_path, cv2.IMREAD_GRAYSCALE)\n",
"\n",
" # 创建一个和光谱图像形状相同的全零数组,用于存储西红柿区域的光谱图像\n",
" tomato_spectrum_image = np.zeros_like(spectrum_image)\n",
"\n",
" # 将掩码图像扩展到和光谱图像形状相同,以便进行按位与操作\n",
" mask = np.stack([mask]*bands, axis=2)\n",
"\n",
" # 使用掩码提取西红柿区域的光谱图像\n",
" tomato_spectrum_image = np.where(mask == 255, spectrum_image, 0)\n",
"\n",
" # 计算每个谱段上西红柿的光谱信息均值\n",
" average_values_tomato = np.mean(tomato_spectrum_image, axis=(0, 1))\n",
"\n",
" # 打印结果\n",
" print(f\"文件 {file} 中西红柿区域各波段的平均值:\", average_values_tomato)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" ...\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]]\n",
"\n",
" [[0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" ...\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]]\n",
"\n",
" [[0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" ...\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]]\n",
"\n",
" ...\n",
"\n",
" [[0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" ...\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]]\n",
"\n",
" [[0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" ...\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]]\n",
"\n",
" [[0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" ...\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]\n",
" [0. 0. 0. ... 0. 0. 0.]]]\n",
"西红柿区域各波段的平均值: [0.02331245 0.02331355 0.02331434 0.02330351 0.02328778 0.02329406\n",
" 0.02326228 0.02324953 0.02320422 0.02319987 0.02320639 0.02320258\n",
" 0.02320204 0.02322337 0.02322935 0.02323991 0.02326387 0.02327245\n",
" 0.02326593 0.02327108 0.02324165 0.02325363 0.02322353 0.02321509\n",
" 0.02317422 0.02317277 0.02317923 0.02317507 0.02318152 0.02320312\n",
" 0.02320597 0.02322854 0.02325159 0.02326882 0.02326178 0.02326993\n",
" 0.02324535 0.02325787 0.02323463 0.02322856 0.02319151 0.02319442\n",
" 0.02320041 0.02319903 0.02320681 0.02323294 0.02324102 0.02326111\n",
" 0.02328818 0.02330373 0.02330286 0.02330293 0.02328876 0.02330017\n",
" 0.02327968 0.0232756 0.0232363 0.02323936 0.02324224 0.02324493\n",
" 0.02325153 0.02327927 0.02328969 0.02330778 0.02333499 0.02334016\n",
" 0.02334813 0.02334237 0.0233291 0.02333678 0.02331344 0.02330263\n",
" 0.02326433 0.02325943 0.02326216 0.02326128 0.02326241 0.02328353\n",
" 0.023289 0.02329529 0.0233208 0.02332475 0.02332811 0.02332091\n",
" 0.02330128 0.02331001 0.02327851 0.02326897 0.02322166 0.02321757\n",
" 0.02322467 0.02322131 0.02321952 0.02323928 0.02324408 0.02325601\n",
" 0.02328104 0.02329077 0.02328341 0.02329056 0.02326275 0.02327192\n",
" 0.02324721 0.02323763 0.02319732 0.02319809 0.02320318 0.02319734\n",
" 0.0232041 0.02322625 0.02322948 0.02325055 0.02327887 0.02329449\n",
" 0.02329181 0.02329812 0.02327645 0.02328718 0.02326863 0.02325965\n",
" 0.02322663 0.02322855 0.02323177 0.02323323 0.02324393 0.02327164\n",
" 0.02328169 0.0233025 0.02333108 0.02334195 0.02334395 0.0233406\n",
" 0.02332718 0.02333372 0.02331317 0.02330532 0.02326252 0.02326255\n",
" 0.02326744 0.02326685 0.02326851 0.02328742 0.02329235 0.02330284\n",
" 0.02332182 0.02332215 0.02332292 0.02331406 0.02329648 0.02330398\n",
" 0.02327215 0.02326116 0.02321737 0.02320999 0.02321728 0.02321167\n",
" 0.02320731 0.02323294 0.02323689 0.02324381 0.02326807 0.02327303\n",
" 0.02327289 0.02326986 0.02324365 0.02325905 0.02322375 0.02321331\n",
" 0.02317007 0.02316415 0.02317228 0.02316663 0.02316599 0.02318855\n",
" 0.02318928 0.02320436 0.02322696 0.02323972 0.02323037 0.02323674\n",
" 0.02321082 0.02322122 0.02319865 0.02318867 0.02314706 0.02315172\n",
" 0.02315546 0.02315242 0.02316325 0.02318312 0.02318851 0.02321093\n",
" 0.02323843 0.02325755 0.0232583 0.02326134 0.02324466 0.02325552\n",
" 0.02324029 0.02323927 0.02320264 0.0232074 0.02321127 0.02321607\n",
" 0.02322524 0.02325411 0.02326476 0.0232875 0.02331666 0.02332832\n",
" 0.02333939 0.02333211 0.02332272 0.0233288 0.02330828 0.02329813\n",
" 0.0232588 0.02325521 0.02325736 0.02325919 0.02325801 0.02327962\n",
" 0.02328656 0.02329215]\n"
]
}
],
"source": [
"import numpy as np\n",
"import cv2\n",
"\n",
"# 指定raw光谱图像和mask掩码图像的路径\n",
"raw_file_path = 'data/39_ref.raw' # 替换成你的raw光谱图像路径\n",
"mask_file_path = 'result/39.tiff' # 替换成你的mask掩码图像路径\n",
"\n",
"# 读取raw光谱图像\n",
"raw_image = np.fromfile(raw_file_path, dtype='float32')\n",
"\n",
"# 使用图像的宽度、高度和波段数来重塑raw光谱图像的形状\n",
"height = 756 # 替换成你的图像高度\n",
"width = 1200 # 替换成你的图像宽度\n",
"bands = 224 # 替换成你的图像波段数\n",
"spectrum_image = raw_image.reshape((height, width, bands))\n",
"\n",
"# 读取mask掩码图像假设掩码图像是一个二值图像西红柿的区域为255其他区域为0\n",
"mask = cv2.imread(mask_file_path, cv2.IMREAD_GRAYSCALE)\n",
"\n",
"# 创建一个和光谱图像形状相同的全零数组,用于存储西红柿区域的光谱图像\n",
"tomato_spectrum_image = np.zeros_like(spectrum_image)\n",
"\n",
"# 将掩码图像扩展到和光谱图像形状相同,以便进行按位与操作\n",
"mask = np.stack([mask]*bands, axis=2)\n",
"\n",
"# 使用掩码提取西红柿区域的光谱图像\n",
"tomato_spectrum_image = np.where(mask == 255, spectrum_image, 0)\n",
"print(tomato_spectrum_image)\n",
"# 计算每个谱段上西红柿的光谱信息均值\n",
"average_values_tomato = np.mean(tomato_spectrum_image, axis=(0, 1))\n",
"\n",
"# 打印结果\n",
"print(\"西红柿区域各波段的平均值:\", average_values_tomato)\n",
"import pandas as pd\n",
"\n",
"# 创建一个DataFrame来存储结果\n",
"df = pd.DataFrame(average_values_tomato, columns=['Average Spectral Values'])\n",
"\n",
"# # 将DataFrame导出为Excel文件\n",
"# df.to_excel('39.xlsx', index=False)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-20T13:35:21.812410400Z",
"start_time": "2023-11-20T13:35:17.418971100Z"
}
},
"id": "8bee30bdaaf3385e"
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"# 创建一个DataFrame来存储结果\n",
"df = pd.DataFrame(average_values_tomato, columns=['Average Spectral Values'])\n",
"\n",
"# 将DataFrame导出为Excel文件\n",
"df.to_excel('average_values_tomato.xlsx', index=False)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-11-20T11:14:35.425251700Z",
"start_time": "2023-11-20T11:14:34.752973600Z"
}
},
"id": "28ec09f66baecac8"
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
},
"id": "5d455acb1d6881e0"
}
],
"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
}