mirror of
https://github.com/NanjingForestryUniversity/supermachine--tomato-passion_fruit.git
synced 2025-11-09 14:54:07 +00:00
405 lines
12 KiB
Plaintext
405 lines
12 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "initial_id",
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"ExecuteTime": {
|
|
"end_time": "2024-03-18T12:58:36.021344Z",
|
|
"start_time": "2024-03-18T12:58:35.489144Z"
|
|
}
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"import cv2\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"import numpy as np\n",
|
|
"import cv2 as cv\n",
|
|
"import os"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"<class 'str'>\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(39911424,)\n",
|
|
"(87, 2048, 224)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"[[[1686. 1654. 1654. ... 1654. 1622. 1654.]\n",
|
|
" [1689. 1673. 1673. ... 1689. 1689. 1673.]\n",
|
|
" [1673. 1657. 1657. ... 1673. 1753. 1609.]\n",
|
|
" ...\n",
|
|
" [1609. 1689. 1721. ... 1657. 1705. 1577.]\n",
|
|
" [1718. 1702. 1654. ... 1574. 1638. 1686.]\n",
|
|
" [1686. 1670. 1686. ... 1590. 1686. 1670.]]\n",
|
|
"\n",
|
|
" [[1654. 1670. 1718. ... 1686. 1654. 1734.]\n",
|
|
" [1721. 1657. 1641. ... 1689. 1657. 1721.]\n",
|
|
" [1641. 1625. 1705. ... 1705. 1689. 1609.]\n",
|
|
" ...\n",
|
|
" [1625. 1673. 1689. ... 1673. 1673. 1641.]\n",
|
|
" [1686. 1622. 1654. ... 1686. 1654. 1606.]\n",
|
|
" [1734. 1654. 1670. ... 1510. 1654. 1654.]]\n",
|
|
"\n",
|
|
" [[1718. 1702. 1670. ... 1622. 1670. 1686.]\n",
|
|
" [1657. 1641. 1641. ... 1705. 1689. 1657.]\n",
|
|
" [1673. 1673. 1689. ... 1673. 1625. 1657.]\n",
|
|
" ...\n",
|
|
" [1625. 1673. 1705. ... 1625. 1609. 1625.]\n",
|
|
" [1702. 1686. 1654. ... 1638. 1670. 1654.]\n",
|
|
" [1638. 1702. 1686. ... 1670. 1702. 1638.]]\n",
|
|
"\n",
|
|
" ...\n",
|
|
"\n",
|
|
" [[1638. 1606. 1686. ... 1654. 1670. 1638.]\n",
|
|
" [1721. 1673. 1673. ... 1689. 1641. 1625.]\n",
|
|
" [1641. 1641. 1673. ... 1657. 1641. 1625.]\n",
|
|
" ...\n",
|
|
" [1625. 1609. 1689. ... 1657. 1657. 1673.]\n",
|
|
" [1638. 1606. 1702. ... 1622. 1622. 1654.]\n",
|
|
" [1574. 1654. 1622. ... 1718. 1670. 1622.]]\n",
|
|
"\n",
|
|
" [[1590. 1686. 1638. ... 1654. 1654. 1638.]\n",
|
|
" [1641. 1657. 1609. ... 1673. 1657. 1641.]\n",
|
|
" [1721. 1657. 1641. ... 1657. 1673. 1577.]\n",
|
|
" ...\n",
|
|
" [1657. 1673. 1641. ... 1593. 1609. 1609.]\n",
|
|
" [1622. 1670. 1670. ... 1686. 1734. 1622.]\n",
|
|
" [1670. 1590. 1670. ... 1638. 1590. 1638.]]\n",
|
|
"\n",
|
|
" [[1654. 1654. 1638. ... 1606. 1606. 1558.]\n",
|
|
" [1689. 1657. 1689. ... 1657. 1625. 1673.]\n",
|
|
" [1657. 1561. 1657. ... 1657. 1673. 1609.]\n",
|
|
" ...\n",
|
|
" [1657. 1625. 1689. ... 1657. 1609. 1625.]\n",
|
|
" [1638. 1654. 1654. ... 1622. 1606. 1590.]\n",
|
|
" [1590. 1686. 1606. ... 1574. 1686. 1686.]]]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"##合成真彩色图像\n",
|
|
"#读取raw文件\n",
|
|
"imagePath ='data/new/yellow/1.raw' \n",
|
|
"print(type(imagePath))\n",
|
|
"rawImage = np.fromfile(imagePath,\"uint16\")\n",
|
|
"print(type(rawImage))\n",
|
|
"rawShape = rawImage.shape\n",
|
|
"print(rawShape)\n",
|
|
"formatImage = np.zeros((87,2048,224))\n",
|
|
"for row in range(0,87):\n",
|
|
" for dim in range(0,224):\n",
|
|
" formatImage[row,:,dim] = rawImage[(dim + row*224) * 2048:(dim + 1 + row*224)* 2048]\n",
|
|
"print(formatImage.shape)\n",
|
|
"print(type(formatImage))\n",
|
|
"print(formatImage)\n",
|
|
"\n",
|
|
"#选择通道数为12/46/96的三个通道相当于rgb三通道的图片\n",
|
|
"# imgR = formatImage[:,:,88]\n",
|
|
"# imgG = formatImage[:,:,58]\n",
|
|
"# imgB = formatImage[:,:,31]\n",
|
|
"# \n",
|
|
"# rgbImg = cv.merge([imgR,imgG,imgB])\n",
|
|
"# print(rgbImg.shape)\n",
|
|
"# print(type(rgbImg))\n",
|
|
"# cv.imshow('test',rgbImg)\n",
|
|
"# cv.waitKey(0)\n",
|
|
"# cv.destroyAllWindows()\n",
|
|
"# plt.imshow(rgbImg)\n",
|
|
"# #保存\n",
|
|
"# plt.savefig('result/1_ref.png')\n",
|
|
"\n"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2024-03-18T13:06:05.055643Z",
|
|
"start_time": "2024-03-18T13:06:04.869067Z"
|
|
}
|
|
},
|
|
"id": "3443037608cc60f6"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"<class 'str'>\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(50462720,)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"##合成真彩色图像\n",
|
|
"#读取raw文件\n",
|
|
"imagePath = 'data/数据/原光源(条光)/黄果(成熟)/1.raw' \n",
|
|
"print(type(imagePath))\n",
|
|
"rawImage = np.fromfile(imagePath,\"float16\")\n",
|
|
"print(type(rawImage))\n",
|
|
"rawShape = rawImage.shape\n",
|
|
"print(rawShape)\n",
|
|
"formatImage = np.zeros((110,2048,224))\n",
|
|
"# 假设 rawImage 已经正确读取\n",
|
|
"raw_length = len(rawImage) # 获取 rawImage 的实际长度\n",
|
|
"\n",
|
|
"for row in range(0, 110):\n",
|
|
" for dim in range(0, 224):\n",
|
|
" start_index = (dim + row*224) * 2048\n",
|
|
" end_index = (dim + 1 + row*224) * 2048\n",
|
|
" \n",
|
|
" # 检查索引是否在 rawImage 的边界内\n",
|
|
" if start_index < raw_length and end_index <= raw_length:\n",
|
|
" formatImage[row,:,dim] = rawImage[start_index:end_index]\n",
|
|
" else:\n",
|
|
" print(f\"索引超出范围: 行 {row}, 波段 {dim}\")\n",
|
|
"\n",
|
|
"# for row in range(0,110):\n",
|
|
"# for dim in range(0,224):\n",
|
|
"# # formatImage[row,:,dim] = rawImage[(dim + row*224) * 2048:(dim + 1 + row*224)* 2048]\n",
|
|
" \n",
|
|
"#选择通道数为12/46/96的三个通道相当于rgb三通道的图片\n",
|
|
"# imgR = formatImage[:,:,31]\n",
|
|
"# imgG = formatImage[:,:,58]\n",
|
|
"# imgB = formatImage[:,:,88]\n",
|
|
"# \n",
|
|
"# rgbImg = cv.merge([imgR,imgG,imgB])\n",
|
|
"# print(rgbImg.shape)\n",
|
|
"# print(type(rgbImg))\n",
|
|
"# cv.imshow('test',rgbImg)\n",
|
|
"# cv.waitKey(0)\n",
|
|
"# cv.destroyAllWindows()\n",
|
|
"# plt.imshow(rgbImg)\n",
|
|
"# #保存\n",
|
|
"# plt.savefig('result/1_ref.png')"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2024-03-18T03:26:37.014093Z",
|
|
"start_time": "2024-03-18T03:26:36.769031Z"
|
|
}
|
|
},
|
|
"id": "283f370b2401221b",
|
|
"execution_count": 5
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"(928, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(848, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(902, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(795, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(783, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(708, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(747, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(630, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(709, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(699, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(796, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(865, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(791, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(723, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(797, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(760, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(701, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(770, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(637, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(729, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(762, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(732, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(839, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(760, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(711, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(736, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(758, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(772, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(744, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(775, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(821, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(775, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(820, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(772, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(772, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(747, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(770, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(645, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n",
|
|
"(756, 1200, 3)\n",
|
|
"<class 'numpy.ndarray'>\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"##循环合成真彩色图像\n",
|
|
"# 指定文件夹路径\n",
|
|
"folder_path = 'data' # 替换成你的文件夹路径\n",
|
|
"# mask_out_path = 'result' # 替换成你的掩码图像文件夹路径\n",
|
|
"\n",
|
|
"# 获取文件夹中的所有文件\n",
|
|
"files = os.listdir(folder_path)\n",
|
|
"\n",
|
|
"\n",
|
|
"for i in range(1, 40):\n",
|
|
" with open(folder_path + '/' + str(i) + '_ref.hdr', '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",
|
|
" raw_image = np.fromfile(folder_path + '/' + str(i) + '_ref.raw', dtype='float32')\n",
|
|
" \n",
|
|
" formatImage = np.zeros((height, width, bands))\n",
|
|
" for row in range(0, height):\n",
|
|
" for dim in range(0, bands):\n",
|
|
" formatImage[row,:,dim] = raw_image[(dim + row*bands) * width:(dim + 1 + row*bands)* width]\n",
|
|
" #选择通道数为12/46/96的三个通道相当于rgb三通道的图片\n",
|
|
" imgR = formatImage[:,:,12]\n",
|
|
" imgG = formatImage[:,:,46]\n",
|
|
" imgB = formatImage[:,:,96]\n",
|
|
" \n",
|
|
" rgbImg = cv.merge([imgR,imgG,imgB])\n",
|
|
" print(rgbImg.shape)\n",
|
|
" print(type(rgbImg))\n",
|
|
" \n",
|
|
" Img_Name = \"./tcimage/\" + str(i)+ \".png\"\n",
|
|
" cv.imwrite(Img_Name, rgbImg*255)\n",
|
|
" # cv.imshow('test',rgbImg)\n",
|
|
" # cv.waitKey(0)\n",
|
|
" # cv.destroyAllWindows()\n",
|
|
"# #读取raw文件\n",
|
|
"# imagePath = 'data/1_ref.raw' \n",
|
|
"# print(type(imagePath))\n",
|
|
"# rawImage = np.fromfile(imagePath,\"float32\")\n",
|
|
"# print(type(rawImage))\n",
|
|
"# rawShape = rawImage.shape\n",
|
|
"# print(rawShape)\n",
|
|
"# formatImage = np.zeros((928,1200,224))\n",
|
|
"# for row in range(0,928):\n",
|
|
"# for dim in range(0,224):\n",
|
|
"# formatImage[row,:,dim] = rawImage[(dim + row*224) * 1200:(dim + 1 + row*224)* 1200]\n",
|
|
"# #选择通道数为12/46/96的三个通道相当于rgb三通道的图片\n",
|
|
"# imgR = formatImage[:,:,12]\n",
|
|
"# imgG = formatImage[:,:,46]\n",
|
|
"# imgB = formatImage[:,:,96]\n",
|
|
" \n",
|
|
"# rgbImg = cv.merge([imgR,imgG,imgB])\n",
|
|
"# print(rgbImg.shape)\n",
|
|
"# print(type(rgbImg))\n",
|
|
"# cv.imshow('test',rgbImg)\n",
|
|
"# cv.waitKey(0)\n",
|
|
"# cv.destroyAllWindows()\n",
|
|
"# plt.imshow(rgbImg)\n",
|
|
"# #保存\n",
|
|
"# plt.savefig('result/1_ref.png')\n",
|
|
"\n"
|
|
],
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"ExecuteTime": {
|
|
"end_time": "2023-11-24T02:56:13.173734400Z",
|
|
"start_time": "2023-11-24T02:50:24.958389400Z"
|
|
}
|
|
},
|
|
"id": "15a3889f7bf1917f"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"outputs": [],
|
|
"source": [],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "17b929b3de3fa2ed"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"outputs": [],
|
|
"source": [],
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"id": "e00c8e929ee57b60"
|
|
}
|
|
],
|
|
"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
|
|
}
|