MATLAB: different function returns from command line and within function

Posted by Myx on Stack Overflow See other posts from Stack Overflow or by Myx
Published on 2010-05-04T19:26:56Z Indexed on 2010/05/04 19:38 UTC
Read the original article Hit count: 300

Filed under:
|
|

Hello:

I have an extremely bizzare situation: I have a function in MATLAB which calls three other main functions and produces two figures for me. The function reads in an input jpeg image, crops it, segments it using kmeans clustering, and outputs 2 figures to the screen - the original image and the clustered image with the cluster centers indicated. Here is the function in MATLAB:

function [textured_avg_x photo_avg_x] = process_database_images()
clear all
warning off %#ok
type_num_max = 3; % type is 1='texture', 2='graph', or 3='photo'
type_num_max = 1;
img_max_num_photo = 100; % 400 photo images
img_max_num_other = 100; % 100 textured, and graph images

for type_num = 1:2:type_num_max
    if(type_num == 3)
        img_num_max = img_max_num_photo;
    else
        img_num_max = img_max_num_other;
    end
    img_num_max = 1;
    for img_num = 1:img_num_max
        [type img] = load_image(type_num, img_num);
        %img = imread('..\images\445.jpg');
        img = crop_image(img);
        [IDX k block_bounds features] = segment_image(img);

    end
end
end

The function segment_image first shows me the color image that was passed in, performs kmeans clustering, and outputs the clustered image. When I run this function on a particular image, I get 3 clusters (which is not what I expect to get).

When I run the following commands from the MATLAB command prompt:

>> img = imread('..\images\texture\1.jpg');
>> img = crop_image(img);
>> segment_image(img);

then the first image that is displayed by segment_image is the same as when I run the function (so I know that the clustering is done on the same image) but the number of clusters is 16 (which is what I expect).

In fact, when I run my process_database_images() function on my entire image database, EVERY image is evaluated to have 3 clusters (this is a problem), whereas when I test some images individually, I get in the range of 12-16 clusters, which is what I prefer and expect.

Why is there such a discrepancy? Am I having some syntax bug in my process_database_images() function? If more code is required from me (i.e. segment_images function, or crop_image function), please let me know.

Thanks.

© Stack Overflow or respective owner

Related posts about matlab

Related posts about function