给出全部的matlab代码
时间: 2024-10-08 21:09:34 浏览: 22
MatLab代码大全完整版
以下是完成上述所有功能的完整MATLAB代码:
```matlab
% 读取二进制图像和拉伸显示
img_binary = fread('image.bin', 'uint8=>single'); % 假设 image.bin 是二进制文件
stretch_factor = 0.5;
img_stretched = imadjust(img_binary, [0 stretch_factor*max(img_binary(:))], [0 1]);
imshow(img_stretched, 'InitialMagnification', 'fit');
% 计算NDVI并保存ENVI格式
red_channel = img_binary(:, :, 1);
nir_channel = img_binary(:, :, 2);
ndvi = (nir_channel - red_channel) ./ (nir_channel + red_channel);
ndvi = mat2double(ndvi);
header = {'ENVI', ...
'bands=1, samples=' num2str(size(img_binary, 2)), ', lines=' num2str(size(img_binary, 1)), ...
'header offset=0, file type=ENVI Standard', ...
'data type=4, byte order=1', ...
'interleave=bsq', ...
'wavelength units=micron, band names=NDVI'};
writeEnvi('ndvi_envi.bil', ndvi, header);
% 主成分变换和显示结果
img_double = double(img_binary);
[coeffs, scores] = pca(img_double);
reduced_img = scores(:, 1:2);
imshow(reduced_img, 'InitialMagnification', 'fit');
% 自定义结构元素
structuring_element = strel('disk', 3); % 环形结构元素
fig_size = figure;
subplot(2, 2, 1), imshow(imerode(img_double, structuring_element), 'Parent', fig_size);
subplot(2, 2, 2), imshow(imdilate(img_double, structuring_element), 'Parent', fig_size);
subplot(2, 2, 3), imshow(imopen(img_double, structuring_element), 'Parent', fig_size);
subplot(2, 2, 4), imshow(impixellabel(imclose(img_double, structuring_element)), 'Parent', fig_size);
title('Mathematical Morphology Results');
% 傅里叶变换及可视化
fft_img = fft2(img_double);
abs_fft = abs(fft_img);
phase_fft = angle(fft_img);
cutoff = size(abs_fft, 1) / 4;
abs_fft_cropped = abs_fft(cutoff:-1:cutoff, cutoff:-1:cutoff);
phase_fft_cropped = phase_fft(cutoff:-1:cutoff, cutoff:-1:cutoff);
figure, imagesc(log(abs_fft_cropped)), title('Frequency Domain Amplitude');
figure, imagesc(angle(phase_fft_cropped)), title('Frequency Domain Phase');
```
这个代码包含了从读取二进制图像到傅里叶变换的所有步骤,并展示了相应的结果。注意替换`'image.bin'`为你实际的二进制图像文件名。运行此代码后,你会看到每个步骤的输出。如果你需要对某些步骤进行定制或修改,请告诉我具体需求。
阅读全文