Create an empty array of size (MxN) for xlswrite.
3 views (last 30 days)
Show older comments
I have an Image which is 1944® x 2592 (C). I would like to take one column at a time; treating each column as an Image and calculate how many pixels among each row of that column contains value > half(max) value of that column. Those number of pixels are to be written to the excel sheet corresponding to it's respective column.
Here is what I have tried so far, I am not able to write it successfully.
clc;
sig = rgb2gray(imread('1.bmp')); % read in the image.
% imshow(sig);
ArraySize = size(sig); %1944(R) x 2592 (C)
[maxval, maxloc] = max(sig(:)); % Gives the max and the location
maxval; % max value
[maxloc_row, maxloc_col] = ind2sub(size(sig), maxloc); % convert logical
%-------------------------------------------------------------------%
% Count pixels through each column > half(max) value of that column
%-------------------------------------------------------------------%
newfilename = 'Results.csv'; % write new values to .csv files
Array = zeros(2592,2592);
% % R = Array(:,1);
% y = Array(:,2);
for a = 1: 2592% maxloc_row = 635 maxloc_col = 1094
[a_maxval, a_maxloc] = max(sig(:,a)); % search max among every column.
% maxloc is the row at which maxval is.
newval = a_maxval/2; % averaged max value
% New structure for find width
x = 0;
x = Array(:, 1);
for b = 1: 1944 % maxloc_row = 635 maxloc_col = 1094
% R = b;
if sig(b,a) > newval
x=x+1;
end
end % End row search
x;
% y = x*(2.2); % pixels into microns
output = [num2cell(x)];
xlswrite(newfilename, output);
end % End column search
0 Comments
Answers (4)
James Tursa
on 22 Jun 2015
Edited: James Tursa
on 22 Jun 2015
If an array has size 2592 x 2, then it is not empty. An empty array has at least one dimension that has size 0. If you mean create an array with zeros, then
Array = zeros(2592,2);
Jeremy
on 22 Jun 2015
If you are writing to excel then you probably want to use a cell array,
Array = cell(2592,2);
Image Analyst
on 22 Jun 2015
I'm surprised this even finished! You definitely don't want to call xlswrite 2592 times or it will take hours. Perhaps you opened up Excel to look at it after only a few of the calls to xlswrite had been executed.
You should create a 2D matrix for output and then put a single call to xlswrite after both "a" and "b" loops have finished.
Other problems: your (badly-named) x is just column 1 of Array every time. And you don't even need the loop over b because you can use the sum function: sum(x>newval) to count the number of pixels more than the half max value (which you chose the poor, non-descriptive name of newval for).
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!