How do i calculate the Average of each Pixel in Origenal Image to the Average of Class to Classify each class in the Image?

5 views (last 30 days)
I hope somebody can help me.
I have calculated the Average of ROI but i need to know, How can i calculate the Average between each Pixel of (orph) Image and the Average that i already Calculated? to be able to Classify the Pixels for each Class.
The code written below
-------------------------------------------------------------------------------------------------------------------------------------------
% 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
[r, c, ~] = size(orph);
mask_building = createMask(roi_Building, r, c);
mask_street = createMask(roi_street, r, c);
mask_tree = createMask(roi_tree, r, c);
mask_Car = createMask(roi_car, r, c);
building_pixels = [op_red(mask_building), op_green(mask_building), op_blue(mask_building)];
street_pixels = [op_red(mask_street), op_green(mask_street), op_blue(mask_street)];
tree_pixels = [op_red(mask_tree), op_green(mask_tree), op_blue(mask_tree)];
building_Car = [op_red(mask_Car), op_green(mask_Car), op_blue(mask_Car)];
%Buildings
[r, c, p] = size(orph); %Row, Column and layers (size of Image)
mask3_building = repmat(createMask(roi_Building, r, c), [1, 1, 3]); %Repeat copies of array
masked_building = zeros(r, c, p, 'like', orph);
masked_building(mask3_building) = orph(mask3_building);
%Street
mask3_street = repmat(createMask(roi_street, r, c), [1, 1, 3]);
masked_street = zeros(r, c, p, 'like', orph);
masked_street(mask3_street) = orph(mask3_street);
%Tree
mask3_tree = repmat(createMask(roi_tree, r, c), [1, 1, 3]);
masked_tree = zeros(r, c, p, 'like', orph);
masked_tree(mask3_tree) = orph(mask3_tree);
%Car
mask3_car = repmat(createMask(roi_car, r, c), [1, 1, 3]);
masked_car = zeros(r, c, p, 'like', orph);
masked_car(mask3_car) = orph(mask3_car);
%Arithmetic average for each Class
AVG_Building = mean(masked_building);
AVG_Street = mean(masked_street);
AVG_Tree = mean(masked_tree);
AVG_Car = mean(masked_car);
%Average between every Pixel in Picture to average for each Class ??????!!!!!
for i= 1: 10000 :length (i)
for j = 1: 10000: length (j)
AVG_i= mean(i/AVG_Building);
AVG_j= mean(j/AVG_Building);
end
end

Accepted Answer

Image Analyst
Image Analyst on 29 Jul 2022
See my attached demo. It lets you draw an outline around each color class you want, and then it assigns a class number to each pixel in the image based on your training regions and a discriminant classifier. That sounds like what you want to do.
  3 Comments
Image Analyst
Image Analyst on 30 Jul 2022
I don't think you can do
AVG_Building = mean(masked_building);
because that will also include the black areas outside the mask. You need to do
AVG_Building = mean(masked_building(mask));
If my flexible way of asking for the number of classes and getting their names is too complicated for you then just hard code in those values.
If the code for hand tracing the training color regions is too complicated, just use whatever method you used to get your masked images.
If the loop over all possible discriminant analysis method options is too complicated, just go with one, like linear or quadratic.

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!