views:

47

answers:

3

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.

EDIT:

I found the source of the problem. In my load_image function, after I call img = imread(filename), I convert the image to double: `img = im2double(img);'. When I comment this line out, I get the desired result. Anyone know why this happens? (and also how I can 'close' this question since I have located the problem).

A: 

Try calling your function on the command line with the same amount of return values as in the function you wrote. Instead of

>> segment_image(img);

Try:

>> [IDX k block_bounds features] = segment_image(img);

Functions in Matlab check how many return values are expected, and may behave differently depending on that.

Nathan Fellman
tried it - still get the desired result from the command line
Myx
+1  A: 

clear all at the top of your function is unnecessary and may be the source of your trouble.

Also, turning off all warnings is a bad idea since it may mask other problems.

Matthew Simoneau
I turned off warnings because I know that warnings will be produced during k-means clustering when empty clusters are formed.I tried the code with the 2 lines commented out, and I still get the same result.
Myx
+1  A: 

Let's look at this code, simplified by removing redundant code or unused code:

function [textured_avg_x photo_avg_x] = process_database_images()

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 %% 1:2:1 => 1

    img_num_max = 1; %This nullfiies everything in the if block above anyways

    for img_num = 1:img_num_max %% 1:1 => 1
        [type img] = load_image(type_num, img_num); %% Input (1,1)
        img = crop_image(img);
        [IDX k block_bounds features] = segment_image(img);

    end
end
end

It looks like this code runs through the double nested for loop exactly once, maybe that is why you get only one answer, three clusters.

MatlabDoug
one answer is what I am looking for, for now. I simplified the code so that I only process 1 image, as opposed to the entire database that I have. But the one answer that I'm looking for is 16 clusters, not 3, since I am executing crop_image and segment_image on the same image here, in the function, and on the command line.
Myx