Fuzzy recognition problem of triangles

Problem background

Causes of fuzzy recognition of triangles:

In ancient times, people needed to study the properties and characteristics of triangles in order to measure land, calculate angles, and solve other practical problems. Nowadays, with the development of science and technology, triangle, as a basic geometric shape, is widely used in many fields such as mathematics, physics, engineering and so on. The triangle recognition problem arises mainly due to the needs of practical applications.

Application of triangle fuzzy recognition:

With the development of computer technology, triangle recognition problems have also been widely used in computer graphics, computer vision, robot navigation and other scientific and technological fields. For example, in computer vision, it can be used to describe and identify objects in images, such as face recognition…

Triangle fuzzy recognition also has many applications in other professional fields. For example, in medical diagnosis, it can be used to evaluate the severity of symptoms (identification of the degree of cell canceration) and help doctors determine the diagnosis; in legal judgments, it can be used to evaluate the reliability and credibility of evidence…

Here is an introduction to the further extension of triangle fuzzy recognition from face recognition to facial emotion recognition: Imagine that we have an image of a human face. There are eyes, mouth, nose and other parts on this image. Different combinations of their shapes, sizes, and positions constitute various expressions of the human face. For example, when people are happy, their eyes will smile into slits, their mouths will open wide, and their noses will turn up. And when people are sad, their eyes may become moist, the corners of their mouths may droop, and their noses may wrinkle.

The triangle fuzzy recognition algorithm can help us extract these features from this image. It treats the eyes, mouth, nose and other parts as small triangular areas, and then analyzes the shape, size, position and other information of these areas. For example, if the size and position of the mouth area tells us that the mouth is open, then this may mean that the person is smiling.

Then, based on these features, the triangle fuzzy recognition algorithm can “learn” the feature combinations corresponding to different expressions. For example, it may “remember” the shape and position of its eyes, mouth, and nose when smiling. When it sees a new picture, it can use these features to determine whether the person in the picture is laughing or crying, and what their mood is.

Challenges faced by triangle fuzzy recognition:

1. Establish an effective membership function:

Designing effective membership functions to describe the similarities and differences between triangles requires careful consideration of the different types and characteristics of triangles and the context of their application.

2. Design an effective multi-classification algorithm

The triangle fuzzy recognition problem is actually a multi-classification problem, but the boundaries between different types of triangles may not be obvious, so effective multi-classification algorithms need to be designed to handle this fuzziness and uncertainty.

3. Anti-interference in actual use

Noise and interference such as measurement errors, incomplete data, etc. may affect the calculation and judgment of membership functions. Therefore, effective denoising and anti-interference methods need to be studied to improve recognition accuracy and reliability.

4. Improve calculation efficiency

Since triangle fuzzy recognition involves a large amount of calculations and comparisons, attention needs to be paid to how to improve calculation efficiency and reduce calculation time and resource consumption.

Simple pattern recognition method (traditional method)

Restatement of the problem:

Divide triangles into 5 categories and establish 5 patterns: isosceles triangle I, right triangle R, equilateral triangle E, isosceles right triangle IR and atypical triangle O. Identify unknown types of triangles and determine which type they belong to?

Advantages of this method:

Simplicity: The definition of membership function in traditional methods is simple and easy to understand, and the amount of calculation is small.

Ease of use: The approximate fuzzy pattern of the triangle can be directly obtained through the principle of maximum membership.

Disadvantages of this method:

Low recognition accuracy: When the membership degrees of multiple triangles are similar or overlapping, the maximum membership degree principle cannot clearly distinguish them, which can easily lead to recognition errors.

Triangle fuzzy identification method with exponential membership function

Advantages:

The classification effect is obvious: compared with the traditional membership function, the membership degrees obtained by the exponential method are more obviously different from each other, have a larger gap, and are easier to identify.

More reasonable identification: use a membership function with a threshold to ensure that the identified type is above the specified qualified indicator factor, avoid neglecting the secondary membership degree, and ensure its rationality.

Disadvantages:

The calculation is complex and the accuracy is insufficient: Compared with the traditional membership function, the exponential membership function requires a larger amount of calculation and is more difficult to calculate. There is also the problem of false membership.

The recognition effect of isosceles right triangles is not good: when identifying isosceles right triangles, the isosceles triangle membership degree and the right triangle membership degree are minimized, and the isosceles right triangle cannot be clearly identified.

Conclusion

The index method does not directly classify triangles according to the principle of maximum membership, but sets a threshold for comprehensive consideration. However, the process of determining the threshold may be subjective, and a method needs to be found to comprehensively analyze the properties and characteristics of triangles to determine the appropriate threshold.

Prospects of triangle fuzzy recognition:

With the development of computer technology and the widespread application of triangle fuzzy recognition, we believe that triangle fuzzy recognition methods will be further explored and researched, and issues such as membership function accuracy, noise in application, and computational efficiency will be solved, which will promote triangle fuzzy recognition. Wider application and development of fuzzy recognition.

Code

MATLAB code: (The code with the variable threshold part should be wrong and needs to be improved)

clear
clc
disp('Triangle fuzzy recognition')
disp('Please enter any two of the three angles of the triangle:');
x = input('Enter the first angle x = ');
y = input('Enter the second angle y = ');
z = 180 - x - y;

% Use the sort function to arrange x, y, z in order from large to small
sorted_angles = sort([x, y, z], 'descend');
x = sorted_angles(1); % The maximum value is x
y = sorted_angles(2); % The middle value is y
z = sorted_angles(3); % The minimum value is z

% Show results
disp(['x = ', num2str(x)]);
disp(['y = ', num2str(y)]);
disp(['z = ', num2str(z)]);

if x + y + z~=180
    error('The sum of the three angles is not 180°, please re-enter!')
end
if x<y||x<z||y<z
    error('The three angles are not strictly sorted from largest to smallest, please re-enter!')
end
if x>=180||z<=0
    error('The entered interior angles do not match, please re-enter!')
end

%Traditional triangle fuzzy recognition membership function
E=1-(1/180) * max(x-y,y-z);%equilateral triangle
I=1-(1/60) * min(x-y,y-z);% isosceles triangle
R=1-(1/90) * abs(x-90);%right triangle
IR=min(1-(1/60)*min(x-y,y-z),1-(1/90)*abs(x-90));% isosceles right triangle
O=min(min((1/60)*min(x-y,y-z),(1/90)*abs(x-90)),(1/180)*max(x-y,y-z));% atypical triangle
max1=0;
% Determine triangle type
if max1<I
    max1=I;
    F1='Recognition result: This is an isosceles triangle!';
end
if max1<E
    max1=E;
    F1='Recognition result: This is an equilateral triangle!';
end
if max1<R
    max1=R;
    F1='Recognition result: This is a right triangle!';
end
if max1<IR
    max1=IR;
    F1='Recognition result: This is an isosceles right triangle!';
end
if max1<O
    max1=O;
    F1='Recognition result: This is an atypical triangle!';
end
disp('Traditional triangle fuzzy recognition:');
disp(F1);
disp('The membership degrees are:');
disp(' Equilateral triangle Isosceles triangle Right-angled triangle Isosceles right-angled triangle Atypical triangle');
disp([E,I,R,IR,O]);

%Exponential triangle fuzzy recognition membership function
Ez=(1-((x-z)/180))^(x-z);%equilateral triangle
Iz=(1-(1/60)*min(x-y,y-z))^(min(x-y,y-z)^(1/2));% isosceles triangle
Rz=(1-(1/90)*abs(x-90))^abs(x-90);% right triangle
IRz=min(Iz,Rz);%Isosceles right triangle
Oz=min(min(1-Ez,1-Iz),1-Rz);% atypical triangle
max2=0;
% Determine triangle type
if max2<Iz
    max2=Iz;
    F2='Recognition result: This is an isosceles triangle!';
end
if max2<Ez
    max2=Ez;
    F2='Recognition result: This is an equilateral triangle!';
end
if max2<Rz
    max2=Rz;
    F2='Recognition result: This is a right triangle!';
end
if max2<IRz
    max2=IRz;
    F2='Recognition result: This is an isosceles right triangle!';
end
if max2<Oz
    max2=Oz;
    F2='Recognition result: This is an atypical triangle!';
end
disp('Exponential triangle fuzzy recognition:');
disp(F2);
disp('The membership degrees are:');
disp(' Equilateral triangle Isosceles triangle Right-angled triangle Isosceles right-angled triangle Atypical triangle');
disp([Ez,Iz,Rz,IRz,Oz]);

%Triangle recognition method based on variable threshold
Ex=(1-((x-z)/180))^(x-z);%equilateral triangle
Ix=(1-(1/60)*min(x-y,y-z))^(min(x-y,y-z)^(1/2));% isosceles triangle
Rx=(1-(1/90)*abs(x-90))^abs(x-90);%right triangle
IRx=min(min(Ix,Rx),(z/45)^z);% Isosceles right triangle
Ox=min(min(1-Ex,1-Ix),1-Rx);% atypical triangle
max3=0;
% Determine triangle type
if max3<Ix
    max3=Ix;
    F3='Recognition result: This is an isosceles triangle!';
end
if max3<Ex
    max3=Ex;
    F3='Recognition result: This is an equilateral triangle!';
end
if max3<Rx
    max3=Rx;
    F3='Recognition result: This is a right triangle!';
end
if max3<IRx
    max3=IRx;
    F3='Recognition result: This is an isosceles right triangle!';
end
if max3<Ox
    max3=Ox;
    F3='Recognition result: This is an atypical triangle!';
end
disp('Triangle recognition based on variable threshold:');
disp(F3);
disp('The membership degrees are:');
disp(' Equilateral triangle Isosceles triangle Right-angled triangle Isosceles right-angled triangle Atypical triangle');
disp([Ex,Ix,Rx,IRx,Ox]);

%Determine the fluctuation of the threshold
e=0.3;
Ey=(1-2*e/60)^(2*e); %equilateral triangle
Iy=(1-e/90)^e; %Isosceles triangle
Ry=(1-2*e/180)^(2*e); %right triangle
IRy=min(min(Iy,Ry),(z/45)^z); %Isosceles right triangle
disp('Threshold for triangle fuzzy recognition:');
disp('Isosceles triangle Isosceles triangle Right triangle Isosceles right triangle');
disp([Ey,Iy,Ry,IRy]);

Python code:

import random
import pandas as pd

# Set Pandas options to display the complete table
pd.set_option("display.max_rows", None)
pd.set_option("display.max_columns", None)

# Membership functions of method one and method two
# Simple method and exponential method
def calculate_membership(method, x, y, z):
    if method == 'traditional method':
        E = 1 - (1/180) * max(x-y, y-z)
        I = 1 - (1/60) * min(x-y, y-z)
        R = 1 - (1/90) * abs(x-90)
        IR = min(I, R)
        O = min(1 - I, 1 - R, 1 - E)
        return {'Equilateral E': E, 'Isosceles I': I, 'Rectangular R': R, 'IR': IR, 'Atypical O': O}
    elif method == 'Exponential method':
        t1=x-z
        t2 = min(x - y, y - z)
        t3 = abs(x - 90)
        E = (1 - t1/180) ** t1
        I = (1 - t2/60) ** t2
        R = (1 - t3/90) ** t3
        IR = min(I, R)
        O = min(1 - I, 1 - R, 1 - E)
        return {'Equilateral E': E, 'Isosceles I': I, 'Rectangular R': R, 'IR': IR, 'Atypical O': O}

# Get user input
user_choice = input("Select operation (enter '1' to randomly generate the angle, enter '2' to enter the angle yourself): ")

if user_choice == '1':
    # Randomly generate angles
    alpha = random.randint(0, 180)
    beta = random.randint(0, 180 - alpha)
    gamma = 180 - alpha - beta
else:
    # User input angle
    alpha = float(input("Please enter the angle alpha: "))
    beta = float(input("Please enter the angle beta: "))
    gamma = 180 - alpha - beta

    # Check if the sum of angles is equal to 180 degrees
    if alpha + beta + gamma != 180:
        raise ValueError("The sum of the entered angles is not equal to 180 degrees, please re-enter.")

# Calculate membership degree
methods = ['traditional method', 'Exponential method']
results = []

for method in methods:
    membership = calculate_membership(method, alpha, beta, gamma)
    results.append(membership)

#Create a table to display membership
df = pd.DataFrame(results, index=methods)

# Find the type with the highest membership
recognized_method_1 = df.loc['traditional method'].idxmax()
recognized_method_2 = df.loc['Exponential method'].idxmax()

# print results
print("Input Angles (alpha, beta, gamma):", alpha, beta, gamma)
print(df)
print()
print("Recognized Triangle Type for traditional method:", recognized_method_1)
print("Recognized Triangle Type for Exponential method:", recognized_method_2)