MATLAB中的函数定义上下文

时间:2013-11-11 10:21:39

标签: matlab image-processing

我编写了一个MATLAB程序来渲染2个给定图像的“Delta E”或色差图像。但是,当我运行程序时,我收到此错误:

Error: File: deltaE.m Line: 6 Column: 1 Function definitions are not permitted in this context.

以下是该计划:

imageOriginal = imread('1.jpg');
imageModified = imread('2.jpg');
function [imageOut] = deltaE(imageOriginal, imageModified)

[imageHeight imageWidth imageDepth] = size(imageOriginal);

% Convert image from RGB colorspace to lab color space.
cform = makecform('srgb2lab');
labOriginal = applycform(im2double(imageOriginal),cform);
labModified = applycform(im2double(imageModified),cform);

% Extract out the color bands from the original image
% into 3 separate 2D arrays, one for each color component.
L_original = labOriginal(:, :, 1); 
a_original = labOriginal(:, :, 2); 
b_original = labOriginal(:, :, 3);

L_modified = labModified(:,:,1);
a_modified = labModified(:,:,2);
b_modified = labModified(:,:,3);

% Create the delta images: delta L, delta A, and delta B.
delta_L = L_original - L_modified;
delta_a = a_original - a_modified;
delta_b = b_original - b_modified;

% This is an image that represents the color difference.
% Delta E is the square root of the sum of the squares of the delta images.
delta_E = sqrt(delta_L .^ 2 + delta_a .^ 2 + delta_b .^ 2);

imageOut = delta_E;

end

我可能犯了一个初学者错误,因为我只有17岁而且我开始使用MATLAB。如果你能告诉我我做错了什么就好了。

1 个答案:

答案 0 :(得分:1)

您无法在script内定义功能。您需要在单独的文件上定义函数,或者将脚本转换为(主)函数,然后您的其他函数将是subfunctions。另请参阅here

编辑:从Matlab R2016b你可以用脚本定义本地函数;见here