我的matlab代码出错了

时间:2014-11-10 05:54:57

标签: matlab image-processing

我对此代码有问题,因为它无法正常工作!我不明白为什么! 我试图创建一个高斯金字塔来获得多分辨率图像,但是当我试图运行此代码时,这个问题出现了 (((错误:下标索引必须是实数正整数或逻辑。)))  ,我试过几次解决但我失败了! ..这个代码对我来说非常必要,因为我需要它来完成我的毕业设计。

clear

clc
% create the convolution kernel 

%    kernel size
size = 8 ;             

%     standard deviation  used  for antialiasing
std_dev= 0.8 ; 

%     convolution kernel used to smooth the image before sampling
k = zeros(size);

%  Gaussian mean value
m = size / 2 ;

%     set the initial value of  k_accomulate
k_accom = 0;

%  getting the value for the normalized kernel

for i=1:1:size
    for j=1:1:size

        k(i,j) = exp(-((i-m).^2+(j-m).^2)/(2*std_dev.^2))/(2*pi*std_dev.^2);                          
        k_accom = k_accom + k(i,j);                                                                                           

    end;
end;

% to get the normalized value

for i=1:1:size
    for j=1:1:size

        k(i,j) = k(i,j)/k_accom ;                                                                                                    %      normalized kernel value for each  i and j 

    end;
end;

% kernel values are ready 

% Before sampling the image is convolved with a gaussian filter

% read the Original Image

image1= imread('1.jpg');

old_size= size(image1);

% set the reduction factor

red_factor= 2 ;

% convolve filter kernel with the picture
                                                                                                                                                                                               % conv_img = image;     X  forget it
%  conv_img = filter2(k,image);

% compute the size of the reduced image

    new_row = old_size(1)/red_factor;
    new_col = old_size(2)/red_factor;

% extract the reduced image 

    new_image = zeros(new_row,new_col);
    for i=1:1:new_row
        for j=1:1:new_col
            new_image(i,j) = conv_img(i*red_factor,j*red_factor);
        end;
    end;

imshow(new_image);

1 个答案:

答案 0 :(得分:2)

问题是因为您使用以下行声明size为变量:

%    kernel size
size = 8 ; 

通过执行此操作,您将覆盖MATLAB函数size,然后尝试使用该函数调用:

old_size= size(image1);

你能看到这样做的问题吗?不要使用size作为变量名称,如果old_size(1)old_size(2)是偶数(因为您将它们除以red_factor,它应该可以正常工作是2)。

总而言之,请使用以下内容:

%    kernel size
kernel_size = 8 ; 

并将size的所有后续出现次序替换为kernel_size,除非您实际调用size中的old_size= size(image1);函数。