矩阵维度的matlab代码出错

时间:2015-10-18 14:24:20

标签: matlab

% Image Arithmetic Operations
% Image addition is done between two similar size of image, so image resize
% function is used to make size of both image same.
% I=I1+I2
clc
close all
I1=imread('test.jpg');
I2=imread('test_1.jpg');
subplot(2,2,1);imshow(I1);title('Original image I1');
subplot(2,2,2);imshow(I2);title('Original image I2');
I=I1+I2; % Addition of two images
subplot(2,2,3);imshow(I);title('Addition of image I1+I2');
I=I1-I2; % Subtraction of two images
subplot(2,2,4);imshow(I);title('Subtraction of image I1-I2');
figure;
subplot(2,2,1);imshow(I1);title('Original image I1');
I=I1+50;
subplot(2,2,2);imshow(I);title('Bright image I');
I=I1-100;
subplot(2,2,3);imshow(I);title('Dark image I');
M=imread('key.png');
M=im2bw(M); % Converts into binary image having 0s and 1s
I=uint8(I1).*uint8(M); % Type casting before multiplication
subplot(2,2,4);imshow(I);title('Masked Image I');
%clear all;
[filename,pathname]=uigetfile({'*.bmp;*.jpg;*.gif','Choose Image File'});
myimage=imread(filename);
if(size(myimage,3)==3)
    myimage=rgb2gray(myimage);
end
[Rows,Cols]=size(myimage);
newimage=zeros(Rows,Cols);
k=1;
while k<5
    for i=1:Rows
    for j=1:Cols
        if k==1
            newimage(i,j)=myimage(i,j)-100;
        end
        if k==2
            newimage(i,j)=myimage(i,j)-50;
        end
        if k==3
            newimage(i,j)=myimage(i,j)+50;
        end
        if k==4
            newimage(i,j)=myimage(i,j)+50;
        end
    end
    end
subplot(2,2,k);imshow(newimage,[]);
k=k+1;
end

% calculate mean value

[Rows,Cols]=size(myimage);
newimage=zeros(Rows,Cols);
total=0;
for i=1:Rows
    for j=1:Cols
        total=total+myimage(i,j);
    end
end
average=total/(Rows*Cols);

我遇到了大麻烦,因为这个错误没有解决,我是matlab编码的新手,所以请帮助我,你的解决方案将来会受到我的赞赏。 我遇到了下面提到的错误:

  

???使用==&gt;时出错时间矩阵维度必须一致。

     

==&gt;中的错误DIP_3 at 23 I = uint8(I1)。* uint8(M); %之前输入类型   乘法

1 个答案:

答案 0 :(得分:1)

假设您使用的三张图片(test.jpgtest_1.jpgtest.png)大小相同,则在调用im2bw函数时会出现问题

M=im2bw(M);

输入矩阵M是3D矩阵(例如100x100x3),而输出矩阵只是2D矩阵(100x100)。

生成错误是因为您将矩阵I1(也是3D矩阵)乘以2D矩阵。

I=uint8(I1).*uint8(M);

因此,您必须将矩阵M设为3D矩阵。

如果要在矩阵I1上应用相同的缩放因子,可以执行以下操作:

M=imread('key.png');
M0=im2bw(M); % Converts into binary image having 0s and 1s
M(:,:,2)=M0;
M(:,:,3)=M0;

否则,您必须以某种方式定义M(:,:,2)M(:,:,3)

希望这有帮助。