; ===== 主程序:读取、显示单波段影像并计算统计信息 =====
pro binary, filename
; 检查文件是否存在
if ~file_test(filename) then begin
print, '错误: 文件不存在 - ', filename
return
endif
; 根据文件扩展名选择读取方法
file_ext = strmid(filename, strpos(filename, '.', /reverse_search) + 1)
file_ext = strlowcase(file_ext)
; 处理JPG/JPEG图像
if (file_ext eq 'jpg') or (file_ext eq 'jpeg') then begin
; 读取JPG图像
read_jpeg, filename, image, /quiet
; 如果是RGB图像,转换为灰度
if (size(image, /n_dimensions) eq 3) then begin
print, '检测到RGB图像,转换为灰度...'
image = reform(0.299*image[0,*,*] + 0.587*image[1,*,*] + 0.114*image[2,*,*])
endif
endif
; 处理TIFF图像
if (file_ext eq 'tif') or (file_ext eq 'tiff') then begin
; 读取TIFF图像
image = read_tiff(filename)
; 如果是多波段图像,取第一个波段
if (size(image, /n_dimensions) gt 2) then begin
print, '检测到多波段图像,使用第一个波段...'
image = image[*,*,0]
endif
endif
; 处理不支持的格式
if (file_ext ne 'jpg') and (file_ext ne 'jpeg') and (file_ext ne 'tif') and (file_ext ne 'tiff') then begin
print, '错误: 不支持的文件格式 - ', file_ext
return
endif
; 获取图像尺寸
dims = size(image)
cols = dims[1]
rows = dims[2]
; 创建窗口显示图像(按实际尺寸)
window, /free, xsize=cols, ysize=rows, title='图像显示: ' + filename
; 显示图像(根据数据类型调整显示范围)
data_type = size(image, /type)
data_type = fix(data_type)
; 使用单独的IF语句而不是ELSE IF
if data_type eq 1 then begin
tv, bytscl(image) ; byte类型
endif
if data_type eq 2 then begin
tv, bytscl(image, min=min(image), max=max(image)) ; int类型
endif
if data_type eq 3 then begin
tv, bytscl(image, min=min(image), max=max(image)) ; long类型
endif
if data_type eq 4 then begin
tv, bytscl(image) ; float类型
endif
if data_type eq 5 then begin
tv, bytscl(image) ; double类型
endif
if data_type eq 12 then begin
tv, bytscl(image) ; uint类型
endif
if (data_type ne 1) and (data_type ne 2) and (data_type ne 3) and (data_type ne 4) and (data_type ne 5) and (data_type ne 12) then begin
tv, image
endif
; 计算统计信息
image_mean = mean(image)
image_min = min(image)
image_max = max(image)
image_stddev = stddev(image)
image_variance = variance(image)
; 在图像上显示统计信息
text = '均值: ' + string(image_mean, format='(F8.2)') + $
' 最小值: ' + string(image_min) + $
' 最大值: ' + string(image_max) + $
' 标准差: ' + string(image_stddev, format='(F8.2)') + $
' 方差: ' + string(image_variance, format='(F8.2)')
; 创建文本背景
text_dims = textsize(text)
text_width = text_dims[0]
text_height = text_dims[1]
xpos = (cols - text_width)/2
ypos = rows - text_height - 10
; 绘制半透明背景
device, decomposed=0
polyfill, [xpos-5, xpos+text_width+5, xpos+text_width+5, xpos-5], $
[ypos-5, ypos-5, ypos+text_height+5, ypos+text_height+5], $
/device, color=0, fill_color=0, background=0.7, transparency=50
; 显示统计信息文本
xyouts, xpos, ypos, text, /normal, color=255, font='helvetica', size=1.2
; 显示文件名
xyouts, 10, 10, filename, /normal, color=255, font='helvetica', size=1.0
; 打印统计信息到控制台
print, '===== 图像统计信息 ====='
print, '文件: ', filename
print, '尺寸: ', cols, 'x', rows, ' 像素'
print, '数据类型: ', data_type
print, '最小值: ', image_min
print, '最大值: ', image_max
print, '均值: ', image_mean
print, '标准差: ', image_stddev
print, '方差: ', image_variance
print, '========================'
; 保存统计信息到文本文件
outfile = file_dirname(filename) + '/' + file_basename(filename, '.') + '_stats.txt'
openw, lun, outfile, /get_lun
printf, lun, '===== 图像统计信息 ====='
printf, lun, '文件: ' + filename
printf, lun, '尺寸: ' + string(cols) + 'x' + string(rows) + ' 像素'
printf, lun, '数据类型: ' + string(data_type)
printf, lun, '最小值: ' + string(image_min)
printf, lun, '最大值: ' + string(image_max)
printf, lun, '均值: ' + string(image_mean)
printf, lun, '标准差: ' + string(image_stddev)
printf, lun, '方差: ' + string(image_variance)
printf, lun, '========================'
free_lun, lun
print, '统计信息已保存到: ', outfile
; 等待用户关闭窗口
print, '按任意键关闭窗口...'
key = '' ; 初始化变量
wait, key ; 等待按键
wdelete, !d.window
end出现IDL> .compile -v 'D:\Harris\IDLwork\Default\binary.pro'
% Compiled module: BINARY.
IDL> binary, 'D:\five\p5.jpg'
% Variable is undefined: TEXTSIZE.
% Execution halted at: BINARY 91 D:\Harris\IDLwork\Default\binary.pro
% $MAIN$
最新发布