How can I know the value of pixel "ROI" in each (RGB) Channel?

6 views (last 30 days)
I hope someone can help me out.
I have 4 ROI Buildings, Cars, Street and Trees and each one has defined but what i need to know, How can i get the Values of pixels of ROI in each channel in RGB image (Red, Green and Blue)?
Please tell me exactly, the code, that i should write!
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% CleanUp
clc;
clear;
close all;
%Read Photo RGB and DSM
orph = imread('BE_ORTHO_27032011_315145_56865.tif');
figure, imshow (orph,[]);
%Define Region of interest
roi_Building = drawpolygon('Color','r');
roi_Street = drawline('Color','y');
roi_Tree = drawfreehand('Color','g');
roi_car = drawpolygon('Color','b');
%Seperate channels RGB
op_red= orph(:,:,1); % Red channel
op_green= orph(:,:,2); % Green channel
op_blue= orph(:,:,3); % Blue channel
%Value of pixels in all layers

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jul 2022
[r, c, p] = size(orph);
mask_building = createMask(roi_Building, r, c);
building_pixels = [op_red(mask_building), op_green(mask_building), op_blue(mask_building)];
This would be N x 3 where N is the number of selected pixels, and the columns are R, G, B.
But I wonder if what you would prefer would instead be
[r, c, p] = size(orph);
mask3_building = repmat(createMask(roi_Building, r, c), [1, 1, 3]);
masked_building = zeros(r, c, p, 'like', orph);
masked_building(mask3_building) = orph(mask3_building);
which would create a new imae masked_building in which the pixels that are not inside the ROI are all set to 0 (black)
  8 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!