Filter löschen
Filter löschen

converting a cell array into numeric arrays

4 Ansichten (letzte 30 Tage)
yasmine
yasmine am 9 Dez. 2011
I am dividing images into blocks using mat2cell then i want to convert each block from cell array to a numeric array to make histogram for every block.
I need to make it in a for loop.
so what do you think?
here is my code:
nb(1) = ceil(256/ b(1)); % number of blocks in each dimension
nb(2) = ceil(384/ b(2)); % number of blocks in each dimension
C=mat2cell(images,ones(256/ b(1),1)*b(1),ones(384/ b(2),1)*b(2),3);% dividing the Image into blocks.
%C1=mat2cell(images,repmat(b(1),1,nb(1)), repmat(b(2),1,nb(2)),3);
m=1;
while (m < ((nb(1)*nb(2))+1))
for i=1:nb(1)%256
for j=1:nb(2)%384
[counts(i,j), x(i,j)] = imhist(C{i,j});%error
end
end
end
end
end
it gave me these errors:
??? Error using ==> iptcheckinput Function IMHIST expected its first input, I or X, to be two-dimensional.
Error in ==> imhist>parse_inputs at 281 iptcheckinput(a, {'double','uint8','int8','logical','uint16','int16','single','uint32', 'int32'}, ...
Error in ==> imhist at 59 [a, n, isScaled, top, map] = parse_inputs(varargin{:});
Error in ==> fll1 at 26 [counts(i,j), x(i,j)] = imhist(C{i,j});

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 9 Dez. 2011
Just use cellfun with imhist as the function being applied.
As for the two dimensionality, use rgb2gray to convert your rgb image to a gray image so that data are 2-dimensional. Or use a for-loop form 1:3 to apply imhist to each slice of the third dimension.
  3 Kommentare
yasmine
yasmine am 9 Dez. 2011
i used cellfun with imhist but this error occurred:
??? Error using ==> iptcheckinput
Function IMHIST expected its first input, I or X, to be two-dimensional.
Error in ==> imhist>parse_inputs at 281
iptcheckinput(a,
{'double','uint8','int8','logical','uint16','int16','single','uint32', 'int32'}, ...
Error in ==> imhist at 59
[a, n, isScaled, top, map] = parse_inputs(varargin{:});
Error in ==> fll1 at 22
H=cellfun(@imhist,C(:,:,1));
Walter Roberson
Walter Roberson am 9 Dez. 2011
Because you did not convert your whole image to grayscale, or convert your block to grayscale, or apply imhist to each of the panes separately.
H = cellfun(@(B) imhist(rgb2gray(B)), C);
Note that your cell array C is 2 dimensional, and it is the contents of the cell that are 3 dimensional. If you wanted to just run imhist against the red plane, you could use
H = cellfun(@(B) imhist(B(:,:,1)), C);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 9 Dez. 2011
Copied from the original thread:
You are trying to apply imhist() to a (subsection of) a truecolor image (which is thus a 3D matrix). imhist() is only for grayscale images. You should convert your whole image to grayscale before you break it up in to blocks; or you should convert each block to grayscale and imhist that as you go; or you should imhist() each of the three color planes separately.
  4 Kommentare
Walter Roberson
Walter Roberson am 9 Dez. 2011
You would only convert an array of cells with cell2mat. e.g., to take a 2 x 3 cell array and unpack it back to (say) the original 37 x 19 numeric array. cell2mat should seldom be used on a single element of a cell array: to convert a single element of a cell array to numeric form, just use {} indices to reference the cell.
I think you should probably be looking in to blockproc() or blkproc() instead of worrying about mat2cell and cell2mat .
yasmine
yasmine am 9 Dez. 2011
yes i think so !
would you help me with code to use blkproc as i tried before and failed so i switched to mat2cell

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by