Command line 不同的函数从命令行和函数内返回
我有一个非常奇怪的情况:我在MATLAB中有一个函数,它调用其他三个主要函数并为我生成两个数字。该函数读取输入的jpeg图像,对其进行裁剪,使用kmeans聚类对其进行分割,并向屏幕输出2个图形-原始图像和指示聚类中心的聚类图像。以下是MATLAB中的函数:Command line 不同的函数从命令行和函数内返回,command-line,matlab,function,Command Line,Matlab,Function,我有一个非常奇怪的情况:我在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
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\u image
显示的第一个图像与我运行函数时的图像相同(因此我知道聚类是在同一个图像上完成的),但聚类数是16(这是我所期望的)
事实上,当我在整个图像数据库上运行我的process\u database\u images()
函数时,每个图像都被评估为有3个簇(这是一个问题),而当我单独测试一些图像时,我得到的簇数在12-16个之间,这是我喜欢和期望的
为什么会有这样的差异?我的process\u database\u images()函数中是否存在语法错误?如果需要我提供更多代码(即分段图像功能或裁剪图像功能),请告知我
谢谢
编辑:
我找到了问题的根源。在我的
load_image
函数中,调用img=imread(filename)
后,我将图像转换为double:`img=im2double(img);'。当我把这行注释掉时,我得到了期望的结果。有人知道为什么会这样吗?(以及我如何“关闭”这个问题,因为我已经找到了问题所在)。试着在命令行上使用与您编写的函数相同的返回值调用您的函数。而不是
>> segment_image(img);
尝试:
Matlab中的函数检查预期的返回值数量,并可能根据返回值的不同而表现不同。
函数顶部的“全部清除”是不必要的,可能是问题的根源
此外,关闭所有警告也是一个坏主意,因为它可能会掩盖其他问题。让我们看看这段代码,通过删除冗余代码或未使用的代码简化:
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
看起来这段代码只在双嵌套for循环中运行了一次,可能这就是为什么您只得到一个答案,三个簇。尝试了它-仍然从命令行获得了所需的结果我关闭了警告,因为我知道在k-means聚类过程中,当形成空簇时会产生警告。我用注释掉的两行代码尝试了这段代码,但仍然得到了相同的结果。我简化了代码,这样我只处理一个图像,而不是我拥有的整个数据库。但是我要寻找的一个答案是16个集群,而不是3个,因为我在同一个图像上,在函数中,在命令行上执行crop_image和segment_image。
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