clc; clear; img = imread('10.png'); %import the image Size = size(img); %get the size of the image temp = uint8(ones(Size).*255); %create a temp image, same size with original image %%%%%%%%%%%%%%%%%%%%%%%%% %The threshold to separate the leaf from the background %%%%%%%%%%%%%%%%%%%%%%%%% GreenThreshold = 0; %set the green threshold in RGB HSV2Thresholdmin = 10; %Set the minimum threshold of H channel in HSV HSV2Thresholdmax = 99; %Set the maximum threshold of H channel in HSV VThresholdmin = 46; %Set the minimum threshold of V channel in HSV SThresholdmin = 43; %Set the minimum threshold of S channel in HSV %dissociate RGB values, the green channel value of the leaf will be relatively large, so anything less than the threshold value is not a leaf for I=1:Size(1) for J=1:Size(2) if img(I,J,2)>GreenThreshold temp(I,J,1) = img(I,J,1); temp(I,J,2) = img(I,J,2); temp(I,J,3) = img(I,J,3); end end end HSV = rgb2hsv(temp); %Convert RGB gamut to HSV gamut HSV(:,:,1) = HSV(:,:,1).*180; %All the values converted by matlab are [0,1], so multiply by the corresponding coefficient to restore the real value HSV(:,:,2) = HSV(:,:,2).*255; HSV(:,:,3) = HSV(:,:,3).*255; LeafArea = 0; %Define variables that whole leaf area TotalArea = Size(1)*Size(2); %The area of the entire image %%%%%%%%%%%%%%%%%%%%%%%%%% %On the basis of RGB separation of the leaves, HSV is used to separate them again to ensure the purity of the background and to calculate the size of the leaves %%%%%%%%%%%%%%%%%%%%%%%%% for I=1:Size(1) for J=1:Size(2) if HSV(I,J,1)>HSV2Thresholdmin&&HSV(I,J,1)SThresholdmin&&HSV(I,J,3)>VThresholdmin %In this range are leaves LeafArea = LeafArea+1; else %Otherwise the background will be white temp(I,J,1) = 255; temp(I,J,2) = 255; temp(I,J,3) = 255; end end end TempletSize = round(LeafArea.*0.005); %Calculate the area of 10% imshow(temp); %%%%%%%%%%%%%%%%%%%%%%%%% %Mouse position acquisition %%%%%%%%%%%%%%%%%%%%%%%%% k = waitforbuttonpress; % Wait for the mouse to press point1 = get(gca,'CurrentPoint'); % The mouse was clicked finalRect = rbbox; point2 = get(gca,'CurrentPoint'); % Loosen the mouse point1 = point1(1,1:2); % Extract two points point2 = point2(1,1:2); p1 = min(floor(point1),floor(point2)); % Calculate of position p2 = max(floor(point1),floor(point2)); %%%%%%%%%%%%%%%%%%%%%%%%%%% %Define 10% %%%%%%%%%%%%%%%%%%%%%%%%%%% W = sqrt(TempletSize); H = sqrt(TempletSize); if abs(p2(2)-p1(2))*abs(p2(1)-p1(1))TempletSize %If the area taken from the box is inconsistent with 10%, it shall be adjusted p2(2) = round(p2(2)+W-abs(p2(2)-p1(2))); p2(1) = round(p2(1)+H-abs(p2(1)-p1(1))); end Templet = uint8(ones(abs(p2(2)-p1(2)),abs(p2(1)-p1(1)),3)*255); %Create template image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Saves the area taken from the box to the template image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for I=1:abs(p2(2)-p1(2)) for J = 1:abs(p2(1)-p1(1)) Templet(I,J,1) = temp(p1(2)+I-1,p1(1)+J-1,1); Templet(I,J,2) = temp(p1(2)+I-1,p1(1)+J-1,2); Templet(I,J,3) = temp(p1(2)+I-1,p1(1)+J-1,3); end end TempletHsv = rgb2hsv(Templet); %Convert the template to an HSV gamut THSize = size(TempletHsv); %Calculate the area of the template THSizeAll = THSize(1)*THSize(2); SH = (sum(sum(TempletHsv(:,:,1)))/THSizeAll)*180; %Statistics the average HSV gamut of the template SS = (sum(sum(TempletHsv(:,:,2)))/THSizeAll)*255; SV = (sum(sum(TempletHsv(:,:,3)))/THSizeAll)*255; GreenHsvThreshold = uint8(SH.*0.8); %Set an 80% threshold for average green HSV = rgb2hsv(temp); %Convert the image to HSV HSV(:,:,1) = HSV(:,:,1)*180; out = temp; YellowTotal = 0; %Create a variable for the yellow area for I=1:Size(1) for J=1:Size(2) if HSV(I,J,1)11 %Yellow leaf area calculation and marking out(I,J,1) = 255; out(I,J,2) = 0; out(I,J,3) = 0; YellowTotal = YellowTotal+1; end end end imshow(temp); figure imshow(out); W = (YellowTotal/LeafArea)*100; Text = ['Etiolation rate(W):',num2str(W),'%']; text(10,10,Text,'horiz','left','color','r') %Mark the Etiolation rate In the Image