图窗口显示matlab

时间:2014-03-27 14:19:59

标签: image matlab

我已编写此代码以帮助我比较不同的图像直方图,但是当我运行它时,我会弹出一个图形窗口。我在代码中的任何地方都看不到我写的imshow,我真的很困惑。谁能明白为什么?感谢

%ensure we start with an empty workspace
clear

myPath= 'C:\coursework\'; %#'
number_of_desired_results = 5; %top n results to return

images_path = strcat(myPath, 'fruitnveg');
images_file_names = dir(fullfile(images_path, '*.png'));
images = cell(length(images_file_names), 3);
number_of_images = length(images);

%textures contruction
%loop through all textures and store them
disp('Starting construction of search domain...');
for i = 1:length(images)
    image = strcat(images_path, '\', images_file_names(i).name); %#'

    %store image object of image
    images{i, 1} = imread(image);
    %store histogram of image
    images{i, 2} = imhist(rgb2ind(images{i, 1}, colormap(colorcube(256))));
    %store name of image
    images{i, 3} = images_file_names(i).name;
    disp(strcat({'Loaded image '}, num2str(i)));
end
disp('Construction of search domain done');

%load the three example images

RGB1 = imread('C:\coursework\examples\salmon.jpg');
X1 = rgb2ind(RGB1,colormap(colorcube(256)));
example1 = imhist(X1);

RGB2 = imread('C:\coursework\examples\eggs.jpg');
X2 = rgb2ind(RGB2,colormap(colorcube(256)));
example2 = imhist(X2);

RGB3 = imread('C:\coursework\examples\steak.jpg');
X3 = rgb2ind(RGB3,colormap(colorcube(256)));
example3 = imhist(X3);

disp('three examples loaded');

disp('compare examples to loaded fruit images');

results = cell(length(images), 2);

results2 = cell(length(images), 2);

results3 = cell(length(images), 2);

for i = 1:length(images)
    results{i,1} = images{i,3};
    results{i,2} = hi(example1,images{i, 2});
end

results = flipdim(sortrows(results,2),1);

for i = 1:length(images)
    results2{i,1} = images{i,3};
    results2{i,2} = hi(example2,images{i, 2});
end

results2 = flipdim(sortrows(results2,2),1);

for i = 1:length(images)
    results3{i,1} = images{i,3};
    results3{i,2} = hi(example3,images{i, 2});
end

results3 = flipdim(sortrows(results3,2),1);

3 个答案:

答案 0 :(得分:4)

colormap函数设置当前图形的色彩映射,如果没有创建图形。

imhist的第二个参数应该是直方图中使用的二进制数,而不是色图。

答案 1 :(得分:2)

在Matlab调试器中运行您的代码,逐行逐步执行,并查看弹出图形窗口的时间。那会告诉你是什么造就了它。

答案 2 :(得分:1)

Etienne的回答是正确的,为什么你得到一个数字,但我只想补充一点,colormap在这段代码中是不必要的:

images{i, 2} = imhist(rgb2ind(images{i, 1}, colormap(colorcube(256))));

您只需要:

images{i, 2} = imhist(rgb2ind(images{i, 1}, colorcube(256)));

rgb2ind的第二个输入应该是色彩映射,是的。但colorcube 的输出已经是一个色彩映射。除非你有一个现有的图形,并且你想要设置它的颜色图或检索它当前正在使用的颜色图,否则不需要实际的函数colormap

除了打开一个不必要的数字之外,现有代码的输出不会出错,因为我认为在这种情况下colormap将作为输出参数传递给作为输入参数的colormap。例如,如果要将当前图形颜色映射设置为其中一个内嵌,则返回实际的颜色映射:

cmap = colormap('bone');