I need to extract the RGB values of each pixel of a given image in a serial data format like R1:G1:B1, R2:G2:B2, R3:G3:B3… and so on.
MATLAB: How to extract RGB values of each pixel from an image
image processingMATLAB
Related Solutions
Sucharita: Here, I've done 95% of it for you. If I do much more, then you'd just be turning in my answer as your own and I don't think your course rules allow that. You just need to replace the line that says "% code" with your own code and then display the output image after the loop is done.
% Demo to find color distances in RGB color space. By Image Analyst, May 26, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 16;fprintf('Beginning to run %s.m ...\n', mfilename);a1 = [134, 51, 143]a2 = [131, 132, 4]R0 = 30%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = pwd;baseFileName = 'image.jpeg';fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);[rows, columns, numberOfColorChannels] = size(rgbImage);% Display the test image full size.
subplot(2, 2, 1);imshow(rgbImage, []);axis('on', 'image');caption = sprintf('Reference Image : "%s"', baseFileName);title(caption, 'FontSize', fontSize, 'Interpreter', 'None');drawnow;hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;hFig1.Units = 'Normalized';hFig1.WindowState = 'maximized';% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';[rows, columns, numberOfColorChannels] = size(rgbImage);outputImage = zeros(rows, columns, numberOfColorChannels, class(rgbImage));distanceImage = zeros(rows, columns);for col = 1 : columns for row = 1 : rows thisR = double(rgbImage(row, col, 1)); thisG = double(rgbImage(row, col, 2)); thisB = double(rgbImage(row, col, 3)); distanceImage(row, col) = sqrt((thisR - a1(1))^2 + (thisG - a1(2))^2 + (thisB - a1(3))^2); % or a2 for case ii
if distanceImage(row, col) < R0 % code...
end endendmaxDistance = max(distanceImage(:))% Display the distance image.
subplot(2, 2, 2);imshow(distanceImage, []);axis('on', 'image');caption = sprintf('Distance Image : "%s"', baseFileName);title(caption, 'FontSize', fontSize, 'Interpreter', 'None');colorbar;drawnow;hp = impixelinfo(); % Set up status line to see values when you mouse over the image.% Display the histogram of the distance image.
subplot(2, 2, 3);histogram(distanceImage, 256);grid on;title('Histogram of Color Distances', 'FontSize', fontSize, 'Interpreter', 'None');xlabel('Gray Level', 'FontSize', fontSize, 'Interpreter', 'None');ylabel('Count', 'FontSize', fontSize, 'Interpreter', 'None');fprintf('Done running %s.m ...\n', mfilename);
Henrique, see the demo code below that I wrote to produce this figure:
% Demo to have the user freehand draw an irregular shape over
% a gray scale image, have it convert that portion to a
% color RGB image defined by a colormap.
clc; % Clear command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'cameraman.tif';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendgrayImage = imread(fullFileName);imshow(grayImage, []);axis on;title('Original Grayscale Image', 'FontSize', fontSize);set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');uiwait(msgbox(message));hFH = imfreehand();% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();xy = hFH.getPosition;% Now make it smaller so we can show more images.
subplot(2, 2, 1);imshow(grayImage, []);axis on;drawnow;title('Original Grayscale Image', 'FontSize', fontSize);% Display the freehand mask.
subplot(2, 2, 2);imshow(binaryImage);axis on;title('Binary mask of the region', 'FontSize', fontSize);% Convert the grayscale image to RGB using the jet colormap.
rgbImage = ind2rgb(grayImage, jet(256));% Scale and convert from double (in the 0-1 range) to uint8.
rgbImage = uint8(255*rgbImage);% Display the RGB image.
subplot(2, 2, 3);imshow(rgbImage);axis on;title('RGB Image from Jet Colormap', 'FontSize', fontSize);% Extract the red, green, and blue channels from the color image.
redChannel = rgbImage(:, :, 1);greenChannel = rgbImage(:, :, 2);blueChannel = rgbImage(:, :, 3);% Create a new color channel images for the output.
outputImageR = grayImage;outputImageG = grayImage;outputImageB = grayImage;% Transfer the colored parts.
outputImageR(binaryImage) = redChannel(binaryImage);outputImageG(binaryImage) = greenChannel(binaryImage);outputImageB(binaryImage) = blueChannel(binaryImage);% Convert into an RGB image
outputRGBImage = cat(3, outputImageR, outputImageG, outputImageB);% Display the output RGB image.
subplot(2, 2, 4);imshow(outputRGBImage);axis on;title('Output RGB Image', 'FontSize', fontSize);
Best Answer