Does anybody Know how I can get from image 1 to image 2 and find the area f each element (as we have in image 2) in Matlab, knowing that the total area of the image one is 61*89 cm?
Hello Mojtaba, you have to first convert the image into black and whtie using the inbuilt function in matlab "im2bw", you will then get your desired second image. Regarding the area, the total area occupied by all the white regions in image 2 can be computed using the function "bwarea", whereas for computing individual areas, you will have to use image segmentation. One such example is given in:
You will find excellent answers to MATLAB image analysis questions over on the MATHWORKS forums.
Here my suggestion for you. For your image, pre-processing and segmentation can be carried out by a simple threshold. This will not always be the case, but the distinction between the area of interest (leaves) and the background is quite clear. Check the histrogram of the image to confirm this.
The next step is analysis of the shapes. In MATLAB, 'bwconncomp' is a useful function for extracting data about binary shapes in images. Last, convert the 'pixel size' to 'metric size', which you can see at the end of the code I have provided.
Here is some annotated code I have used recently in MATLAB. I modified it for the data your provided, but I have not tested on your image.
% I = your image
% thr = your fractional threshold
BW = im2bw(I,thr);
CC = bwconncomp(BW);
CC2 = regionprops(CC,'Area');
% Should get...
% CC2 =
% 125x1 struct array with fields:
% Area
% Regarding conversion of area: this is a direct
% conversion from 'pixels' to 'cm.' You have the
% metric dimensions, but you need to get the
% 'pixel size' of the image:
dim = size(I); % image size in PX
met = [61 89]; % image size in CM
cm_px = mean(met./dim); % metric dimension of pixel. assuming accurate %measurements, the average of the two values should suffice for now.
A_px = cm_px^2; % area of a pixel, CM^2
shape_area = cat(1,CC2.Area)*A_px; % area of each shape in CM^2
total_area = sum(shape_area); % area occupied by shapes