How can i create a table that will contains the pixel values that i get from impixel()?

4 views (last 30 days)
I want to display the values from impixel to a uitable but, as the user is going to choose the points, the values are not constant so i can't initialize the table's data. How can i read the values from impixel and then put it in the table(RGB) along with two other columns (Name, Color)?
P = impixel() ;
Table = uitable(Tab1,'Units','normalized','Position',[.0 .1 1 .6]);
Table.ColumnName = {'Name','RGB','Color'};
Table.ColumnEditable = [true false false];
Table.RowName = 'numbered';
[numSelections,numCols] = size(P);
for i = 1 :numSelections-1
cell(i).name ='Label '+i;
cell(i).rgb = P(i,1);
cell(i).color = 'Color'+ i ;
end
Table.Data = cell2table(cell);

Answers (1)

Jemima Pulipati
Jemima Pulipati on 23 Nov 2020
Edited: Jemima Pulipati on 23 Nov 2020
Hello,
The impixel() accepts the image as an argument and returns the values of the pixels selected interactively on the image.
image = imread('peppers.png');
P = impixel(image) ;
The points on the image can be selected using normal button clicks but the last point has to be selected using 'Shift - click'.
Here is the modified code using the impixel() and storing the data in a uitable.
image = imread('peppers.png');
P = impixel(image) ;
% Specifying a parent container for uitable
f = uifigure;
Table = uitable(f,'Units','normalized','Position',[.0 .1 1 .6]);
Table.ColumnName = {'Name','RGB','Color'};
Table.ColumnEditable = [true false false];
Table.RowName = 'numbered';
[numSelections,numCols] = size(P);
% Pre allocating the array for speed
cellArray = cell(numSelections-1,3);
for i = 1 :numSelections-1
% storing the values in a cell array
cellArray(i,:) = {"Label"+num2str(i), P(i,1), "Color"+ num2str(i)};
end
Table.Data = cell2table(cellArray);
The output of this code is
NOTE: The number of values displayed in the table depend on the number of iterations, the loop runs. Since, you are running the loop till 'numSelections - 1' the last point selected on the image will not be displayed in the table. To get all the values selected interactively, you may use
for i = 1 :numSelections
  2 Comments
Stephani Kanga
Stephani Kanga on 24 Nov 2020
Hello, thanks for your answer but it doesn't work because I use figure instead of uifigure. Is there a way to have a uitable in a figure?
Jemima Pulipati
Jemima Pulipati on 25 Nov 2020
Hello,
A uitable can accept a 'figure' as a parent container.
Here is the modified code using a 'figure'
image = imread('peppers.png');
P = impixel(image) ;
% Specifying a parent container for uitable
f = figure;
Table = uitable(f,'Units','normalized','Position',[.0 .1 1 .6]);
Table.ColumnName = {'Name','RGB','Color'};
Table.ColumnEditable = [true false false];
Table.RowName = 'numbered';
[numSelections,numCols] = size(P);
% Pre allocating the array for speed
cellArray = cell(numSelections-1,3);
for i = 1 :numSelections-1
% storing the values in a cell array
% converting the string to a char while storing in a cell array
cellArray(i,:) = {char("Label "+num2str(i)), P(i,1), char("Color "+num2str(i))};
end
Table.Data = cellArray;
NOTE: Clearing the workspace would help incase of any errors while running the code

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!