Output argument <XX> (and possibly others) not assigned a value in the execution with <YY> function

91 次查看(过去 30 天)
I have a function script as follows:
function [h]=getColorHist(im, color_table, opt)
im = rgb2hsv(im);
[ht,wid,~]=size(im);
if opt == 'euc' % euclidean distance
D = pdist2(color_table, reshape(im, [ht*wid, 3]));
end
end
I call this function from a different script HW3.m from the command line and get the following error:
HW3
Output argument "h" (and possibly others) not assigned a value in the execution with "getColorHist"
function.
Error in HW3 (line 23)
f_hist(f_cnt, :) = getColorHist(hsv_im, color_table,'euc')
where HW3.m is:
im_path='/MATLAB Drive/Published/NWPU-RESISC45_1';
ids = {'airplane', 'airport', 'baseball_diamond', 'baseball_court', 'beach','bridge','chaparral', 'church', 'circular_farmland', 'cloud','commercial_area','dense_residental', 'desert', 'forest', 'freeway'};
kColor = 64;
color_table = getHSVColorTable_2(im_path, ids, kColor);
nClass = length(ids); nImg = 40;
f_hist = zeros(nImg*nClass, kColor);
labels = zeros(nImg*nClass, 1);
f_cnt = 0;
for k=1:nClass
% list of files
class_path = sprintf('%s/%s', im_path, ids{k});
f_list = dir(sprintf('%s/*.jpg', class_path));
offs = randperm(length(f_list));
% load random 256 pixels from each images
if isempty(offs) == 0
for j=1:nImg
f_cnt = f_cnt + 1;
% read in a random image from the class
im = imread(sprintf('%s/%s', class_path, f_list(offs(j)).name));
hsv_im = rgb2hsv(im);
f_hist(f_cnt, :) = getColorHist(hsv_im, color_table,'euc');
labels(f_cnt) = k;
% plot
if (1)
figure(3); subplot(nClass, nImg, f_cnt);
plot(f_hist(f_cnt, :)); grid on;
end
end
end
end
Can anyone check whether my code has any errors to let this problem occur? This is mind-boggling to me, appreciate any help!
Here this the script for function getHSVColorTable_2 in HW3.m:
function [color_table]=getHSVColorTable_2(im_path, ids, kColor)
dbg=1;
if dbg
im_path='/MATLAB Drive/Published/NWPU-RESISC45_1';
ids = {'airplane', 'airport', 'baseball_diamond', 'baseball_court', 'beach','bridge','chaparral', 'church', 'circular_farmland', 'cloud','commercial_area','dense_residental', 'desert', 'forest', 'freeway'};
kColor = 64;
end
nClass = length(ids); nImg = 40; nPixelPerImage = 256;
% sample images from the repository to train a global HSV color table
% training pixels
x = zeros(nImg*nPixelPerImage*nClass, 3); cnt = 0;
for k=1:nClass
% list of files
class_path = sprintf('%s/%s', im_path, ids{k});
f_list = dir(sprintf('%s/*.jpg', class_path));
offs = randperm(length(f_list));
% load random 256 pixels from each images
if isempty(offs) == 0
for j=1:nImg
% read in a random image from the class
im = imread(sprintf('%s/%s', class_path, f_list(offs(j)).name));
hsv_im = rgb2hsv(im);
[ht,wid,~] = size(im);
hsv_im = reshape(hsv_im, [ht*wid, 3]);
pix_offs = randperm(ht*wid);
x(cnt+1:cnt+nPixelPerImage, :) = hsv_im(pix_offs(1:nPixelPerImage), :);
cnt = cnt + nPixelPerImage;
end
% plot
if dbg
figure(1); subplot(1, nClass, k);
subplot(1, nClass, k); imshow(im); title(ids{k});
end
end
end

采纳的回答

KSSV
KSSV 2022-2-23
The error is simple. In the given function:
function [h]=getColorHist(im, color_table, opt)
im = rgb2hsv(im);
[ht,wid,~]=size(im);
if opt == 'euc' % euclidean distance
D = pdist2(color_table, reshape(im, [ht*wid, 3]));
end
end
The output seeked is h. Did you see any variable calculated in the function on the name h? No, the variable is not ther ein the function and you are trying to take it as output, so the error pops out.
I think the output should not be h, it shoudle be D. Change the function to:
function D=getColorHist(im, color_table, opt)
im = rgb2hsv(im);
[ht,wid,~]=size(im);
if opt == 'euc' % euclidean distance
D = pdist2(color_table, reshape(im, [ht*wid, 3]));
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by