필터 지우기
필터 지우기

Save pixel color values of all the pixels of an image

조회 수: 4 (최근 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 CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by