A list,
1 Overview 1.1 Basic Ideas Use structural elements with certain forms to measure and extract the corresponding shapes in the image, so as to achieve the purpose of image analysis and recognition
1.2 Basic operations Expansion, corrosion, open operation, close operation
1.3 Fundamentals of MathematicsSet theoryStructural elements:The original image needs to be expanded so that the extension can cover the entire structure element when it is at the edge of the original image.
2 basic operation of binary image morphology 2.1 Corrosion operationAll the structural elements B are located in the corresponding displacement z in A, so the boundary will be weakenedEffect:Is a shrinking or thinning operation.
2.1 Expansion Operation Structural element B and A have an intersection corresponding displacement z, so the boundary will be expandedEffect:Is a growth or coarsening operation. Note: Corrosion and expansion are dual (i.e. the inverse of foreground corrosion = the result of background expansion) 2.3 open operationCorrosion first and then expansion, smooth the outline of the object, break the narrow strip, eliminate the thin protrusion 2.4 closed operationExpansion followed by corrosion also smoothes the contours of the object, but Bridges narrower gaps and elongated stripsEffect: The outward Angle remains the same, the inward Angle is rounded, fill the gap – “add” 2.5 Comparison between open operation and closed operation2.5.1 Duality (Negative result after closed operation = Open operation on background)2.5.2 There is no change after the property operator is applied once
2.5.3 Comparison effect2.5.4 Use open operation to take out small targets in the image (denoising) Close operation to connect targets (eliminating small intervals)
2.6 Hit cannot transformPurpose: Shape detection (detecting the position of a particular shape and requiring each object to be surrounded by a background at least one pixel wide) matches foreground and background together 3. Binary image morphology algorithm 3.1 Boundary extraction Foreground – Corroded foreground
3.2 Hole filling Find A starting point (located in the hole), keep expanding and inverted A, until there is no change
3.3 Extraction of unicom components The whole linkage pattern can be obtained from a known point of the linkage pattern through the above iterative formula
3.4 convex hullConvex: the line connecting any two points in set A is in A, then set A is A convex shell: the convex shell H of any set S is the minimum convex set contained in SWhere X0 is A, Bi is A structural element in different directions, A Bi can make A Di, and the convex shell of A is all Di intersects Results: 3.5 elaborationA minus the pixel that A and structure element B hit and miss transform matchWhere B is the sequence of structural elements Procedure: A is refined sequentially by B1~Bn, and then returns to B1, and refined sequentially from B1 until convergence 3.6 coarseningThe refined morphologic dual A and make the sum of the pixels matched by the hit miss transform between A and structure element B 3.7 the skeletonThe skeleton of A can be expressed by corrosion and open operationWhere k represents k times of corrosion on A, and K is the last iteration before A is corroded into A null set 3.8 cutIs a complement to thinning and skeleton drawing algorithms, because these processes leave the add-ons clean and need pruning to remove parasitic excess
4. Gray scale morphology 4.1 operatingEtch: The output image becomes dark and the light details are reduced4.2 the effect Open operation: remove small bright details close operation: remove small dark details
4.3 Algorithm Image smoothing: open first (suppress bright details) and then close (suppress dark details) Morphological gradient: expansion-corrosion top hat transformation: correct uneven illumination and enhance shadow details
Ii. Source code
clear; clc; close all; % % % % % % % % % % % % % % % % % to read the original image, and processed into binary image % % % % % % % % % % % % % % % % % % % % % % % % I = imread ('pear 2. JPG');
%I=imread('peach 2. JPG');
%I=imread('apple 2. JPG');
%I=imread('bananas. JPG');
%I=imread('green peppers. JPG');
I=imread('test4.jpg');
I2=rgb2gray(I);
BW=im2bw(I2,0.9);
figure(1),subplot(1.3.1),imshow(I),title('Original image');
subplot(1.3.2),imshow(I2),title('Grayscale image');
subplot(1.3.3),imshow(BW),title('Binary image'); % % % % % % % % % % % % % % % % edge detection by using discrete graphics boundary % % % % % % % % % % % % % % % % % % % % % % % to get the graphical SE = strel (continuous border'rectangle'[40 30]); % structure definition J2=imopen(BW,SE); % open operation figure(2),imshow(J2),title('Open operation on binary image');
SE=strel('square'.5); J=imerode(~J2,SE); BW2=(~J2)-J; % edge figure(3),imshow(BW2),title('Edge detection'); % fills the continuous shape boundary of the existing detection B = imfill(BW2,'holes'); % image fill B = bwmorph(B,'remove'); % Remove internal pixel figure(4),imshow(B),title('Extracted boundary image'); [Label,num] = bwLabel (B,8); % % % % % % % % % % % % % % % % % % % to calculate each graphic unit boundary pixel points % % % % % % % % % % % % % % % % % % % % % % % % % % %for i = 1 : num
Premeter(i) = 0;
end
[row,col] = size(Label);
for i = 1 : row
for j = 1 : col
if(Label(i,j) > 0)
Premeter(Label(i,j)) = Premeter(Label(i,j)) + 1; % calculation after mark the total number of the number of pixels in each piece of graphic border end end end % % % % % % % % % % % % % % % % % % % calculated each graphic unit area % % % % % % % % % % % % % % % % % % % % % % % % % % % % % FilledLabel = imfill(Label,'holes'); Figure,imshow(FilledLabel),title(FilledLabel)'Marked and populated results');
for i = 1 : num
Area(i) = 0;
end
[row,col] = size(FilledLabel);
for i = 1 : row
for j = 1 : col
if(FilledLabel(i,j) > 0)
Area(FilledLabel(i,j)) = Area(FilledLabel(i,j)) + 1; % by means of statistical number of pixels to find the area of each shape end end end % % % % % % % % % % % % % % % % % % to calculate roundness of the graphical units % % % % % % % % % % % % % % % % % % % % % % % % % %for i = 1 : num
Ecllipseratio(i) = 4*pi*Area(i)/Premeter(i)^2; End % % % % % % % % % % % % % % % % % % % % calculation of each image chroma % % % % % % % % % % % % % % % % % % % % % HSV = rgb2hsv (I); % to HSV in preparation for extraction of chromaticity elements [row,col] = size(FilledLabel); % Count the number of pixels in each block of the filled graph. MeanHue = zeros(1,num);
for i = 1 : num
Hue = zeros(Area(i),1);
nPoint = 0;
for j = 1 : row
for k = 1 : col
if(FilledLabel(j,k) == i)
nPoint = nPoint + 1;
Hue(nPoint,1) = HSV(j,k,1);
end
end
end
Hue(:,i) = sort(Hue(:,1));
for j = floor(nPoint*0.1) : floor(nPoint*0.9)
MeanHue(i) = MeanHue(i) + Hue(j,1);
end
MeanHue(i) = MeanHue(i) / (0.8*nPoint); % to calculate the average chroma value end % % % % % % % % % % % % % % % % % % % % % % % % a variety of fruits to identify % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % recognition pears, in 2 d feature space to the distinction between the various image category pear =0; result1=0;
for i=1:num
if(MeanHue(i)<0.125) % judge the average chroma value in each graph is less than0.125For pear pear = I; result1=1; End end % Build the corresponding image mask for the separated pears, and mask the brightness image of the original image.for j = 1 : row
for k = 1 : col
if(FilledLabel(j,k) ~=pear)
pearHSV(j,k,3) =0; End end % transform to generate the final result of the image Pearmatrix = hSV2RGB (pearHSV); % to RGB color map, color map has filtered out the rest of the fruit, only pearsif(result1==1) figure,imshow(pearmatrix); title('It's a pear.'); End %%%%%%%%% identify peach and classify each image in two-dimensional feature space pitch=0; result2=0;
for i=1:num
if(MeanHue(i)>0.5) % judge that the average chroma value in each graph is greater than0.5For the peach pitch = I; result2=1;
end
end
Copy the code
3. Operation results
Fourth, note
Version: 2014 a