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

4 次查看(过去 30 天)
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);

回答(1 个)

Jemima Pulipati
Jemima Pulipati 2020-11-23
编辑:Jemima Pulipati 2020-11-23
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 个评论
Stephani Kanga
Stephani Kanga 2020-11-24
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 2020-11-25
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

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by