MATLAB编程求梯度值

MATLAB编程求梯度值,第1张

x=[];

m=[];

for i=1:24

sx=['x',num2str(i)];

sm=['m',num2str(i)];

eval(['syms ',sx,' ',sm])

eval(['x=[x,',sx,'];']);

eval(['m=[m,',sm,'];']);

end

sh=sum(x^2)/21e6;

y=0;

for i=1:length(x)

y=y+sum(x(1:i));

sh=sh+(y-sum(x(1:i))/2)^2m(i);

end

jac=jacobian(sh,x);

jac'

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)

%GRADIENTDESCENT Performs gradient descent to learn theta

% theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by

% taking num_iters gradient steps with learning rate alpha

% Initialize some useful values

m = length(y); % number of training examples

J_history = zeros(num_iters, 1);

for iter = 1:num_iters,

% ====================== YOUR CODE HERE ======================

% Instructions: Perform a single gradient step on the parameter vector

% theta

%

% Hint: While debugging, it can be useful to print out the values

% of the cost function (computeCost) and gradient here

p=theta(1)-alpha(1/m)(sum((Xtheta-y)X(:,1)));

q=theta(2)-alpha(1/m)(sum((Xtheta-y)X(:,2)));

theta(1)=p;

theta(2)=q;

% ============================================================

% Save the cost J in every iteration

J_history(iter) = computeCost(X, y, theta);

end

end

clear;

%装入原图像1

load woman; %读入图像

I=X;

%小波函数

type = 'db1'; %选择小波类型

% 2维离散Daubechies小波变换

[CA1, CH1, CV1, CD1] = dwt2(I,type); %小波变换

C1 = [CH1 CV1 CD1];

%系数矩阵大小

[length1, width1] = size(CA1);

[M1, N1] = size(C1);

% 定义阈值T1

T1 =50; %水印参数

alpha = 02;

%在图像中加入水印

for counter2 = 1: 1: N1

for counter1 = 1: 1: M1

if( C1(counter1, counter2) > T1 ) %如果C1>阈值

marked1(counter1,counter2) = randn(1,1); %marked1为水印图像

NEWC1(counter1, counter2) = double( C1(counter1, counter2) ) +alpha abs( double( C1(counter1, counter2) ) ) marked1(counter1,counter2) ; %NEWC1为加入水印后的小波域

else

marked1(counter1, counter2) = 0;

NEWC1(counter1, counter2) = double( C1(counter1, counter2) );

end;

end;

end;

%重构图像

NEWCH1 = NEWC1(1:length1, 1:width1);

NEWCV1 = NEWC1(1:length1, width1+1:2width1);

NEWCD1 = NEWC1(1:length1, 2width1+1:3width1);

R1 = double( idwt2(CA1, NEWCH1, NEWCV1, NEWCD1, type) );

%分离水印

watermark1 = double(R1) - double(I);

figure(1);

subplot(1,2,1);

image(I); %显示原始图像

axis('square');

title('原始图像');

subplot(1,2,2);

imshow(R1/250); %显示小波变换后图像

axis('square');

title('Daubechies小波变换后图像');

figure(2);

imshow(watermark110^16); %显示水印图像

axis('square');

title('水印图像');

% 水印检测

newmarked1 = reshape(marked1, M1N1, 1);

% 检测阈值

T2 = 60;

for counter2 = 1: 1: N1

for counter1 = 1: 1: M1

if( NEWC1(counter1, counter2) >T2 ) %当NEWC1大于阈值时提取水印

NEWC1X(counter1, counter2) = NEWC1(counter1, counter2);

else

NEWC1X(counter1, counter2) = 0;

end;

end;

end;

NEWC1X = reshape(NEWC1X, M1N1, 1); %锐化

correlation1 = zeros(1000,1);

for corrcounter = 1: 1: 1000

if( corrcounter == 500)

correlation1(corrcounter,1) = NEWC1X'newmarked1 / (M1N1);

else

rnmark = randn(M1N1,1);

correlation1(corrcounter,1) = NEWC1X'rnmark / (M1N1);

end;

end;

% 计算阈值

originalthreshold = 0;

for counter2 = 1: 1: N1

for counter1 = 1: 1: M1

if( NEWC1(counter1, counter2) > T2 )

originalthreshold = originalthreshold + abs( NEWC1(counter1, counter2) );

end;

end;

end;

originalthreshold = originalthreshold alpha / (2M1N1);

corrcounter = 1000;

originalthresholdvector = ones(corrcounter,1) originalthreshold;

figure(3);

plot(correlation1, '-');

hold on;

plot(originalthresholdvector, '--'); %显示原始的加水印图像

title('原始的加水印图像');

xlabel('水印');

ylabel('检测响应');

梯度时,已经转成了double了,所以结果要转回unit8  程序修改后

clear all

close all

I = imread('1灰度图jpg');

hy = fspecial('sobel');

hx = hy';

Iy = imfilter(double(I), hy, 'replicate');

Ix = imfilter(double(I), hx, 'replicate');

gradmag = sqrt(Ix^2 + Iy^2);

figure('units', 'normalized', 'position', [0 0 1 1]);

subplot(1, 2, 1); imshow(I,[]), title('灰度增强图');

subplot(1, 2, 2); imshow(gradmag/255,[]), title('梯度幅值图像');

imwrite(gradmag,'2梯度幅值图像jpg')%保存图像为文件

editNN应该是你GUI界面上的一个输入值,通过你在GUI上选择网络模式得到一个字符串,然后就通过get(handleseditNN,'string');这句话获取editNN的文本信息即网络模式。按出错信息应该是你GUI上现在没有editNN这个值或者控件

频域增强:

global I

J=imnoise(I,'salt & pepper',002);

%gei tuxiang tianjia yanjiao zaosheng

subplot(121),imshow(J)

title('han you zaosheng de yuan tuxiang')

J=double(J);

f=fft2(J);

g=fftshift(f);

[M,N]=size(f);

n=3;d0=20;

n1=floor(M/2);n2=floor(N/2);

for i=1:M

for j=1:N

d=sqrt((i-n1)^2+(j-n2)^2);

h=1/(1+0414(d/d0)^(2n));

g(i,j)=hg(i,j);

end

end

g=ifftshift(g);

g=uint8(real(ifft2(g)));

subplot(122),imshow(g)

title('san jie Butterworth lvbo tuxiang')

以上就是关于MATLAB编程求梯度值全部的内容,包括:MATLAB编程求梯度值、用matlab实现梯度下降算法(gradient descent)。、帮我翻译一个matlab程序 简单的说明每句话的要做什么 为什么这么做 太简单的可以不翻译 高分等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10105937.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存