前言
大家直接看代码好了,好久前做的东西,逻辑理不顺了,但是代码能直接跑通
代码
提示:运行主函数之前,把辅助函数先建立好。
辅助函数
在matlab中点击新建函数,将下面代码粘贴进去,点击保存即可
函数1
function mask = fs_mask(alpha,mn)
% 构建掩膜算子
mask = zeros(3,3);
for i = 1:3
for j = 1:3
if i == 2 && j == 2
mask(i,j) = -8*alpha+mn;
else
mask(i,j) = (alpha^2 -alpha+2)/2;
end
end
end
end
函数2
function [Gx,Gy] = get_gradient_magnitude(I)
Gx = imfilter(double(I), fspecial('sobel')'); % 水平方向梯度
Gy = imfilter(double(I), fspecial('sobel'));
end
函数3
function std_value = get_std(I)
I_double = im2double(I);
std_value = std(I_double(:));
end
函数4
function H = getH(I)
[counts, ~] = imhist(I); % 获取图像的直方图
totalPixels = numel(I); % 获取图像总像素数
probabilities = counts / totalPixels; % 计算各像素值出现的概率
H = -sum(probabilities(probabilities > 0) .* log2(probabilities(probabilities > 0))); % 计算信息熵值
% disp(['图像的信息熵值为:', num2str(H)]);
end
主函数
主函数1
clear all;
I = imread('lena.tif');
[C,S] = wavedec2(I,1,'db4');
img_high = appcoef2(C,S,'db4',1);
img_low = detcoef2('h',C,S,1);
k1=0.85;
k2=1-k1;
alpha = 1.2;
img_high = normalize(img_high, 'range');
H_high = getH(img_high); % 信息熵
sigma_high = get_std(img_high);
f = k1*H_high + k2*sigma_high;
alpha_1 = exp(-f) + alpha;
% 低频
k3=0.45;
k4=1-k3;
beta = 4.32;
yinta = -1.86;
[Gx,Gy] = get_gradient_magnitude(img_low);
H_low = getH(img_low); % 信息熵
G = sqrt(Gx.^2 + Gy.^2); % 梯度模值
g = k3*abs(G) + k4*H_low;
g = (g - min(g(:))) / (max(g(:)) - min(g(:))); % 归一化
g_t= mean2(g);
alpha_2 = beta./(1 + exp(-g_t)) + yinta;
edge_high = conv2(img_high,fs_mask(1.2,0),'same');
edge_low = conv2(img_low,fs_mask(0.5,0),'same');
edge_img = edge_high + edge_low;
I1 = imadjust(edge_img,stretchlim(edge_img),[0,1]);
imshow(uint8(edge_img));
主函数1效果图
主函数2
在这里插入代码片clear all;
close all;
I = imread('lena.tif');
I1 = imadjust(I,stretchlim(I),[0,1]);
subplot(2,2,1); imshow(I1); title("原始图像");
edgesCanny = edge(I1, 'canny_old');
subplot(2,2,2); imshow(edgesCanny); title("Canny检测");
[N,M] = size(I);
h = [0.125,0.375,0.375,0.125];
g = [0.01,-0.01]; %高通滤波器
delta = [1,0,0];
J = 2; % 二级小波变换
a(1:N,1:M,1,1:J+1) = 0; %初始化变量
dx(1:N,1:M,1,1:J+1) = 0; %x方向偏导初始化
dy(1:N,1:M,1,1:J+1) = 0; %y方向偏导初始化
d(1:N,1:M,1,1:J+1) = 0; % 模值初始化
a(:,:,1,1) = conv2(h,h,I,'same'); %卷积运算
dx(:,:,1,1) = conv2(delta,g,I,'same'); %计算x方向偏导
dy(:,:,1,1) = conv2(g,delta,I,'same'); %计算y方向偏导
x = dx(:,:,1,1);
y = dy(:,:,1,1);
d(:,:,1,1) = sqrt(x.^2+y.^2); %计算模值
I1 = imadjust(d(:,:,1,1),stretchlim(d(:,:,1,1)),[0 1]); %增强对比度
subplot(2,2,3);imshow(I1); title("二进小波变换检测");
fusedImage = (I1 + edgesCanny) / 2;
subplot(2,2,4);imshow(fusedImage); title("融合检测");
主函数2效果图