Select area in picture and send information about r,g,b

1 view (last 30 days)
Hello,
I need to select an area on my picture and i want to have informations about r,g,b.
Someone can help me, please ?
Thanks!

Answers (2)

KSSV
KSSV on 11 Sep 2020
If I is an image you canget:
R = I(:,:,1) ;
G = I(:,:,2) ;
B = I(:,:,3) ;
You can select your required area using imcrop, imfreehand.
  2 Comments
Abbass SAYEGH
Abbass SAYEGH on 11 Sep 2020
yes it's an image
But if for example i need to get theses information about colors, can i have save them on txt file ?
Thank you
KSSV
KSSV on 11 Sep 2020
Yes..you can write the data into text file. Read about fprintf, saveas, writematrix.

Sign in to comment.


Image Analyst
Image Analyst on 11 Sep 2020
What information do you need? For example, you can mask it and get the means and area. See attached demo.
You can get the histogram.
No idea what you want really. See my File Exchange for some ideas.
  29 Comments
Walter Roberson
Walter Roberson on 24 Sep 2020
Moved: DGM on 23 Feb 2023
my Image is a N-by-3 list of (x,y,graylevel)
Is the file "complete", in the sense that every possible x and y combination appears? Or are some not listed -- for example are the locations that are zero not listed ? Are the x and y regularly spaced? Are they positive integers that start from 1 ? Should it be considered scattered information, samples of the actual surface with the other locations on the grid to be interpolated based upon the points that are present?
  • Browse my folder and find all my xlsx files
We gave you code for that.
  • Open these xlsx files like image
readtable() or readmatrix() or xlsread(). But then you have to arrange the list of data into an image, and to do that we need to know the answers to the questions I asked above.
  • When i opened them, i want to select an area
A fixed area? If so then imcrop() or index the image. A variable area? If so then imfreehand(). Image Analyst showed you how to do that in https://www.mathworks.com/matlabcentral/answers/592150-select-area-in-picture-and-send-information-about-r-g-b#comment_1005523 and also attached a demonstration program in one of his other answers. Or use https://www.mathworks.com/help/images/ref/roipoly.html roipoly()
  • when i select this area, i want to put the values of pixels (r,g,b) on new xlsx file
If you used imfreehand() then use createmask https://www.mathworks.com/help/images/ref/imroi.createmask.html to get a binary selection matrix. If you used roipoly() then it returns a binary selection matrix directly. If you somehow have a list of coordinates that bound the selection region then poly2mask() https://www.mathworks.com/help/images/ref/poly2mask.html
Now use find() with two outputs on the mask to get a list of rows and column numbers where the mask is set. The row numbers are your y coordinates. The column numbers are your x coordinates. Do not make the mistake of using the rows as your x coordinates!!
Now use the logical mask to index your grayscale image. The result will be a vector of image values, in the same order that find() returned its values.
Put the x, y, and values together into an N x 3 array.
Now writematrix() or writetable() or xlswrite() the data out.
  • Calculate the mean of these values and put it on new xlsx file
mean() the vector of values referred to above. writematrix() or writetable() or writecell() or xlswrite() the data out.
  • Do the same process for the rest of the files
We gave you the code for that.
If you are wanting to put the mean values all in the same xlsx file, then I recommend that you do not write the mean values into a file inside the loop. I recommend that you instead record the values in arrays, and then do a single write at the end. Keeping track of the current position inside an xlsx file to be able to write at the "end" of it is a nuisance best avoided (and writing only once would be considerably more efficient.)
Can I have help for my question ? or i cant ?
Ummm.. you know we are volunteers, and that we have other things going on in our lives, like answering other questions, or sleeping, or our jobs, or relaxing because we are worn out from answering lots of questions? The majority of the active volunteers are in North America, and your post about the file format and your prodding for additional assistance were during the middle of the night in North America.
Image Analyst
Image Analyst on 25 Sep 2020
Moved: DGM on 23 Feb 2023
I'll ask you the same question you asked us "Can I have help for my question ? or i cant ?"
I specifically and directly asked you to attach one of your Excel workbooks, and instead of simply attaching one, you said "It's like other excel file." Come on - make it easy for us to help you not hard! You're basically saying to me "I don't want to spend a few seconds to attach my workbook but I expect you to write code to read in some image, then write out the x,y,grayLevel values, in order to create some sample image." Why are you wanting me to go through all that? No, sorry but I won't. If you won't spend the time to attach it and make it easy for us to help you, then I won't spend my time either.
We can't just write the whole thing for you, but there are people who would gladly do so. They are here. Please contact them and they'd be happy to do it all for you and deliver a turnkey system, or train you throughout the process while you do it.
We non-Mathworks volunteers generally provide some help if we can do it within a few minutes (which we've already spent). If it would be more than that, we can provide code snippets or refer you to the FAQ or other places (File Exchange) for code, which we've done. Try to make an effort with the code snippets you now know about and make a stab at creating your program. I mean the code in the FAQ is basically half the program you'd need. Here, I adapted the FAQ code a little for you to give you a start.
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.xls*'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = xlsread(fullFileName);
% Find size in the last row. Form is [x, y, grayLevel]
columns = imageArray(end, 1); % Max X
rows = imageArray(end, 2); % Max Y
grayLevels = imageArray(:, 3);
% Reshape into rectangle:
imageArray = reshape(imageArray, rows, columns);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
% Call roipoly() or drawfreehand() to get a mask
% I'm sure you can figure this out.
% Get row, column for pixels in the mask
[r, c] = find(mask);
% Make new array
output = zeros(length(r), 3);
for kk = 1 : length(r)
output(kk, 1) = c(kk);
output(kk, 2) = r(kk);
output(kk, 3) = imageArray(r(kk), c(kk), kk);
end
% Create Excel filename by appending _output
excelFullFileName = strrep(lower(fullFileName), '.xls', '_output.xls');
writematrix(output, excelFullFileName);
end
Why not try it? Good luck.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!