11 views (last 30 days)
Show older comments
Shubham Sharma on 25 Jul 2024 at 10:49
Edited: Walter Roberson on 25 Jul 2024 at 16:37
Open in MATLAB Online
% Define area threshold (e.g., 100 pixels)
areaThreshold = 500; % Set your desired threshold value here
% Read the TIFF image
image = imread('quartz1.tif');
% Convert to grayscale if not already
if size(image, 3) == 3
grayImage = rgb2gray(image);
else
grayImage = image;
end
% Apply Gaussian blur
blurredImage = imgaussfilt(grayImage, 2);
% Enhance contrast
enhancedImage = imadjust(blurredImage);
% Apply sharpening
sharpenedImage = imsharpen(enhancedImage);
% Detect edges
edges = edge(sharpenedImage, 'Canny');
% Find contours
contours = bwconncomp(edges);
% Calculate properties for each grain
stats = regionprops(contours, 'Area', 'Perimeter', 'MajorAxisLength', 'MinorAxisLength', 'Orientation', 'BoundingBox', 'PixelIdxList', 'Centroid');
% Process each grain
for i = 1:length(stats)
if stats(i).Area > areaThreshold
% Get the bounding box for each grain
bb = stats(i).BoundingBox;
grainRegion = imcrop(grayImage, bb);
% Create a new image to draw axes
grainImageWithAxes = cat(3, grainRegion, grainRegion, grainRegion);
% Get the ellipse properties
orientation = stats(i).Orientation;
majorAxisLength = stats(i).MajorAxisLength;
minorAxisLength = stats(i).MinorAxisLength;
perimeter = stats(i).Perimeter; % Perimeter value
centerX = stats(i).Centroid(1) - bb(1);
centerY = stats(i).Centroid(2) - bb(2);
% Compute the endpoints of the major and minor axes
majorAxisEnd1 = [centerX + majorAxisLength / 2 * cosd(orientation), centerY - majorAxisLength / 2 * sind(orientation)];
majorAxisEnd2 = [centerX - majorAxisLength / 2 * cosd(orientation), centerY + majorAxisLength / 2 * sind(orientation)];
minorAxisEnd1 = [centerX + minorAxisLength / 2 * cosd(orientation + 90), centerY - minorAxisLength / 2 * sind(orientation + 90)];
minorAxisEnd2 = [centerX - minorAxisLength / 2 * cosd(orientation + 90), centerY + minorAxisLength / 2 * sind(orientation + 90)];
% Ensure endpoints are within bounds of the image
majorAxisEnd1 = max(min(majorAxisEnd1, size(grainRegion, 2) - 1), 1);
majorAxisEnd2 = max(min(majorAxisEnd2, size(grainRegion, 2) - 1), 1);
minorAxisEnd1 = max(min(minorAxisEnd1, size(grainRegion, 2) - 1), 1);
minorAxisEnd2 = max(min(minorAxisEnd2, size(grainRegion, 2) - 1), 1);
% Draw the axes
grainImageWithAxes = insertShape(grainImageWithAxes, 'Line', [majorAxisEnd1, majorAxisEnd2], 'Color', 'red', 'LineWidth', 3);
grainImageWithAxes = insertShape(grainImageWithAxes, 'Line', [minorAxisEnd1, minorAxisEnd2], 'Color', 'blue', 'LineWidth', 3);
% Draw contour (perimeter)
contour = bwboundaries(contours.PixelIdxList{i});
contourVertices = contour{1} - [bb(1), bb(2)];
% Convert contourVertices to a cell array with one matrix
contourVerticesCell = {contourVertices};
% Draw the contour
grainImageWithAxes = insertShape(grainImageWithAxes, 'Polygon', 'Vertices', {contourVertices}, 'Color', 'green', 'LineWidth', 2);
% Add text annotations with larger font size
grainImageWithAxes = insertText(grainImageWithAxes, [10 10], sprintf('Major Axis: %.2f', majorAxisLength), 'FontSize', 12, 'TextColor', 'red', 'BoxColor', 'black');
grainImageWithAxes = insertText(grainImageWithAxes, [10 30], sprintf('Minor Axis: %.2f', minorAxisLength), 'FontSize', 12, 'TextColor', 'blue', 'BoxColor', 'black');
grainImageWithAxes = insertText(grainImageWithAxes, [10 50], sprintf('Perimeter: %.2f', perimeter), 'FontSize', 12, 'TextColor', 'green', 'BoxColor', 'black');
% Save the image with axes using PNG format
filename = sprintf('grain_%d_axes.png', i);
imwrite(grainImageWithAxes, filename, 'PNG');
% Display the image with axes
figure;
imshow(grainImageWithAxes);
title(sprintf('Sand Grain %d', i));
end
end
the error i am getting is
Error using insertShape
Expected POSITION to be one of these types:
cell
Error in insertShape>checkPosition (line 332)
validateattributes(position,{'cell'}, {'nonempty', 'vector'}, ...
Error in insertShape>validateAndParseInputs (line 256)
checkPosition(position, shape1);
Error in insertShape (line 129)
validateAndParseInputs(I, shape, position, varargin{:});
Error in untitled8 (line 74)
grainImageWithAxes = insertShape(grainImageWithAxes, 'Polygon', 'Vertices', {contourVertices}, 'Color', 'green', 'LineWidth', 2);
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (1)
Karan Singh on 25 Jul 2024 at 11:08
Open in MATLAB Online
- tiffbucket.zip
@Shubham Sharma
It looks like there's an issue with the way the vertices are being passed to the insertShape function. The Vertices parameter expects a cell array. The conversion to a cell array is not being done correctly.
Here Is the corrected code.
% Define area threshold (e.g., 100 pixels)
areaThreshold = 500; % Set your desired threshold value here
% Read the TIFF image
unzip tiffbucket.zip
image = imread('indexedblobs.tiff');
% Convert to grayscale if not already
if size(image, 3) == 3
grayImage = rgb2gray(image);
else
grayImage = image;
end
% Apply Gaussian blur
blurredImage = imgaussfilt(grayImage, 2);
% Enhance contrast
enhancedImage = imadjust(blurredImage);
% Apply sharpening
sharpenedImage = imsharpen(enhancedImage);
% Detect edges
edges = edge(sharpenedImage, 'Canny');
% Find contours
contours = bwconncomp(edges);
% Calculate properties for each grain
stats = regionprops(contours, 'Area', 'Perimeter', 'MajorAxisLength', 'MinorAxisLength', 'Orientation', 'BoundingBox', 'PixelIdxList', 'Centroid');
% Process each grain
for i = 1:length(stats)
if stats(i).Area > areaThreshold
% Get the bounding box for each grain
bb = stats(i).BoundingBox;
grainRegion = imcrop(grayImage, bb);
% Create a new image to draw axes
grainImageWithAxes = cat(3, grainRegion, grainRegion, grainRegion);
% Get the ellipse properties
orientation = stats(i).Orientation;
majorAxisLength = stats(i).MajorAxisLength;
minorAxisLength = stats(i).MinorAxisLength;
perimeter = stats(i).Perimeter; % Perimeter value
centerX = stats(i).Centroid(1) - bb(1);
centerY = stats(i).Centroid(2) - bb(2);
% Compute the endpoints of the major and minor axes
majorAxisEnd1 = [centerX + majorAxisLength / 2 * cosd(orientation), centerY - majorAxisLength / 2 * sind(orientation)];
majorAxisEnd2 = [centerX - majorAxisLength / 2 * cosd(orientation), centerY + majorAxisLength / 2 * sind(orientation)];
minorAxisEnd1 = [centerX + minorAxisLength / 2 * cosd(orientation + 90), centerY - minorAxisLength / 2 * sind(orientation + 90)];
minorAxisEnd2 = [centerX - minorAxisLength / 2 * cosd(orientation + 90), centerY + minorAxisLength / 2 * sind(orientation + 90)];
% Ensure endpoints are within bounds of the image
majorAxisEnd1 = max(min(majorAxisEnd1, size(grainRegion, 2) - 1), 1);
majorAxisEnd2 = max(min(majorAxisEnd2, size(grainRegion, 2) - 1), 1);
minorAxisEnd1 = max(min(minorAxisEnd1, size(grainRegion, 2) - 1), 1);
minorAxisEnd2 = max(min(minorAxisEnd2, size(grainRegion, 2) - 1), 1);
% Draw the axes
grainImageWithAxes = insertShape(grainImageWithAxes, 'Line', [majorAxisEnd1, majorAxisEnd2], 'Color', 'red', 'LineWidth', 3);
grainImageWithAxes = insertShape(grainImageWithAxes, 'Line', [minorAxisEnd1, minorAxisEnd2], 'Color', 'blue', 'LineWidth', 3);
% Draw contour (perimeter)
contour = bwboundaries(contours.PixelIdxList{i});
contourVertices = contour{1} - [bb(1), bb(2)];
% Convert contourVertices to a cell array with one matrix
contourVerticesCell = {contourVertices};
% Draw the contour
grainImageWithAxes = insertShape(grainImageWithAxes, 'Polygon', contourVerticesCell, 'Color', 'green', 'LineWidth', 2);
% Add text annotations with larger font size
grainImageWithAxes = insertText(grainImageWithAxes, [10 10], sprintf('Major Axis: %.2f', majorAxisLength), 'FontSize', 12, 'TextColor', 'red', 'BoxColor', 'black');
grainImageWithAxes = insertText(grainImageWithAxes, [10 30], sprintf('Minor Axis: %.2f', minorAxisLength), 'FontSize', 12, 'TextColor', 'blue', 'BoxColor', 'black');
grainImageWithAxes = insertText(grainImageWithAxes, [10 50], sprintf('Perimeter: %.2f', perimeter), 'FontSize', 12, 'TextColor', 'green', 'BoxColor', 'black');
% Save the image with axes using PNG format
filename = sprintf('grain_%d_axes.png', i);
imwrite(grainImageWithAxes, filename, 'PNG');
% Display the image with axes
figure;
imshow(grainImageWithAxes);
title(sprintf('Sand Grain %d', i));
end
end
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphics2-D and 3-D PlotsContour Plots
Find more on Contour Plots in Help Center and File Exchange
Tags
- insertshape
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office