New here, i cannot figure it out ( Index exceeds the number of array elements (2). )

1 visualización (últimos 30 días)
i=imread('1_crack.png');
bw=rgb2gray(i);
[S T]=graythresh(bw);
bw=imbinarize(bw,S);
bwn1=bw_filter(bw,15);
f = ones(3,3) / 9;
smooth_0 = imfilter(bw,f);
for i = 1:5
smooth_0 = imfilter(smooth_0,f);
end
[canny_smooth_0,thres_0] = edge(smooth_0,'canny');
[canny_smooth_0,thres_0] = edge(smooth_0,'canny',thres_0);
canny_smooth_0 = uint8(canny_smooth_0) * 255; %把遮罩轉換成影像
bwn1=canny_smooth_0;
bwn1 = imfill(bwn1, 'holes');
labeledImage = bwlabel(bwn1);
% % Measure the area
measurements_o = regionprops(labeledImage, 'Area');
[~, columns] = size(bw);
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
boundaries = bwboundaries(bw); %跟蹤二值圖像中的區域邊界
numberOfBoundaries = size(boundaries, 1);
for blobIndex_o = 1 : numberOfBoundaries
thisBoundary = boundaries{blobIndex_o};
x_o = thisBoundary(:, 2); % x = columns.
y_o = thisBoundary(:, 1); % y = rows.
% Find which two bounary points are farthest from each other.
maxDistance_o = 0;
for k = 1 : length(x_o)
distances = sqrt( (x_o(k) - x_o) .^ 2 + (y_o(k) - y_o) .^ 2 );
[thisMaxDistance, indexOfMaxDistance] = max(distances);
if thisMaxDistance > maxDistance_o
maxDistance_o = thisMaxDistance;
index1_o = k;
index2_o = indexOfMaxDistance;
end
end
% Find the midpoint of the line.
xMidPoint_o = mean([x_o(index1_o), x_o(index2_o)]);
yMidPoint_o = mean([y_o(index1_o), y_o(index2_o)]);
longSlope = (y_o(index1_o) - y_o(index2_o)) / (x_o(index1_o) - x_o(index2_o));
perpendicularSlope = -1/longSlope;
% Use point slope formula (y-ym) = slope * (x - xm) to get points
y1_o = perpendicularSlope * (1 - xMidPoint_o) + yMidPoint_o;
y2_o = perpendicularSlope * (columns - xMidPoint_o) + yMidPoint_o;
% Get the profile perpendicular to the midpoint so we can find out when if first enters and last leaves the object.
[cx_o,cy_o,c] = improfile(bw,[1, columns], [y1_o, y2_o], 1000);
% Get rid of NAN's that occur when the line's endpoints go above or below the image.
c(isnan(c)) = 0;
firstIndex_o = find(c, 1, 'first');
lastIndex_o = find(c, 1, 'last');
% Compute the distance of that perpendicular width.
perpendicularWidth_o = sqrt( (cx_o(firstIndex_o) - cx_o(lastIndex_o)) .^ 2 + (cy_o(firstIndex_o) - cy_o(lastIndex_o)) .^ 2 );
% Get the average perpendicular width. This will approximately be the area divided by the longest length.
averageWidth_o = measurements_o(blobIndex_o).Area / maxDistance_o;
end
error : Index exceeds the number of array elements (2).

Respuestas (2)

Simon Chan
Simon Chan el 12 de Mzo. de 2022
The labeledImage is based on 'bwn1' and I think you should also use 'bwn1' instead of 'bw' when finding their boundaries.
[~, columns] = size(bwn1);
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
boundaries = bwboundaries(bwn1); %跟蹤二值圖像中的區域邊界

yanqi liu
yanqi liu el 14 de Mzo. de 2022
clc; clear all; close all;
i=imread('image.png');
bw=rgb2gray(i);
[S T]=graythresh(bw);
bw=imbinarize(bw,S);
% bwn1=bw_filter(bw,15);
f = ones(3,3) / 9;
smooth_0 = imfilter(bw,f);
for i = 1:5
smooth_0 = imfilter(smooth_0,f);
end
[canny_smooth_0,thres_0] = edge(smooth_0,'canny');
[canny_smooth_0,thres_0] = edge(smooth_0,'canny',thres_0);
canny_smooth_0 = uint8(canny_smooth_0) * 255; %把遮罩轉換成影像
bwn1=canny_smooth_0;
bwn1 = imfill(bwn1, 'holes');
labeledImage = bwlabel(bwn1);
% % Measure the area
measurements_o = regionprops(labeledImage, 'Area');
[~, columns] = size(bw);
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
boundaries = bwboundaries(~bw); %跟蹤二值圖像中的區域邊界
numberOfBoundaries = size(boundaries, 1);
for blobIndex_o = 1 : size(boundaries,1)
thisBoundary = boundaries{blobIndex_o};
x_o = thisBoundary(:, 2); % x = columns.
y_o = thisBoundary(:, 1); % y = rows.
% Find which two bounary points are farthest from each other.
maxDistance_o = 0;
for k = 1 : length(x_o)
distances = sqrt( (x_o(k) - x_o) .^ 2 + (y_o(k) - y_o) .^ 2 );
[thisMaxDistance, indexOfMaxDistance] = max(distances);
if thisMaxDistance > maxDistance_o
maxDistance_o = thisMaxDistance;
index1_o = k;
index2_o = indexOfMaxDistance;
end
end
if index2_o > length(x_o)
continue;
end
% Find the midpoint of the line.
xMidPoint_o = mean([x_o(index1_o), x_o(index2_o)]);
yMidPoint_o = mean([y_o(index1_o), y_o(index2_o)]);
longSlope = (y_o(index1_o) - y_o(index2_o)) / (x_o(index1_o) - x_o(index2_o));
perpendicularSlope = -1/longSlope;
% Use point slope formula (y-ym) = slope * (x - xm) to get points
y1_o = perpendicularSlope * (1 - xMidPoint_o) + yMidPoint_o;
y2_o = perpendicularSlope * (columns - xMidPoint_o) + yMidPoint_o;
% Get the profile perpendicular to the midpoint so we can find out when if first enters and last leaves the object.
[cx_o,cy_o,c] = improfile(bw,[1, columns], [y1_o, y2_o], 1000);
% Get rid of NAN's that occur when the line's endpoints go above or below the image.
c(isnan(c)) = 0;
firstIndex_o = find(c, 1, 'first');
lastIndex_o = find(c, 1, 'last');
% Compute the distance of that perpendicular width.
perpendicularWidth_o = sqrt( (cx_o(firstIndex_o) - cx_o(lastIndex_o)) .^ 2 + (cy_o(firstIndex_o) - cy_o(lastIndex_o)) .^ 2 );
% Get the average perpendicular width. This will approximately be the area divided by the longest length.
bwi = ~bw;
bwi = bwselect(bwi, round(thisBoundary(:, 2)), round(thisBoundary(:, 1)));
measurements_oi = regionprops(bwi, 'Area');
if isempty(measurements_oi)
continue;
end
averageWidth_o = measurements_oi.Area / maxDistance_o;
end

Categorías

Más información sobre Computer Vision with Simulink en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by