Bao Ngo asked . 2022-12-15

Output argument (and possibly others) not assigned

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

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

 

color-histogram , Pattern Recognition and Classification , AI, Data Science, and Statistics

Expert Answer

Prashant Kumar answered . 2024-12-20 19:39:22

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


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.