Hi @Michael ,
After going through your comments and to tackle the problem of image similarity comparisons between a hand-drawn tactile map and its template, you need to consider several factors, including the nature of the images, the features you want to compare, and the potential inconsistencies in the hand-drawn lines. Below is a detailed approach to developing a MATLAB script that addresses these challenges. Before you can compare the images, you need to preprocess them to enhance the features you are interested in. This includes converting the images to grayscale, applying edge detection, and possibly thinning the lines to ensure you are working with a consistent representation of the line segments.
% Load images template = imread('/MATLAB Drive/vecteezy_blank-paper-scroll-png- illustration_8501601.png'); hand_drawn = imread('/MATLAB Drive/vecteezy_mandala-for-design-hand- drawn_9874711.png');
% Convert to grayscale template_gray = rgb2gray(template); hand_drawn_gray = rgb2gray(hand_drawn);
% Apply edge detection template_edges = edge(template_gray, 'Canny'); hand_drawn_edges = edge(hand_drawn_gray, 'Canny');
% Thinning the edges template_thinned = bwmorph(template_edges, 'thin', inf); hand_drawn_thinned = bwmorph(hand_drawn_edges, 'thin', inf);
Next, you need to extract features from both images. This can include the lengths of line segments, angles between segments, and the overall structure of the drawings. You can use the regionprops function to obtain properties of the detected edges.
% Extract line segments using Hough Transform [H, theta, rho] = hough(template_thinned); peaks = houghpeaks(H, 5, 'threshold', ceil(0.3 * max(H(:)))); lines_template = houghlines(template_thinned, theta, rho, peaks);
[H_hd, theta_hd, rho_hd] = hough(hand_drawn_thinned); peaks_hd = houghpeaks(H_hd, 5, 'threshold', ceil(0.3 * max(H_hd(:)))); lines_hand_drawn = houghlines(hand_drawn_thinned, theta_hd, rho_hd, peaks_hd);
To measure similarity, you can define a scoring function that takes into account the angles, lengths, and structural features of the line segments. You can compute the angle differences and length ratios between corresponding line segments.
function score = calculate_similarity(lines_template, lines_hand_drawn) score = 0; num_lines_template = length(lines_template); num_lines_hand_drawn = length(lines_hand_drawn);
% Iterate through each line in the template for i = 1:num_lines_template % Extract properties of the template line angle_template = atan2d(lines_template(i).point2(2) - lines_template(i).point1(2), ... lines_template(i).point2(1) - lines_template(i).point1(1)); length_template = norm(lines_template(i).point2 - lines_template(i).point1);
% Compare with each line in the hand-drawn image for j = 1:num_lines_hand_drawn angle_hand_drawn = atan2d(lines_hand_drawn(j).point2(2) - lines_hand_drawn(j).point1(2), ... lines_hand_drawn(j).point2(1) - lines_hand_drawn(j).point1(1)); length_hand_drawn = norm(lines_hand_drawn(j).point2 - lines_hand_drawn(j).point1);
% Calculate angle difference and length ratio angle_diff = abs(angle_template - angle_hand_drawn); length_ratio = length_hand_drawn / length_template;
% Update score based on similarity criteria score = score + exp(-angle_diff^2) * exp(-abs(length_ratio - 1)^2); end end
% Normalize score score = score / (num_lines_template + num_lines_hand_drawn); end
% Calculate similarity score similarity_score = calculate_similarity(lines_template, lines_hand_drawn); disp(['Similarity Score: ', num2str(similarity_score)]);
To improve the robustness of the similarity measurement, consider breaking the images into quadrants. This can help in localizing features and reducing the impact of misalignments.
% Divide images into quadrants [rows, cols] = size(template_thinned); quadrants = {template_thinned(1:rows/2, 1:cols/2), template_thinned(1:rows/2, cols/2+1:end), ... template_thinned(rows/2+1:end, 1:cols/2), template_thinned(rows/ 2+1:end, cols/2+1:end)};
% Calculate similarity for each quadrant for k = 1:length(quadrants) quadrant_score = calculate_similarity(lines_template, lines_hand_drawn); disp(['Quadrant ', num2str(k), ' Similarity Score: ', num2str(quadrant_score)]); end
Please see attached.
By preprocessing the images, extracting relevant features, and calculating a similarity score based on angles and lengths, you can effectively assess the similarity between the two images. Additionally, breaking the images into quadrants can enhance the accuracy of the comparison, especially in cases where the hand-drawn lines are inconsistent or incomplete.
Feel free to adjust the parameters and functions to better suit your specific requirements and the characteristics of your images.
If you have any further questions, please let me know.