Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
利用Matlab'选择8位平面;s bitget()。显示8位平面?_Matlab_Image Processing - Fatal编程技术网

利用Matlab'选择8位平面;s bitget()。显示8位平面?

利用Matlab'选择8位平面;s bitget()。显示8位平面?,matlab,image-processing,Matlab,Image Processing,我正在努力使这个代码工作,但我不能找出问题 img = imread('cameraman.tif'); for i = 1:8 a{i}=bitget(img,i); subplot(2,4,i), imshow(logical(a{i})), title('Bit plane'); end 输出错误为 Unable to perform assignment because brace indexing is not supported for variables of

我正在努力使这个代码工作,但我不能找出问题

img = imread('cameraman.tif');

for i = 1:8
    a{i}=bitget(img,i);
    subplot(2,4,i), imshow(logical(a{i})), title('Bit plane');
end
输出错误为

Unable to perform assignment because brace indexing is not supported for variables of this type.
    Error in (line 15)
        a{i}=bitget(img,i);

你看到问题出在哪里了吗?感谢错误消息表明
a
不支持大括号索引。这意味着,在运行代码之前,
a
已经定义

要编写健壮的代码,您应该始终初始化所有变量。在这种情况下,写
a={}在循环之前。这将确保
a
的类型正确

更好的方法是预先分配
a
以获得正确的大小,这样它就不会在每次循环迭代时都调整大小:

a = cell(8,1);

我使用MATLAB制作了8位平面,以下是我的代码供您参考:)


如果我复制并粘贴您的代码,它将按预期工作,不会出现错误。由于错误消息指出错误发生在第15行,但示例代码只有6行,因此导致错误的部分似乎丢失了。我刚刚粘贴了“工作”摘录,从1到8的实数行只是注释,所以实际的15行是a{I}=bitget(img,I);有趣的是,当我运行它一次时,我得到了错误。再次运行它可以使它工作,甚至可以清除workspace@AmosCappellaro在运行代码之前,请尝试
清除a
。它现在工作得很好,我没有想过。非常感谢。
row=512;  col=512;
file_raw = 'tif.raw';
fin=fopen(file_raw,'r');
I=fread(fin,row*col,'uint8=>uint8'); 
Z=reshape(I,row,col);
Z=Z';
fclose(fin);
figure('Name',file_raw,'NumberTitle','off');
subplot(3,3,1);
imshow(Z);
title(file_raw)  

B=zeros(size(I));
B1=bitset(B,1,bitget(I,1));
B2=bitset(B,2,bitget(I,2));
B3=bitset(B,3,bitget(I,3));
B4=bitset(B,4,bitget(I,4));
B5=bitset(B,5,bitget(I,5));
B6=bitset(B,6,bitget(I,6));
B7=bitset(B,7,bitget(I,7));
B8=bitset(B,8,bitget(I,8));
B = B1+B2+B3+B4+B5+B6+B7+B8;
B=uint8(B);
Br=reshape(B,row,col);
Br=Br';
subplot(3,3,2);
imshow(Br);
title('reconstruct');

C=255*bitget(I,5);
C=uint8(C);
Cr=reshape(C,row,col);
Cr=Cr';
subplot(3,3,3);
imshow(Cr);
title('bitget 5');

D=255*bitget(I, 8);
D=uint8(D);
Dr=reshape(D,row,col);
Dr=Dr';
subplot(3,3,4);
imshow(Dr);
title('bitget 8');

file_bprint = 'bprint7.raw';
fin=fopen(file_bprint,'r');
I=fread(fin,row*col,'uint8=>uint8'); 
E=reshape(I,row,col);
E=E';
fclose(fin);
subplot(3,3,5);
imshow(E);
title(file_bprint);