• 2021-04-23 15:16:04

论文来源：

function [xm,fv]=GAEOQ1

%%初始目标函数与约束条件

%求解变量t, k, 目标函数 f,约束条件 g1

syms t k z;

f=100/t+25*(25*t+k*10*sqrt(1+1.25*t))+100*10*sqrt(1+1.25*t)*int((z-k)*normpdf(z,0,1),z,k,inf)/t;

tmin=1e-2;

tmax=4;

kmin=0;

kmax=2;

g1=1-200*sqrt(2+0.75*t)*int((z-k)*normpdf(z,0,1),z,k,inf)/(50*t);%%将约束标准化，将右端变为1

NP=50; %进化多少代

%%初始化种群，种群长度40

size=20;

E=zeros(20,5); %前两列为初始解，第三列为适应函数值，第四列记录是否为可行解，第五列记录违背约束条件的差值

E(:,1)=tmin+(tmax-tmin)*rand(size,1);

E(:,2)=kmin+(kmax-kmin)*rand(size,1);

fv=inf;%初始最优值为无穷大的值

D=zeros(NP,4);%用来记录每代的最优解,平均值，最差解,最优解是否为可行解

%%计算适应函数罚函数值，判断是否为可行解

for i=1:size

B=zeros(1,1);

B(1)=subs(g1,[t,k],E(i,(1:2)));

if B(1)>=0

E(i,4)=1;

E(i,3)=subs(f,[t,k],E(i,(1:2)));

else

E(i,4)=0;

E(i,3)=0;

end

if B(1)>=0

B(1)=0;

else

B(1)=abs(B(1));

end

E(i,5)=B(1);

end

fmax=max(E(:,3));

for i=1:size

if E(i,4)<1e-6

E(i,3)=fmax+E(i,5);

end

end

%%遗传进化   %%到这步适应值还没出错

for g=1:NP    %%原来错误在这里，这个k跟前面的k重复了

%%竞标赛选择  %%小生态技术

M=zeros(size,2);%用来存储优胜者的中间矩阵

for i=1:size

%A=randperm(size,6);

%dij1=sqrt(0.5*(E(A(1),1)-E(A(2),1))^2);  %%小生态技术,只有单界时小生态技术没法用

%dij2=sqrt(0.5*(E(A(1),1)-E(A(3),1))^2);

%dij3=sqrt(0.5*(E(A(1),1)-E(A(4),1))^2);

%dij4=sqrt(0.5*(E(A(1),1)-E(A(5),1))^2);

%dij5=sqrt(0.5*(E(A(1),1)-E(A(6),1))^2);

%if dij1<0.1

%if E(A(1),3)<=E(A(2),3)

%M(i,:)=E(A(1),(1:2));

%else

%M(i,:)=E(A(2),(1:2));

%end

%continue;

%elseif dij2<0.1

%if E(A(1),3)<=E(A(3),3)

%M(i,:)=E(A(1),(1:2));

%else

%M(i,:)=E(A(3),(1:2));

%end

%continue;

%elseif dij3<0.1

%if E(A(1),3)<=E(A(4),3)

%M(i,:)=E(A(1),(1:2));

%else

%M(i,:)=E(A(4),(1:2));

%end

%continue;

%elseif dij4<0.1

%if E(A(1),3)<=E(A(5),3)

%M(i,:)=E(A(1),(1:2));

%else

%M(i,:)=E(A(5),(1:2));

%end

%continue;

%elseif dij5<0.1

%if E(A(1),3)<=E(A(6),3)

%M(i,:)=E(A(1),(1:2));

%else

%M(i,:)=E(A(6),(1:2));

%end

%else

%M(i,:)=E(A(1),(1:2));

%end

%end

A=randperm(size,2);

if E(A(1),3)<=E(A(2),3)

M(i,:)=E(A(1),(1:2));

else

M(i,:)=E(A(2),(1:2));

end

end

%%模拟二进制交叉生成后代

for j=1:size/2

if rand()>=0.5

A=randperm(size,2);

c=rand();

x2=max(M(A(1),1),M(A(2),1));

x1=min(M(A(1),1),M(A(2),1));

beita1_t=1+2*(x1-tmin)/(x2-x1);

rfa_t=2-beita1_t^(-2);

if c<=1/rfa_t

beita2_t=sqrt(rfa_t*c);

else

beita2_t=sqrt(1/(2-rfa_t*c));

end

E(2*j-1,1)=0.5*(x1+x2-beita2_t*(x2-x1));

E(2*j,1)=0.5*(x1+x2+beita2_t*(x2-x1));

end

%%只在可行解时出错是怎么回事？是不是变异的原因，已纠正

if rand()>0.5

c=rand();

x2=max(M(A(1),2),M(A(2),2));

x1=min(M(A(1),2),M(A(2),2));

beita1_t=1+2*(x1-kmin)/(x2-x1);

rfa_t=2-beita1_t^(-2);

if c<=1/rfa_t

beita2_t=sqrt(rfa_t*c);

else

beita2_t=sqrt(1/(2-rfa_t*c));

end

E(2*j-1,2)=0.5*(x1+x2-beita2_t*(x2-x1));

E(2*j,2)=0.5*(x1+x2+beita2_t*(x2-x1));

end

end

%%变异，变异会不会导致可行解不可行?单下界时不用变异,设置判断条件防止过界

for i=1:size

nita=100+g;

pm=1/size+g*(1-1/size)/NP;

if rand()

u=rand();

%x=E(i,1);

x=E(i,1);%%

deltamax=1;

if u<=0.5

delta_2=(2*u)^(1/(nita+1))-1;

else

delta_2=1-(2*(1-u))^(1/(nita+1));

end

if x+delta_2*deltamax>=tmin

E(i,1)=x+delta_2*deltamax;

end

end

if rand()

u=rand();

x=E(i,2);

deltamax=1;

if u<=0.5

delta_1=(2*u)^(1/(nita+1))-1;

else

delta_1=1-(2*(1-u))^(1/(nita+1));

end

if x+delta_1*deltamax>=kmin

E(i,2)=x+delta_1*deltamax;

end

end

end

%%计算子代罚函数值，判断是否满足可行解

for i=1:size

B(1)=subs(g1,[t,k],E(i,(1:2)));

if B(1)>=0

E(i,4)=1;

E(i,3)=subs(f,[t,k],E(i,(1:2)));%%跟直接算的结果不一样，也跟EOQ得到的结果不一样 eval出错的原因

else

E(i,4)=0;

E(i,3)=0;

end

if B(1)>=0

B(1)=0;

else

B(1)=abs(B(1));

end

E(i,5)=B(1);

end

for i=1:size

if E(i,4)<1e-6

E(i,3)=fmax+E(i,5);

end

end

[Q,IX]=sort(E,1);

%Q=vpa(Q,4);%%

D(g,1)=Q(1,3);

D(g,2)=mean(Q(:,3));

D(g,3)=Q(size,3);

D(g,4)=E(IX(1,3),4);

if Q(1,3)

fv=Q(1,3);

xm=E(IX(1,3),(1:2));

xm=[xm,E(IX(1,3),4)];

end

end

%画图

k=1;

for i=1:NP

if D(i,4)==1

N(k,:)=D(i,:);

k=k+1;

end

end

plot(N(:,1),'r*');

hold on

plot(N(:,2),'b+');

hold on

plot(N(:,3),'ms');

legend('最优值','平均值','最差值');

hold off

end

侵删

更多相关内容
• 汽车半主动悬架作动器故障诊断与容错控制方法研究。我们复现了所上传的大论文内容！
• 论文复现代码,论文复现代码 难吗,matlab源码.zip
• 题主根据论文中的公式，复现了这篇论文的代码，从效果来看，该算法的噪声平滑能力较为有限，但 V PC {{\text{V}}_{\text{PC}}} VPC​和 V PE {{\text{V}}_{\text{PE}}} VPE​的值却比原始论文的高。 算法实现步骤 ...

## 参考文献

[1] Chuang K S, Tzeng H L, Chen S, et al. Fuzzy c-means clustering with spatial information for image segmentation[J]. computerized medical imaging and graphics, 2006, 30(1): 9-15.

## 概述

模糊c均值聚类（FCM）算法用于图像分割时，由于缺少图像的空间信息，因此对噪声的鲁棒性较差，为此国内外学者提出了许多FCM变体算法。其中主流的方法是通过构造新的目标函数将空间信息添加到原始FCM中，从目前来看，构造目标函数的方法大致分为两类：一是通过滤波算法预处理，接着在处理后的图像上利用数学知识构造新的目标函数，达到提高鲁棒性的目的；二是利用局部信息或正则化运算改变隶属度矩阵，进而达到提高鲁棒性的目的。
本文主要关注的第二种方法，即通过局部运算，改变每次迭代的隶属度值，从而将图像的空间信息引入FCM目标函数之中，提高了算法的鲁棒性。题主根据论文中的公式，复现了这篇论文的代码，从效果来看，该算法的噪声平滑能力较为有限，但 V PC {{\text{V}}_{\text{PC}}} V PE {{\text{V}}_{\text{PE}}} 的值却比原始论文的高。

## 算法实现步骤

1. 参数设置：包括聚类数目 c l u s t e r _ n u m cluster\_{num} 、模糊因子 m m 、最大迭代次数 m a x _ i t e r max\_{iter} 、窗口尺寸 k k
2. 图像灰度转换及其归一化处理；
3. 添加噪声（可无）；
4. 初始化隶属度矩阵 u i j {{u}_{ij}}
5. 更新聚类中心 c i c_{i} ，更新公式为 c i = ∑ j = 1 N u i j m x j ∑ j = 1 N u i j m {{c}_{i}}=\frac{\sum\limits_{j=1}^{N}{u_{ij}^{m}{{x}_{j}}}}{\sum\limits_{j=1}^{N}{u_{ij}^{m}}}
6. 更新隶属度矩阵 u i j u_{ij} ，更新公式为 u i j = 1 ∑ k = 1 c ( ∥ x j − c i ∥ ∥ x j − c k ∥ ) 2 / ( m − 1 )    {{u}_{ij}}=\frac{1}{\sum\limits_{k=1}^{c}{{{\left( \frac{\left\| {{x}_{j}}-{{c}_{i}} \right\|}{\left\| {{x}_{j}}-{{c}_{k}} \right\|} \right)}^{{2}/{\left( m-1 \right)}\;}}}}
7. 计算空间函数 h i j h_{ij} ，计算公式为 h i j = ∑ k ∈ N ( x j ) u i k {{h}_{ij}}=\sum\limits_{k\in N\left( {{x}_{j}} \right)}{{{u}_{ik}}}
8. 计算新的隶属度矩阵 u ′ i j {{{u}'}_{ij}} ，计算公式为 u ′ i j = u i j p h i j q ∑ k = 1 c u k j p h k j q {{{u}'}_{ij}}=\frac{u_{ij}^{p}h_{ij}^{q}}{\sum\limits_{k=1}^{c}{u_{kj}^{p}h_{kj}^{q}}}
9. 计算目标函数 J J ，计算公式为 J = ∑ j = 1 N ∑ i = 1 c u i j m ∥ x j − c i ∥ 2 J=\sum\limits_{j=1}^{N}{\sum\limits_{i=1}^{c}{u_{ij}^{m}{{\left\| {{x}_{j}}-{{c}_{i}} \right\|}^{2}}}}
10. 判断本次迭代与前一次迭代的目标函数的差值，若小于最小误差则退出迭代，若大于误差则返回步骤5；
11. 将像素分配给隶属度最大的标签中

## 楼主原创代码

%% sFCM 论文代码复现
% Title:Fuzzy c-means clustering with spatial information for image segmentation
% Authors:Keh-Shih Chuang Hong-Long Tzeng Sharon Chen Jay Wu Tzong-Jer Chen
% Computerized Medical Imaging and Graphics
% https://doi.org/10.1016/j.compmedimag.2005.10.001
%% 初始化
clc
clear all
close all
%% 参数设置
error=0.001; % 最小误差
cluster_num=3; % 聚类数
max_iter=100; % 最大迭代次数
m=2; % 隶属度指数
k=5; % 窗口大小
%% 读取图像
[row,col,depth]=size(f_uint8);
N=row*col;
%% 彩色图像转换为灰度图像，并归一化到0到1，便于使用imnoise函数加图像噪声
if depth>1
f=rgb2gray(f_uint8);
f=double(f)/255;
else
f=double(f_uint8)/255;
end
%% 加噪声
f = imnoise(f,'gaussian',0,0.01);
figure, imshow(f),title('噪声图像');
f=f*255;
%% 重组原始图像 便于后续处理
f_reorganize=repmat(f,[1 1 cluster_num]);
%% 初始化隶属度矩阵
U=rand(row,col,cluster_num);
U=U./repmat(sum(U,3),[1 1 cluster_num]);
%% 开始聚类
for iter=1:max_iter
U_m=U.^m;
% 公式（3），更新聚类中心
center(:,iter)=sum(sum(f_reorganize.*U_m),2)./(sum(sum(U_m),2));
% 公式（2）分母部分
d=(f_reorganize-repmat(permute(center(:,iter),[3 2 1]),[row col 1])).^2;
% 公式（2）整体，更新旧的隶属度矩阵
molecular=d.^(1/(m-1));
U=(molecular.*repmat(sum(1./molecular,3),[1 1 cluster_num])).^(-1);
U_m=U.^m;
% 为空间函数 h 分配内存
h=zeros(row,col,cluster_num);
% 扩展隶属度矩阵，便于后续的局部信息操作
% 公式（3），计算局部隶属度值
for i=-k:k
for j=-k:k
if i==0 && j==0
continue;
end
end
end
% 公式（4），新的隶属度矩阵
U=(U_m.*h)./repmat(sum(U_m.*h,3),[1 1 cluster_num]);
U_m=U.^m;
% 公式（1），更新目标函数
J(iter)=sum(sum(sum(U_m.*d)));
fprintf('第%d次聚类，J=%f\n',iter,J(iter));
%判断迭代停止条件
if iter>1 && abs(J(iter)-J(iter-1))<error
fprintf('目标函数已最小化\n');
break;
end
if iter > 1 && iter == max_iter && abs(J(iter) - J(iter - 1)) > error
fprintf('目标函数未最小化，提前到达了迭代上限\n');
break;
end
end
%% 分割结果显示
[~,cluster_indice]=max(U,[],3);
FCM_result=Label_image(f_uint8,cluster_indice);
figure,imshow(FCM_result),title('分割结果');
J=gather(J);
figure,plot(1:iter,J(1:iter)),title('目标函数曲线');
%%
Vpc = sum(sum(sum(U .^ 2))) / N * 100;
Vpe = -sum(sum(sum(U .* log(U)))) / N * 100;
fprintf('Fuzzy partition coefficient Vpc = %.2f%%\n', Vpc);
fprintf('Fuzzy partition entropy Vpe = %.2f%%\n', Vpe);


运行结果：
原始图像：
分割结果：（噪声类型为高斯噪声）

目标函数曲线：

V PC {{\text{V}}_{\text{PC}}} V PE {{\text{V}}_{\text{PE}}} 的值：

展开全文
• 基于PSO-BP神经网络的风电功率预测研究论文复现。粒子群优化算法，BP神经网络。论文出图及复现效果见下： https://blog.csdn.net/weixin_44644611/article/details/124643919
• 根据A Local Contrast Method for Small Infrared Target Detection论文中的代码复现 改个路径就可用
• 基于Matlab代码，完美复现了两阶段鲁棒优化文献《微电网两阶段鲁棒优化经济调度方法_刘一欣》。跟目前流传的版本不同，本人硕士方向为微网两阶段鲁棒优化调度，纯原创！内容构建了微网两阶段鲁棒调度模型，建立了min...
• 博客代码
• 参考文献 [1] Chatzis S P , Varvarigou T A . A Fuzzy Clustering Approach Toward Hidden Markov Random Field Models for Enhanced Spatially Constrained ...有关论文的解读这里不再赘述，有一篇帖子写的非常详细，

## 参考文献

[1] Chatzis S P , Varvarigou T A . A Fuzzy Clustering Approach Toward Hidden Markov Random Field Models for Enhanced Spatially Constrained Image Segmentation[J]. IEEE Transactions on Fuzzy Systems, 2008, 16(5):1351-1361.

## 概述

有关论文的解读这里不再赘述，有一篇帖子写的非常详细，生动易懂，这里贴上链接：

https://blog.csdn.net/u011861755/article/details/103897063

在这里指出该帖子的一个错误，可能是楼主在敲公式时不小心敲错的: d i j = w 2 ln ⁡ ( 2 π ) + 1 2 ln ⁡ ( ∣ σ i ∣ ) + ( y j − μ i ) 2 2 σ i {{d}_{ij}}\text{=}\frac{w}{2}\ln (2\pi )+\frac{1}{2}\ln (\left| {{\sigma }_{i}} \right|)+\frac{{{\left( {{y}_{j}}-{{\mu }_{i}} \right)}^{2}}}{2{{\sigma }_{i}}}

## 题主原创代码

题主根据网上代码片段，复现了这篇经典的论文，如有更简洁的实现方法，欢迎随时讨论。

%% HMRF-FCM
% Title:A Fuzzy Clustering Approach Toward Hidden Markov Random Field Models for Enhanced Spatially Constrained Image Segmentation
% Authors: Sotirios P. Chatzis and Theodora A. Varvarigou
% IEEE Transactions on Fuzzy Systems
% 10.1109/TFUZZ.2008.2005008
%% 初始化
clc
clear all;
close all;
%% 输入图像
%% 参数计算
[row,col,depth]=size(f_uint8);
n=row*col;
%% 将彩色图像转换为灰度图像
if depth>1
f=rgb2gray(f_uint8);
f=double(f)/255;
else
f=double(f_uint8)/255;
end
%% 参数设置
density = 0.1;  %噪声密度
error = 0.1;    %误差上限
cluster_num =2; %聚类数
max_iter =100;  %最大迭代次数
lambda=50;  %模糊隶属度值的模糊程度
%% 添加噪声
f = imnoise(f,'gaussian',0,density);
f = imnoise(f,'salt & pepper',density);
f = imnoise(f,'speckle',density);
figure,imshow(f),title('噪声图像');
f=f*255;
%% 初始化隶属度矩阵
U=rand(row,col,cluster_num);
U_col_sum = sum(U, 3);
U = U ./ repmat(U_col_sum, [1 1 cluster_num]);
J=zeros(1,max_iter);
%% 得到初始类标签
label = kmeans(f(:),cluster_num);
label = reshape(label,size(f));
%% 开始聚类
for iter=1:max_iter
%% 计算pi
label_u = imfilter(label,[0,1,0;0,0,0;0,0,0],'replicate');
label_d = imfilter(label,[0,0,0;0,0,0;0,1,0],'replicate');
label_l = imfilter(label,[0,0,0;1,0,0;0,0,0],'replicate');
label_r = imfilter(label,[0,0,0;0,0,1;0,0,0],'replicate');
label_ul = imfilter(label,[1,0,0;0,0,0;0,0,0],'replicate');
label_ur = imfilter(label,[0,0,1;0,0,0;0,0,0],'replicate');
label_dl = imfilter(label,[0,0,0;0,0,0;1,0,0],'replicate');
label_dr = imfilter(label,[0,0,0;0,0,0;0,0,1],'replicate');
p_i = zeros(cluster_num,n);
for i = 1:cluster_num
label_i = i * ones(size(label));
temp = ~(label_i - label_u) + ~(label_i - label_d) + ...
~(label_i - label_l) + ~(label_i - label_r) + ...
~(label_i - label_ul) + ~(label_i - label_ur) + ...
~(label_i - label_dl) +~(label_i - label_dr);
p_i(i,:) = temp(:)/8;%计算概率
end
p_i((p_i == 0)) = 0.001;%防止出现0
%% 计算均值和方差和距离
mu = zeros(1,cluster_num);
sigma = zeros(1,cluster_num);
dist=zeros(row,col,cluster_num);
%求出每一类的的均值方差
for i = 1:cluster_num
index = find(label == i);%找到每一类的点
data_c = f(index);
mu(i) = mean(data_c);   %均值
sigma(i) = var(data_c); %方差
dist(:,:,i)=(3*log(2*pi))/2+(log(sqrt((sigma(i)).^2)))/2+((f-mu(i)).^2)/(2*sigma(i));   %距离
end
p_i=reshape(p_i',row,col,cluster_num);
% 更新隶属度矩阵
U=(p_i.*exp(-dist/lambda))./sum((p_i.*exp(-dist/lambda)),3);
% 重新更新标签
[~,cluster_indice]=max(U,[],3);
label=cluster_indice;
% 计算目标函数
J(iter)=sum(sum(sum(U.*dist)))+lambda*sum(sum(sum(U.*log(U./p_i))));
fprintf('第%d次聚类，J=%f\n',iter,J(iter));
%判断迭代停止条件
if iter>1 && abs(J(iter)-J(iter-1))<error
fprintf('目标函数已最小化\n');
break;
end
if iter > 1 && iter == max_iter && abs(J(iter) - J(iter - 1)) > error
fprintf('目标函数未最小化，提前到达了迭代上限\n');
break;
end
end
%% 分割结果显示
[~,cluster_indice]=max(U,[],3);
result=Label_image(f_uint8,cluster_indice);
figure,imshow(result),title('聚类结果');
J=gather(J);
figure,plot(1:iter,J(1:iter)),title('目标函数曲线');


在函数中用到了一个聚类区域着色函数，Label_image.m，将此函数也贴于此，此函数位于Matlab脚本包中，网址为

https://ww2.mathworks.cn/matlabcentral/fileexchange/75245-afcf?s_tid=srchtitle>

Label_image.m

function [fs,center_p,Num_p,center_lab]=Label_image(f,L)
f=double(f);
num_area=max(L,[],'all');
Num_p=zeros(num_area,1);
if size(f,3)<2
[M,N]=size(f);
s3=L;
fs=zeros(M,N);
center_p=zeros(num_area,1);
for i=1:num_area
f2=f(s3==i);f_med=median(f2);fx=double((s3==i))*double(f_med);
fs=fs+fx;
center_p(i,:)=uint8(f_med);
Num_p=zeros(num_area,1);
end
fs=uint8(fs);
%% Color image
else
[M,N]=size(f(:,:,1));
s3=L;
fs=zeros(M,N,3);
fr=f(:,:,1);fg=f(:,:,2);fb=f(:,:,3);
center_p=zeros(num_area,3);
for i=1:num_area
fr2=fr(s3==i);r_med=median(fr2);r=(s3==i)*r_med;
fg2=fg(s3==i);g_med=median(fg2);g=(s3==i)*g_med;
fb2=fb(s3==i);b_med=median(fb2);b=(s3==i)*b_med;
fs=fs+cat(3,r,g,b);
center_p(i,:)=uint8([r_med g_med b_med]);
Num_p(i)=sum(sum(s3==i));
end
fs=uint8(fs);
TT=cat(3,center_p(:,1),center_p(:,2),center_p(:,3));
TT2=colorspace('Lab<-RGB',TT);
TT2r=TT2(:,:,1);TT2g=TT2(:,:,2);TT2b=TT2(:,:,3);
center_lab(:,1)=TT2r(:);center_lab(:,2)=TT2g(:);center_lab(:,3)=TT2b(:);
end
end


colorspace.m

function varargout = colorspace(Conversion,varargin)
%COLORSPACE  Convert a color image between color representations.
%   B = COLORSPACE(S,A) converts the color representation of image A
%   where S is a string specifying the conversion.  S tells the
%   source and destination color spaces, S = 'dest<-src', or
%   alternatively, S = 'src->dest'.  Supported color spaces are
%
%     'RGB'              R'G'B' Red Green Blue (ITU-R BT.709 gamma-corrected)
%     'YPbPr'            Luma (ITU-R BT.601) + Chroma
%     'YCbCr'/'YCC'      Luma + Chroma ("digitized" version of Y'PbPr)
%     'YUV'              NTSC PAL Y'UV Luma + Chroma
%     'YIQ'              NTSC Y'IQ Luma + Chroma
%     'YDbDr'            SECAM Y'DbDr Luma + Chroma
%     'JPEGYCbCr'        JPEG-Y'CbCr Luma + Chroma
%     'HSV'/'HSB'        Hue Saturation Value/Brightness
%     'HSL'/'HLS'/'HSI'  Hue Saturation Luminance/Intensity
%     'XYZ'              CIE XYZ
%     'Lab'              CIE L*a*b* (CIELAB)
%     'Luv'              CIE L*u*v* (CIELUV)
%     'Lch'              CIE L*ch (CIELCH)
%
%  All conversions assume 2 degree observer and D65 illuminant.  Color
%  space names are case insensitive.  When R'G'B' is the source or
%  destination, it can be omitted. For example 'yuv<-' is short for
%  'yuv<-rgb'.
%
%  MATLAB uses two standard data formats for R'G'B': double data with
%  intensities in the range 0 to 1, and uint8 data with integer-valued
%  intensities from 0 to 255.  As MATLAB's native datatype, double data is
%  the natural choice, and the R'G'B' format used by colorspace.  However,
%  for memory and computational performance, some functions also operate
%  with uint8 R'G'B'.  Given uint8 R'G'B' color data, colorspace will
%  first cast it to double R'G'B' before processing.
%
%  If A is an Mx3 array, like a colormap, B will also have size Mx3.
%
%  [B1,B2,B3] = COLORSPACE(S,A) specifies separate output channels.
%  COLORSPACE(S,A1,A2,A3) specifies separate input channels.
% Pascal Getreuer 2005-2006
%%% Input parsing %%%
if nargin < 2, error('Not enough input arguments.'); end
[SrcSpace,DestSpace] = parse(Conversion);
if nargin == 2
Image = varargin{1};
elseif nargin >= 3
Image = cat(3,varargin{:});
else
error('Invalid number of input arguments.');
end
FlipDims = (size(Image,3) == 1);
if FlipDims, Image = permute(Image,[1,3,2]); end
if ~isa(Image,'double'), Image = double(Image)/255; end
if size(Image,3) ~= 3, error('Invalid input size.'); end
SrcT = gettransform(SrcSpace);
DestT = gettransform(DestSpace);
if ~ischar(SrcT) & ~ischar(DestT)
% Both source and destination transforms are affine, so they
% can be composed into one affine operation
T = [DestT(:,1:3)*SrcT(:,1:3),DestT(:,1:3)*SrcT(:,4)+DestT(:,4)];
Temp = zeros(size(Image));
Temp(:,:,1) = T(1)*Image(:,:,1) + T(4)*Image(:,:,2) + T(7)*Image(:,:,3) + T(10);
Temp(:,:,2) = T(2)*Image(:,:,1) + T(5)*Image(:,:,2) + T(8)*Image(:,:,3) + T(11);
Temp(:,:,3) = T(3)*Image(:,:,1) + T(6)*Image(:,:,2) + T(9)*Image(:,:,3) + T(12);
Image = Temp;
elseif ~ischar(DestT)
Image = rgb(Image,SrcSpace);
Temp = zeros(size(Image));
Temp(:,:,1) = DestT(1)*Image(:,:,1) + DestT(4)*Image(:,:,2) + DestT(7)*Image(:,:,3) + DestT(10);
Temp(:,:,2) = DestT(2)*Image(:,:,1) + DestT(5)*Image(:,:,2) + DestT(8)*Image(:,:,3) + DestT(11);
Temp(:,:,3) = DestT(3)*Image(:,:,1) + DestT(6)*Image(:,:,2) + DestT(9)*Image(:,:,3) + DestT(12);
Image = Temp;
else
Image = feval(DestT,Image,SrcSpace);
end
%%% Output format %%%
if nargout > 1
varargout = {Image(:,:,1),Image(:,:,2),Image(:,:,3)};
else
if FlipDims, Image = permute(Image,[1,3,2]); end
varargout = {Image};
end
return;

function [SrcSpace,DestSpace] = parse(Str)
% Parse conversion argument
if isstr(Str)
Str = lower(strrep(strrep(Str,'-',''),' ',''));
k = find(Str == '>');

if length(k) == 1         % Interpret the form 'src->dest'
SrcSpace = Str(1:k-1);
DestSpace = Str(k+1:end);
else
k = find(Str == '<');

if length(k) == 1      % Interpret the form 'dest<-src'
DestSpace = Str(1:k-1);
SrcSpace = Str(k+1:end);
else
error(['Invalid conversion, ''',Str,'''.']);
end
end

SrcSpace = alias(SrcSpace);
DestSpace = alias(DestSpace);
else
SrcSpace = 1;             % No source pre-transform
DestSpace = Conversion;
if any(size(Conversion) ~= 3), error('Transformation matrix must be 3x3.'); end
end
return;

function Space = alias(Space)
Space = strrep(Space,'cie','');
if isempty(Space)
Space = 'rgb';
end
switch Space
case {'ycbcr','ycc'}
Space = 'ycbcr';
case {'hsv','hsb'}
Space = 'hsv';
case {'hsl','hsi','hls'}
Space = 'hsl';
case {'rgb','yuv','yiq','ydbdr','ycbcr','jpegycbcr','xyz','lab','luv','lch'}
return;
end
return;

function T = gettransform(Space)
% Get a colorspace transform: either a matrix describing an affine transform,
% or a string referring to a conversion subroutine
switch Space
case 'ypbpr'
T = [0.299,0.587,0.114,0;-0.1687367,-0.331264,0.5,0;0.5,-0.418688,-0.081312,0];
case 'yuv'
% R'G'B' to NTSC/PAL YUV
T = [0.299,0.587,0.114,0;-0.147,-0.289,0.436,0;0.615,-0.515,-0.100,0];
case 'ydbdr'
% R'G'B' to SECAM YDbDr
T = [0.299,0.587,0.114,0;-0.450,-0.883,1.333,0;-1.333,1.116,0.217,0];
case 'yiq'
% R'G'B' in [0,1] to NTSC YIQ in [0,1];[-0.595716,0.595716];[-0.522591,0.522591];
T = [0.299,0.587,0.114,0;0.595716,-0.274453,-0.321263,0;0.211456,-0.522591,0.311135,0];
case 'ycbcr'
% R'G'B' (range [0,1]) to ITU-R BRT.601 (CCIR 601) Y'CbCr
% Poynton, Equation 3, scaling of R'G'B to Y'PbPr conversion
T = [65.481,128.553,24.966,16;-37.797,-74.203,112.0,128;112.0,-93.786,-18.214,128];
case 'jpegycbcr'
T = [0.299,0.587,0.114,0;-0.168736,-0.331264,0.5,0.5;0.5,-0.418688,-0.081312,0.5]*255;
case {'rgb','xyz','hsv','hsl','lab','luv','lch'}
T = Space;
otherwise
error(['Unknown color space, ''',Space,'''.']);
end
return;

function Image = rgb(Image,SrcSpace)
% Convert to Rec. 709 R'G'B' from 'SrcSpace'
switch SrcSpace
case 'rgb'
return;
case 'hsv'
% Convert HSV to R'G'B'
Image = huetorgb((1 - Image(:,:,2)).*Image(:,:,3),Image(:,:,3),Image(:,:,1));
case 'hsl'
% Convert HSL to R'G'B'
L = Image(:,:,3);
Delta = Image(:,:,2).*min(L,1-L);
Image = huetorgb(L-Delta,L+Delta,Image(:,:,1));
case {'xyz','lab','luv','lch'}
% Convert to CIE XYZ
Image = xyz(Image,SrcSpace);
% Convert XYZ to RGB
T = [3.240479,-1.53715,-0.498535;-0.969256,1.875992,0.041556;0.055648,-0.204043,1.057311];
R = T(1)*Image(:,:,1) + T(4)*Image(:,:,2) + T(7)*Image(:,:,3);  % R
G = T(2)*Image(:,:,1) + T(5)*Image(:,:,2) + T(8)*Image(:,:,3);  % G
B = T(3)*Image(:,:,1) + T(6)*Image(:,:,2) + T(9)*Image(:,:,3);  % B
% Desaturate and rescale to constrain resulting RGB values to [0,1]
% Apply gamma correction to convert RGB to Rec. 709 R'G'B'
Image(:,:,1) = gammacorrection(R);  % R'
Image(:,:,2) = gammacorrection(G);  % G'
Image(:,:,3) = gammacorrection(B);  % B'
otherwise  % Conversion is through an affine transform
T = gettransform(SrcSpace);
temp = inv(T(:,1:3));
T = [temp,-temp*T(:,4)];
R = T(1)*Image(:,:,1) + T(4)*Image(:,:,2) + T(7)*Image(:,:,3) + T(10);
G = T(2)*Image(:,:,1) + T(5)*Image(:,:,2) + T(8)*Image(:,:,3) + T(11);
B = T(3)*Image(:,:,1) + T(6)*Image(:,:,2) + T(9)*Image(:,:,3) + T(12);
Image(:,:,1) = R;
Image(:,:,2) = G;
Image(:,:,3) = B;
end
% Clip to [0,1]
Image = min(max(Image,0),1);
return;

function Image = xyz(Image,SrcSpace)
% Convert to CIE XYZ from 'SrcSpace'
WhitePoint = [0.950456,1,1.088754];
switch SrcSpace
case 'xyz'
return;
case 'luv'
% Convert CIE L*uv to XYZ
WhitePointU = (4*WhitePoint(1))./(WhitePoint(1) + 15*WhitePoint(2) + 3*WhitePoint(3));
WhitePointV = (9*WhitePoint(2))./(WhitePoint(1) + 15*WhitePoint(2) + 3*WhitePoint(3));
L = Image(:,:,1);
Y = (L + 16)/116;
Y = invf(Y)*WhitePoint(2);
U = Image(:,:,2)./(13*L + 1e-6*(L==0)) + WhitePointU;
V = Image(:,:,3)./(13*L + 1e-6*(L==0)) + WhitePointV;
Image(:,:,1) = -(9*Y.*U)./((U-4).*V - U.*V);                  % X
Image(:,:,2) = Y;                                             % Y
Image(:,:,3) = (9*Y - (15*V.*Y) - (V.*Image(:,:,1)))./(3*V);  % Z
case {'lab','lch'}
Image = lab(Image,SrcSpace);
% Convert CIE L*ab to XYZ
fY = (Image(:,:,1) + 16)/116;
fX = fY + Image(:,:,2)/500;
fZ = fY - Image(:,:,3)/200;
Image(:,:,1) = WhitePoint(1)*invf(fX);  % X
Image(:,:,2) = WhitePoint(2)*invf(fY);  % Y
Image(:,:,3) = WhitePoint(3)*invf(fZ);  % Z
otherwise   % Convert from some gamma-corrected space
% Convert to Rec. 701 R'G'B'
Image = rgb(Image,SrcSpace);
% Undo gamma correction
R = invgammacorrection(Image(:,:,1));
G = invgammacorrection(Image(:,:,2));
B = invgammacorrection(Image(:,:,3));
% Convert RGB to XYZ
T = inv([3.240479,-1.53715,-0.498535;-0.969256,1.875992,0.041556;0.055648,-0.204043,1.057311]);
Image(:,:,1) = T(1)*R + T(4)*G + T(7)*B;  % X
Image(:,:,2) = T(2)*R + T(5)*G + T(8)*B;  % Y
Image(:,:,3) = T(3)*R + T(6)*G + T(9)*B;  % Z
end
return;

function Image = hsv(Image,SrcSpace)
% Convert to HSV
Image = rgb(Image,SrcSpace);
V = max(Image,[],3);
S = (V - min(Image,[],3))./(V + (V == 0));
Image(:,:,1) = rgbtohue(Image);
Image(:,:,2) = S;
Image(:,:,3) = V;
return;

function Image = hsl(Image,SrcSpace)
% Convert to HSL
switch SrcSpace
case 'hsv'
% Convert HSV to HSL
MaxVal = Image(:,:,3);
MinVal = (1 - Image(:,:,2)).*MaxVal;
L = 0.5*(MaxVal + MinVal);
temp = min(L,1-L);
Image(:,:,2) = 0.5*(MaxVal - MinVal)./(temp + (temp == 0));
Image(:,:,3) = L;
otherwise
Image = rgb(Image,SrcSpace);  % Convert to Rec. 701 R'G'B'
% Convert R'G'B' to HSL
MinVal = min(Image,[],3);
MaxVal = max(Image,[],3);
L = 0.5*(MaxVal + MinVal);
temp = min(L,1-L);
S = 0.5*(MaxVal - MinVal)./(temp + (temp == 0));
Image(:,:,1) = rgbtohue(Image);
Image(:,:,2) = S;
Image(:,:,3) = L;
end
return;

function Image = lab(Image,SrcSpace)
% Convert to CIE L*a*b* (CIELAB)
WhitePoint = [0.950456,1,1.088754];
switch SrcSpace
case 'lab'
return;
case 'lch'
% Convert CIE L*CH to CIE L*ab
C = Image(:,:,2);
Image(:,:,2) = cos(Image(:,:,3)*pi/180).*C;  % a*
Image(:,:,3) = sin(Image(:,:,3)*pi/180).*C;  % b*
otherwise
Image = xyz(Image,SrcSpace);  % Convert to XYZ
% Convert XYZ to CIE L*a*b*
X = Image(:,:,1)/WhitePoint(1);
Y = Image(:,:,2)/WhitePoint(2);
Z = Image(:,:,3)/WhitePoint(3);
fX = f(X);
fY = f(Y);
fZ = f(Z);
Image(:,:,1) = 116*fY - 16;    % L*
Image(:,:,2) = 500*(fX - fY);  % a*
Image(:,:,3) = 200*(fY - fZ);  % b*
end
return;

function Image = luv(Image,SrcSpace)
% Convert to CIE L*u*v* (CIELUV)
WhitePoint = [0.950456,1,1.088754];
WhitePointU = (4*WhitePoint(1))./(WhitePoint(1) + 15*WhitePoint(2) + 3*WhitePoint(3));
WhitePointV = (9*WhitePoint(2))./(WhitePoint(1) + 15*WhitePoint(2) + 3*WhitePoint(3));
Image = xyz(Image,SrcSpace); % Convert to XYZ
U = (4*Image(:,:,1))./(Image(:,:,1) + 15*Image(:,:,2) + 3*Image(:,:,3));
V = (9*Image(:,:,2))./(Image(:,:,1) + 15*Image(:,:,2) + 3*Image(:,:,3));
Y = Image(:,:,2)/WhitePoint(2);
L = 116*f(Y) - 16;
Image(:,:,1) = L;                        % L*
Image(:,:,2) = 13*L.*(U - WhitePointU);  % u*
Image(:,:,3) = 13*L.*(V - WhitePointV);  % v*
return;

function Image = lch(Image,SrcSpace)
% Convert to CIE L*ch
Image = lab(Image,SrcSpace);  % Convert to CIE L*ab
H = atan2(Image(:,:,3),Image(:,:,2));
H = H*180/pi + 360*(H < 0);
Image(:,:,2) = sqrt(Image(:,:,2).^2 + Image(:,:,3).^2);  % C
Image(:,:,3) = H;                                        % H
return;

function Image = huetorgb(m0,m2,H)
% Convert HSV or HSL hue to RGB
N = size(H);
H = min(max(H(:),0),360)/60;
m0 = m0(:);
m2 = m2(:);
F = H - round(H/2)*2;
M = [m0, m0 + (m2-m0).*abs(F), m2];
Num = length(m0);
j = [2 1 0;1 2 0;0 2 1;0 1 2;1 0 2;2 0 1;2 1 0]*Num;
k = floor(H) + 1;
Image = reshape([M(j(k,1)+(1:Num).'),M(j(k,2)+(1:Num).'),M(j(k,3)+(1:Num).')],[N,3]);
return;

function H = rgbtohue(Image)
% Convert RGB to HSV or HSL hue
[M,i] = sort(Image,3);
i = i(:,:,3);
Delta = M(:,:,3) - M(:,:,1);
Delta = Delta + (Delta == 0);
R = Image(:,:,1);
G = Image(:,:,2);
B = Image(:,:,3);
H = zeros(size(R));
k = (i == 1);
H(k) = (G(k) - B(k))./Delta(k);
k = (i == 2);
H(k) = 2 + (B(k) - R(k))./Delta(k);
k = (i == 3);
H(k) = 4 + (R(k) - G(k))./Delta(k);
H = 60*H + 360*(H < 0);
H(Delta == 0) = nan;
return;

function Rp = gammacorrection(R)
Rp = real(1.099*R.^0.45 - 0.099);
i = (R < 0.018);
Rp(i) = 4.5138*R(i);
return;

function R = invgammacorrection(Rp)
R = real(((Rp + 0.099)/1.099).^(1/0.45));
i = (R < 0.018);
R(i) = Rp(i)/4.5138;
return;

function fY = f(Y)
fY = real(Y.^(1/3));
i = (Y < 0.008856);
fY(i) = Y(i)*(841/108) + (4/29);
return;

function Y = invf(fY)
Y = fY.^3;
i = (Y < 0.008856);
Y(i) = (fY(i) - 4/29)*(108/841);
return;

展开全文
• 帮别人做的文献中的程序，原文见 [1]. 江岳文与陈晓榕, 风电波动成本分摊...第二种思路是严格按照公式（1）和公式（2）中的原来编程，设未知数x，迭代求得pw1的和表达式，然后利用matlab来方程的解，然后将解带入到pw1

帮别人做的文献中的程序，原文见
[1]. 江岳文与陈晓榕, 风电波动成本分摊方法. 电力自动化设备, 2020.
要求根据以下原理作图

求等效出力曲线图，如下

想到了两种求解思路，第一种求解思路和文献中的图有出入，编程思路是先求出l和pw的值，满足公式（1），然后讲求得的和相除得到比例因子，然后将l中所有的纵坐标除以比例因子，即得到要求得的pw1
代码如下

第二种思路是严格按照公式（1）和公式（2）中的原来编程，设未知数x，迭代求得pw1的和表达式，然后利用matlab来方程的解，然后将解带入到pw1中
，代码实现如下

clear;clc
pw=x(:,1);
l=x(:,2);
syms x
% k=sum(pw);
pw1=[];
pw1=[pw1;x];

for i=2:length(pw)

q=(((l(i)-l(i-1))/l(i-1))*pw1(i-1))+pw1(i-1);
pw1=[pw1;q];

i
end
res=solve(sum(pw1)-sum(pw));
x=res;
pw1=eval(pw1)


建设使用的时候用第二段代码，严格按照文献中的原理编程，缺点是计算缓慢，数据量大的时候求未知数可能有误差

展开全文
• matlab 代码续行
• Greedy Maximal Scheduling in Wireless NetWorks [论文地址](http://pdfs.semanticscholar.org/2ec2/4fec48b168950217c9d87c7c1114db8a3f00.pdf "")
• target-point formation control论文复现MATLAB程序
• 针对TCM经典论文Channel Coding with Multilevel/Phase Signals的一些推导与仿真
• 1 在你阅读科学论文之前 1.1 先找找开源资源，避免重复编程 1.2 寻找实现你目标的更简便的途径 1.3 注意软件专利 1.4 学习相关领域的更多论文 1.5 保持学习动力 2 三种论文分类 2.1 开创性论文 2.2 抄袭论文...
• matlab中建立了《电力系统碳排放流的计算方法初探》论文的数学模型的仿真，与论文中的案例分析结果完全一致，在论文中标注了其中的一些细节问题，修改了了一些错误，对相关专业的同学有一定的借鉴意义。...
• 之前做的外包，原价200+元 论文图中曲线复现； 图片曲线复现，任意图像的曲线数据提取；输出散点数据，可进行光滑处理
• 基于matlab的表情识别代码目的 这是功能面的Matlab实现：使用了张超，威廉·史密斯等人的功能图的分组密集通信 相依性 该代码是根据一些现有代码，库和数据开发的，其中包括： 成对功能。 M. Ovsjanikov针对纸张功能...
• MTCNN算法分析笔记1. 项目来源（1）论文题目（2）实现目标（3）相关资源2....本次复现论文：Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks ，原作者是使用Matlab进行算法实
• 复现IEEE Transactions on Smart Grid的文章："A Two-Layer Energy Management System for Microgrids With Hybrid Energy Storage Considering Degradation Costs“ 提出了一种用于具有由电池和超级电容器组成的...
• 用卷积滤波器matlab代码计算机视觉各个方向论文速览 上次更新时间：2019/08/30 更新日志 2019/08/30 *- 目录 目标检测的热门论文 2019目标检测CVPR 2019物体检测ICLR 2019物体检测AAAI 2018年目标检测CVPR [DA ...
• 论文“Compressed Channel Estimation for Intelligent Reflecting Surface-Assisted Millimeter Wave Systems”的仿真 PS：这是博主第一次使用CSDN的博客功能，还没弄熟这些功能，先就这样吧。 智能反射面（IRS）...
• 前言看过一些比较知名的推荐系统、CTR预估论文。开【Recommended-System with TensorFlow 2.0】的原因有三个：论文只看理论感觉有些地方简单，但是实践起来...
• 在该论文中所针对的动作是日常生活中喝水动作的研究，并且将喝水这个动作分解为5个子动作，分别是Act1~Act5。除此以外再加上静息动作Act0。 使用肌电采集器将各个动作进行采集并保存数据，数据目录截图如下。其中1-...
• 论文，专利对于同学发展不言而喻。论文不通过，没法毕业，没法毕业就拿不到毕业证，工作也没法找。发表论文数量和质量有利于工作升迁，评职称就需要在核心期刊发布论文。该课程帮助学员了解论文作业的常见误区，如何...
• 风电功率预测。基于PSO-BP神经网络的风电功率预测论文复现matlab程序，注释清晰。
• 多目标优化算法（四）NSGA3(NSGAIII)论文复现以及matlab和python的代码 前沿：最近太忙，这个系列已经很久没有更新了，本次就更新一个Deb大神的NSGA2的“升级版”算法NSGA3。因为multi-objective optimization已经被...

...

matlab 订阅