How to solve the problem?
2 次查看(过去 30 天)
显示 更早的评论
I have a 198x198 matrix, an image. I need to get 3x3 matrices each from the above matrix. Total, 22 matrices, how to access each of them? can somebody help me with the code??
回答(1 个)
Walter Roberson
2018-4-26
blocks = mat2cell(YourMatrix, 3*ones(1,22), 3*ones(1,22));
then you would access (for example) blocks{7,4) to give you the 7th block of 3 down, 4'th block of 3 across.
17 个评论
Darsana P M
2018-4-26
blocks = mat2cell(L0, 3*ones(1,22), 3*ones(1,22));
I got an error.here,L0=200x200 matrix?
Walter Roberson
2018-4-26
You said before your arrays were 198. That size divides neatly into 3x3 arrays. When you have 200x200 then you would have 2 left over after dividing into groups of 3. What do you want to do with the extra 2x3 and 3x2 and the corner 2x2?
Walter Roberson
2018-4-26
[r, c] = size(L0);
temp = L0;
temp( r+1:ceil(r/3)*3, c+1:ceil(c/3)*3 ) = 0;
[r, c] = size(temp);
blocks = mat2cell(temp, 3*ones(1,r/3), 3*ones(1,c/3));
clear temp;
Darsana P M
2018-4-26
Sir,the code works fine. From this how to extract 3x3 matrix and then perform certain operations, and then put them into an new matrix,200x200?
Walter Roberson
2018-4-26
this_block = blocks{7,4}; %extract 3x3 matrix
new_block = reshape(sort(this_block(:)), size(this_block)); %perform certain operations
new_blocks{7,4} = new_block;
new_matrix = cell2mat(new_blocks);
new_matrix = new_matrix(1:size(L0,1), 1:size(L0,2)); %trim out padding
Darsana P M
2018-4-26
Index exceeds matrix dimensions.
Error in daru (line 49) new_matrix = new_matrix(1:size(L0,1), 1:size(L0,2)); %trim out padding Sir I got this error?
Walter Roberson
2018-4-26
new_blocks = blocks;
before the above code would ensure that the blocks not changed were copied over.
Your operations would replace
new_block = reshape(sort(this_block(:)), size(this_block)); %perform certain operations
Do whatever operations you need to this_block, assigning the result to new_block .
Darsana P M
2018-4-27
sir,the dimensions became wrong.
Index exceeds matrix dimensions.
Error in daru (line 60) new_matrix = new_matrix(1:size(L0,1), 1:size(L0,2)); %trim out padding
Sir, i have a doubt, after doing all these operations, will a get a 3x3 matrix or the entire image, 200x200??
Walter Roberson
2018-4-27
>> L0 = imread('cameraman.tif');
>> [r, c] = size(L0);
temp = L0;
temp( r+1:ceil(r/3)*3, c+1:ceil(c/3)*3 ) = 0;
[r, c] = size(temp);
blocks = mat2cell(temp, 3*ones(1,r/3), 3*ones(1,c/3));
clear temp;
>> size(blocks)
ans =
86 86
>> new_blocks = blocks;
>>
this_block = blocks{7,4}; %extract 3x3 matrix
new_block = reshape(sort(this_block(:)), size(this_block)); %perform certain operations
new_blocks{7,4} = new_block;
new_matrix = cell2mat(new_blocks);
new_matrix = new_matrix(1:size(L0,1), 1:size(L0,2)); %trim out padding
>> size(new_matrix)
ans =
256 256
>> size(L0)
ans =
256 256
>> image(new_matrix)
>> colormap(gray(256))
Darsana P M
2018-4-27
clc;
close all;
clear all;
k=0;
I = dicomread('C:\Users\Click Me\Desktop\NIELIT\Output images\dic5.dcm');
info = dicominfo('C:\Users\Click Me\Desktop\NIELIT\Output images\dic5.dcm');
figure(1);
image(I);
h=rgb2gray(I);
imhist(h);
N= 257;
l=zeros(200,400);
B_mat=h(1:200,:);
figure(2);
image(B_mat);
%Rot=[-15,-20,60,20;52,-39,0,25;36,48,32,13;12,43,23,76];
Rot=[-15,-20,60;52,-39,0;36,48,32]
o=[3 4 2 6];
q1=o;
q2=qInv(q1);
L0=B_mat(:,1:200);
R0=B_mat(:,201:400);
h=qGetR(R0);
[M,N]=size(L0);
b=3;
M=M-rem(M,b);
N=N-rem(N,b);
L0=L0;
%A=zeros(200,200);
lo=L0(1:200,1:200);
%s=size(lo);
% dim=198;
% [row column]=size(lo);
% count=0;
% Bigblock=zeros(198,198);
[r, c] = size(L0);
temp = L0;
temp( r+1:ceil(r/3)*3, c+1:ceil(c/3)*3 ) = 0;
[r, c] = size(temp);
blocks = mat2cell(temp, 3*ones(1,r/3), 3*ones(1,c/3));
%J=cell2mat(blocks);
clear temp;
this_block = blocks{7,4};
%extract 3x3 matrix
n1=(this_block).*Rot;
L1=mod(n1,N);
%B=cat(dim,Bigblock,block);
R1=(L1 + this_block);
m=mod(R1,N);
%new_block=this_block;
%new_block=mat2cell(blocks);
%new_block = reshape(sort(this_block(:)), size(this_block)); %perform certain operations
new_blocks{7,4} = new_block;
new_matrix = cell2mat(new_blocks);
%new_blocks= cell2mat(blocks);
new_matrix = new_matrix(1:size(L0,1), 1:size(L0,2)); %trim out padding
Sir, Thanks a lot for your help.But could you please help me, for me there is an issue doing .* multiplication step.
Walter Roberson
2018-4-27
The result if your dicomread() is likely to be either uint8 or int16, maybe uint16. rgb2gray() will preserve that data type. The way you extract data into L0 looks like it will preserve that data type. The mat2cell() will preserve the data type.
So this_block should be the same data type as dicomread() returned, probably either uint8 or int16. You try to .* with double data. That would be trying to calculate with mixed data types. MATLAB is likely to refuse.
If MATLAB did not refuse, then it looks plausible to me that some of the entries might try to be negative even though the original values were positive. If so then you have a problem, because if the original were uint8 then negatives are not possible and the negatives are going to be converted to 0.
Try
n1 = double(this_block).*Rot;
and when you go to write back into the array,
new_blocks{7,4} = cast(new_block, class(this_block));
which will force the output to be the same data type as the original.
Darsana P M
2018-4-27
Thank you sir
Error using cast Conversion to double from cell is not possible.
Error in daru (line 65) new_blocks{7,4} = cast(new_block,class(this_block));
Sir, I got this error.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Number Theory 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)