【发布时间】:2010-05-04 19:26:56
【问题描述】:
我有一个非常奇怪的情况:我在 MATLAB 中有一个函数,它调用其他三个主要函数并为我生成两个数字。该函数读取输入的 jpeg 图像,对其进行裁剪,使用 kmeans 聚类对其进行分割,然后将 2 个图形输出到屏幕 - 原始图像和带有聚类中心的聚类图像。这是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
函数segment_image首先向我显示传入的彩色图像,执行kmeans聚类,并输出聚类图像。当我在特定图像上运行此函数时,我得到 3 个集群(这不是我期望得到的)。
当我从 MATLAB 命令提示符运行以下命令时:
>> img = imread('..\images\texture\1.jpg');
>> img = crop_image(img);
>> segment_image(img);
那么segment_image 显示的第一张图像与我运行该函数时相同(所以我知道聚类是在同一张图像上完成的)但聚类数为 16(这是我所期望的) )。
事实上,当我在整个图像数据库上运行 process_database_images() 函数时,每个图像都被评估为有 3 个集群(这是一个问题),而当我单独测试一些图像时,我得到的范围是 12 -16 个集群,这是我喜欢和期望的。
为什么会有这样的差异?我的 process_database_images() 函数中是否有一些语法错误?如果需要我提供更多代码(即 segment_images 函数或crop_image 函数),请告诉我。
谢谢。
编辑:
我找到了问题的根源。在我的load_image 函数中,在我调用img = imread(filename) 之后,我将图像转换为双精度:`img = im2double(img);'。当我注释掉这一行时,我得到了想要的结果。有谁知道为什么会这样? (以及我找到问题后如何“关闭”这个问题)。
【问题讨论】:
标签: command-line matlab function