필터 지우기
필터 지우기

There is some problem with insertshape function in my matlab program

조회 수: 11 (최근 30일)
Shubham Sharma
Shubham Sharma 2024년 7월 25일 10:49
편집: Walter Roberson 2024년 7월 25일 16:37
% 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);

답변 (1개)

Karan Singh
Karan Singh 2024년 7월 25일 11:08
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

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by