- Use the edge function with 'Canny' to detect edges. Refer the documentation page for the edge function for a better understanding: https://www.mathworks.com/help/images/ref/edge.html
- Compute the gradient magnitude to measure edge contrast.
- Define thresholds to classify the contrast into low, medium, and high categories.
- Calculate the percentage of edge pixels in each category.
calculate the percentages of low, medium, and high contrast edge pixels in the image
2 次查看(过去 30 天)
显示 更早的评论
how i can calculate the percentages of low, medium, and high contrast edge pixels in the image.
N.B we extract edge with canny's function
0 个评论
回答(1 个)
Leepakshi
2025-3-5
Hi Jassem,
To calculate the percentages of low, medium, and high contrast edge pixels after extracting edges with Canny's function, follow these steps:
% Read and convert the image to grayscale
img = imread('example.jpg');
grayImg = rgb2gray(img);
% Detect edges using Canny's method
edges = edge(grayImg, 'Canny');
% Compute gradient magnitudes
[Gx, Gy] = imgradientxy(grayImg);
[Gmag, ~] = imgradient(Gx, Gy);
% Define thresholds for low, medium, and high contrast
lowThreshold = 0.1 * max(Gmag(:)); % Example threshold for low contrast
highThreshold = 0.3 * max(Gmag(:)); % Example threshold for high contrast
% Classify edge pixels
lowContrast = (Gmag < lowThreshold) & edges;
mediumContrast = (Gmag >= lowThreshold) & (Gmag < highThreshold) & edges;
highContrast = (Gmag >= highThreshold) & edges;
% Calculate percentages
totalEdges = sum(edges(:));
lowPercentage = sum(lowContrast(:)) / totalEdges * 100;
mediumPercentage = sum(mediumContrast(:)) / totalEdges * 100;
highPercentage = sum(highContrast(:)) / totalEdges * 100;
% Display results
fprintf('Low Contrast Edge Pixels: %.2f%%\n', lowPercentage);
fprintf('Medium Contrast Edge Pixels: %.2f%%\n', mediumPercentage);
fprintf('High Contrast Edge Pixels: %.2f%%\n', highPercentage);
Adjust the lowThreshold and highThreshold values according to the specific requirements and characteristics of the image. Ensure the image is converted to grayscale before applying edge detection if it is not already in grayscale format.
Additionally, please refer to the following MATLAB Answers post regarding finding Low high contrast values in an image using MATLAB:
Hope that helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!