Save pixel color values of all the pixels of an image
2 views (last 30 days)
Show older comments
Ouael Chkoundali
on 14 Jan 2019
Commented: Ouael Chkoundali
on 15 Jan 2019
Hello,
I am trying to save the pixel color values of an image in a matrix after uploading it on GUI.
First I choose the image with the browser button. I wrote a function, that firtst converts the image in gray and then saves using a for loop all pixel values in the Matrix pixelValues.
I receive the error "Index exceeds matrix dimensions."
Here is my code for the browser button
% --- Executes on button press in browser.
function browser_Callback(hObject, eventdata, handles)
% hObject handle to browser (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[a b] = uigetfile({'*.jpg';'*.png';'*.bmp'},'File Selector');
image = imread([b a]);
axes(handles.axes)
imshow(image)
axis on
yLimits = get(gca,'YLim'); %# Get the y axis limits
yTicks = yLimits(2)-get(gca,'YTick'); %# Get the y axis tick values and
%# subtract them from the upper limit
set(gca,'YTickLabel',num2str(yTicks.')); %'# Convert the tick values to strings
%# and update the y axis labels
A = savePixelValues(image);
And here is the function :
function [pixelValues] = savePixelValues(image)
[x,y,z] = size(image);
I = rgb2gray(image);
v = zeros(1,3);
A = zeros(y,x);
for i=1:y
for j=1:x
v = squeeze(I(i,j,:));
pixelValues(i,j) = v(1);
end
end
end
Can someone help me please?
1 Comment
Jan
on 15 Jan 2019
Please post the complete error message, such that we do not have to guess, where the problem occurs.
Accepted Answer
Jan
on 15 Jan 2019
Some hints:
% Use meaningful names for variables. "[a b]" is confusing:
[fileName, filePath] = uigetfile({'*.jpg';'*.png';'*.bmp'},'File Selector');
% FULLFILE is smarter than [filePath fileName]:
img = imread(fullfile(filePath, fileName));
% "image" is an important command, do not shadow it by a variable.
% It is safer to provide the parent than to rely on the
% current axes by: axes(handles.axes)
imshow(img, 'Parent', handles.axes);
axis(handles.axes, 'on');
yLimits = get(handles.axes,'YLim'); % If you have the handle already, ...
% requesting GCA wastes time only.
% Puh, Matlab is not twitter. No hash characters before comments.
% %# Get the y ax
yTicks = yLimits(2)-get(handles.acers,'YTick');
set(handles.axes,'YTickLabel',num2str(yTicks.'));
A = savePixelValues(img);
Your code:
[x,y,z] = size(image);
I = rgb2gray(image);
v = zeros(1,3);
A = zeros(y,x);
for i=1:y
for j=1:x
v = squeeze(I(i,j,:));
pixelValues(i,j) = v(1);
end
end
could be simplified to the single line:
pixelValues = I(:,:,1);
Pre-allocating v is a waste of time, because you re-create the array in each iteration. A is not used anywhere. But most of all, I is a 2D matrix already after rgb2gray. So you can can be written as:
function pixelValues = savePixelValues(rgb)
pixelValues = rgb2gray(rgb);
end
I assume you mean the gray value as "pixelValue".
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!