Save pixel color values of all the pixels of an image

7 ビュー (過去 30 日間)
Ouael Chkoundali
Ouael Chkoundali 2019 年 1 月 14 日
コメント済み: Ouael Chkoundali 2019 年 1 月 15 日
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 件のコメント
Jan
Jan 2019 年 1 月 15 日
Please post the complete error message, such that we do not have to guess, where the problem occurs.

サインインしてコメントする。

採用された回答

Jan
Jan 2019 年 1 月 15 日
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".

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by