当前位置:   article > 正文

MATLAB读取高光谱数据_matlab如何读取光谱数据

matlab如何读取光谱数据
  1. clear
  2. close all
  3. clc
  4. hdr_path_name = 'G:\cs\DATA_lyx_1.hdr';
  5. fid = fopen(hdr_path_name,'r');
  6. info = fread(fid,'char=>char');
  7. info = info';
  8. fclose(fid);
  9. start = strfind(info,'samples = ');
  10. len = length('samples = ');
  11. stop = strfind(info,'lines ');
  12. samples = [];
  13. for i = start+len : stop-1
  14. samples = [samples,info(i)];
  15. end
  16. samples = str2num(samples);
  17. start = strfind(info,'lines = ');
  18. len = length('lines = ');
  19. stop = strfind(info,'bands');
  20. lines = [];
  21. for i = start+len : stop-1
  22. lines = [lines,info(i)];
  23. end
  24. lines = str2num(lines);
  25. start = strfind(info,'bands = ');
  26. len = length('bands = ');
  27. stop = strfind(info,'header offset');
  28. bands = [];
  29. for i = start+len : stop-1
  30. bands = [bands, info(i)];
  31. end
  32. bands = str2num(bands);
  33. start = strfind(info,'data type = ');
  34. len = length('data type = ');
  35. stop = strfind(info,'interleave');
  36. datatype = [];
  37. for i = start+len : stop-1
  38. datatype = [datatype, info(i)];
  39. end
  40. datatype = str2num(datatype);
  41. precision = [];
  42. switch datatype
  43. case 1
  44. precision = 'uint8 => uint8';
  45. case 2
  46. precision = 'int16 => int16';
  47. case 12
  48. precision = 'uint16 => uint16';
  49. case 3
  50. precision = 'int32 => int32';
  51. case 13
  52. precision = 'uint32 => uint32';
  53. case 4
  54. precision = 'float32 => float32';
  55. case 5
  56. precision = 'double => double';
  57. otherwise
  58. precision = 'invalid type';
  59. end
  60. start = strfind(info,'interleave = ');
  61. len = length('interleave = ');
  62. stop = strfind(info,'sensor type');
  63. interleave = [];
  64. for i = start+len : stop-1
  65. interleave = [interleave, info(i)];
  66. end
  67. interleave = strtrim(interleave);
  68. data = multibandread('G:\cs\DATA_lyx_1.bsq',[lines,samples,bands],precision,0,interleave,'ieee-le',{'Row','Range',[1 256]},{'Column','Range',[1 256]});

需要注意的几个点(影响代码跑通的最关键几个因素):

hdr_path_name = 'G:\cs\DATA_lyx_1.hdr';

此处的hdr文件目录要换成自己所存储的高光谱图像目录

data = multibandread('G:\cs\DATA_lyx_1.bsq',[lines,samples,bands],precision,0,interleave,'ieee-le',{'Row','Range',[1 256]},{'Column','Range',[1 256]});

此处的图像路径也要改为自己的文件路径

关于{'Row','Range',[1 256]},{'Column','Range',[1 256]}中的256不是绝对的,这个是由图像的samples和lines来决定的,也可能是512,1024等。可以打开hdr文件查看

参考:【Matlab】读取".hdr"文件_matlab读取hdr文件_帅气的益达的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/338622
推荐阅读
相关标签
  

闽ICP备14008679号