Digital image correlation error
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Afiq Zan
el 7 de Sept. de 2020
Comentada: Rel
el 2 de Dic. de 2022
So hello guys...My problem is why " Index exceeds the number of array elements" and why bw3 has an error. Here i provided the coding...the image i put is deformation and undeformed of flat dog bone. if any problem with coding pls point out the mistake. It is important for me to understand future about dic analysis.
I =imread("undeformed.jpg");
I2 =imread("deformed.jpg");
[M,N]=size(I);
[M2,N2]=size(I2);
N = round (N/3);
N2= round (N2/3);
if M==M2&&N==2
else
disp('The size of the input images are not same');
end
disp('Enter the img resolution')
image_res_x = input ('34.4');
image_res_y = input ('35.5');
BW = (im2bw (I, 0.30));
BW2 = (im2bw (I2, 0.30));
BW3 = bwselect (~BW,4);
imshow (BW)
figure, imshow (BW3)
L = bwlabel (BW3);
format long eng
format compact
s = regionprops (L,'Centroid');
imshow (L == 1)
centroid_1 = s(1).Centroid;
imshow (L == 2)
centroid_2 = s(2).Centroid;
imshow (L == 3)
centroid_3 = s(3).Centroid;
imshow (L == 4)
centroid_4 = s(4).Centroid;
BW4 = bwselect (~BW2,4);
imshow (BW2)
figure, imshow (BW4)
L2 = bwlabel (BW4);
s2 = regionprops (L2, 'Centroid');
imshow (L2 == 1)
centroid_1d = s2(1).Centroid;
imshow (L2 == 2)
centroid_2d = s2(2).Centroid;
imshow (L2 == 3)
centroid_3d = s2(3).Centroid;
imshow (L2 == 4)
centroid_4d = s2(4).Centroid;
imshow(BW3)
hold on
for k=numel(s)
t = s(k).Centroid;
text (t(1), t(2), sprintf(('%d'), k))
'Color';'r';
'FontWeight'; 'bold';
'HorizontalAlignment'; 'left';
'VerticalAlignment'; 'bottom';
plot (s(k).Centroid(1), s(k).Centroid(2), 'r*')
end
hold off
figure,
imshow(BW4)
hold on
for k = 1:numel(s2)
t = s2(k).Centroid;
text (t(1), t(2), sprintf (('%d'),k))
'Color'; 'r';
'FontWeight'; 'bold';
'HorizontalAlignment'; 'left';
'VerticalAlignment'; 'bottom';
Plot (s2(k).Centroid(1), s2(k).Centroid(2), 'r*')
end
hold off
centroid_1_x = centroid_1(1)/image_res_x;
centroid_1_y = centroid_1(2)/image_res_y;
centroid_2_x = centroid_2(1)/image_res_x;
centroid_2_y = centroid_2(2)/image_res_y;
centroid_3_x = centroid_3(1)/image_res_x;
centroid_3_y = centroid_3(2)/image_res_y;
centroid_4_x = centroid_4(1)/image_res_x;
centroid_4_y = centroid_4(2)/image_res_y;
centroid_1d_x = centroid_1d(1)/image_res_x;
centroid_1d_y = centroid_1d(2)/image_res_y;
centroid_2d_x = centroid_2d(1)/image_res_x;
centroid_2d_y = centroid_2d(2)/image_res_y;
centroid_3d_x = centroid_3d(1)/image_res_x;
centroid_3d_y = centroid_3d(2)/image_res_y;
centroid_4d_x = centroid_4d(1)/image_res_x;
centroid_4d_y = centroid_4d(2)/image_res_y;
s1 = sqrt ((centroid_2_x - centroid_1_x)^2 + (centroid_2_y - centroid_1_y)^2);
s2 = sqrt ((centroid_3_x - centroid_1_x)^2 + (centroid_3_y - centroid_1_y)^2);
s3 = sqrt ((centroid_4_x - centroid_1_x)^2 + (centroid_4_y - centroid_1_y)^2);
s1d = sqrt ((centroid_2d_x - centroid_1d_x)^2 + (centroid_2d_y - centroid_1d_y)^2);
s2d = sqrt ((centroid_3d_x - centroid_1d_x)^2 + (centroid_3d_y - centroid_1d_y)^2);
s3d = sqrt ((centroid_4d_x - centroid_1d_x)^2 + (centroid_4d_y - centroid_1d_y)^2);
strain_1 = (s1d - s1)/s1;
strain_2 = (s2d - s2)/s2;
strain_3 = (s3d - s3)/s3;
cos_x1 = (centroid_2_x - centroid_1_x)/s1;
cos_x2 = (centroid_3_x - centroid_1_x)/s2;
cos_x3 = (centroid_4_x - centroid_1_x)/s3;
cos_y1 = (centroid_2_y - centroid_1_y)/s1;
cos_y2 = (centroid_3_y - centroid_1_y)/s2;
cos_y3 = (centroid_4_y - centroid_1_y)/s3;
a = [cos_x1^2 cos_y1^2 2*cos_x1*cos_y1
cos_x2^2 cos_y2^2 2*cos_x2*cos_y2
cos_x3^2 cos_y3^2 2*cos_x3*cos_y3];
b = [strain_1*(1+0.5*(strain_1))
strain_2*(1+0.5*(strain_2))
strain_3*(1+0.5*(strain_3))];
c = inv(a)*b;
str_1 = ['Exx =' num2str(c(1))];
disp (str_1)
str_2 = ['Eyy =' num2str(c(2))];
disp (str_2)
str_3 = ['Exy =' num2str(c(3))];
disp (str_3)
0 comentarios
Respuesta aceptada
Image Analyst
el 7 de Sept. de 2020
Unfortunately you forgot to attach your images. And you used size() incorrectly. Change this:
[M,N]=size(I);
[M2,N2]=size(I2);
to this corrected way
[M,N, numberOfColorChannels1]=size(I) % Don't use a semicolon to it will echo to the command window.
[M2,N2, numberOfColorChannels2]=size(I2)
and tell us what you see in the command window. I suspect your images are color. So then you can do
if numberOfColorChannels1 == 3
I = rgb2gray(I);
end
if numberOfColorChannels2 == 3
I2 = rgb2gray(I2);
end
Also one time you used Plot() instead of plot(). MATLAB is case sensitive.
I didn't delve into all the other stuff because it's late at night and you didn't make it easy for us by commenting your code. If the above doesn't help, then tell us what you see, add more comments, and attach your two images.
Más respuestas (1)
Image Analyst
el 9 de Sept. de 2020
Not really sure what you want to do but I worked on it way too long (like 45 minutes) to improve it and this is where it stands. It's A LOT BETTER but still needs work.
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
I =imread("undeformed.jpg");
I2 =imread("deformed.jpg");
[rows1, columns1, numberOfColorChannels1] = size(I)
[rows2, columns2, numberOfColorChannels2] = size(I2)
subplot(2, 3, 1);
imshow(I, []);
title('I', 'FontSize', fontSize);
subplot(2, 3, 2);
imshow(I2, []);
title('I2', 'FontSize', fontSize);
columns1 = round(columns1/3);
columns2= round(columns2/3);
if rows1 ~= rows2 || columns1 ~= columns2 || numberOfColorChannels1 ~= numberOfColorChannels2
uiwait(errordlg('The size of the input images are not same!'));
return;
end
promptMessage = sprintf('Enter the image resolution in the command window.');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit', 'IgnoreCase', true)
return;
end
% Ask user for two floating point numbers.
defaultValue = {'34.4', '35.5'};
titleBar = 'Enter a value';
userPrompt = {'Enter x resolution : ', 'Enter y resolution : '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
image_res_x = usersValue1
image_res_y = usersValue2
BW = im2bw(I, 0.50);
BW2 = im2bw(I2, 0.50);
% Maximize the figure.
g = gcf;
g.WindowState = 'maximized';
drawnow;
subplot(2, 3, 3);
imshow(BW, []);
title('BW', 'FontSize', fontSize);
subplot(2, 3, 4);
imshow(BW2, []);
title('BW2', 'FontSize', fontSize);
%------------------------------------------------------------------------------------
% Ask user to select blob(s) in BW2.
uiwait(msgbox('Click on blob(s) in BW2, then type Enter'));
BW3 = bwselect(BW2, 4);
subplot(2, 3, 5);
imshow(BW3)
% Label the selected blobs.
[L, numBlobs] = bwlabel(BW3);
caption = sprintf('BW3 with %d blobs selected', numBlobs);
title(caption, 'FontSize', fontSize);
if numBlobs == 0
uiwait(warndlg('No blobs found'));
return;
else
uiwait(helpdlg(caption));
end
props3 = regionprops(L, 'Centroid');
xyCentroidsInPixels3 = vertcat(props3.Centroid)
% Show up to 4 labels.
figure;
if numBlobs >= 1
subplot(2, 3, 1);
imshow(L == 1)
title('Blob #1', 'FontSize', fontSize);
end
if numBlobs >= 2
subplot(2, 3, 2);
imshow(L == 2)
title('Blob #2', 'FontSize', fontSize);
end
if numBlobs >= 3
subplot(2, 3, 3);
imshow(L == 3)
title('Blob #3', 'FontSize', fontSize);
end
if numBlobs >= 4
subplot(2, 3, 4);
imshow(L == 4)
title('Blob #4', 'FontSize', fontSize);
end
drawnow;
%------------------------------------------------------------------------------------
% Ask user to select blob(s) in BW2.
uiwait(msgbox('Click on blob(s) in BW2, then type Enter'));
BW4 = bwselect(BW2,4);
subplot(2, 3, 5);
imshow(BW4)
title('BW4 - your selection', 'FontSize', fontSize);
% Label the selected blobs.
[L2, numBlobs2] = bwlabel(BW4);
caption = sprintf('BW4 with %d blobs selected', numBlobs2);
title(caption, 'FontSize', fontSize);
if numBlobs2 == 0
uiwait(warndlg('No blobs found'));
return;
else
caption = sprintf('%d blobs found', numBlobs2);
uiwait(helpdlg(caption));
end
props4 = regionprops(L2, 'Centroid');
xyCentroidsInPixels4 = vertcat(props4.Centroid)
figure;
if numBlobs2 >= 1
subplot(2, 3, 1);
imshow(L2 == 1)
title('Blob #1', 'FontSize', fontSize);
end
if numBlobs2 >= 2
subplot(2, 3, 2);
imshow(L2 == 2)
title('Blob #2', 'FontSize', fontSize);
end
if numBlobs2 >= 3
subplot(2, 3, 3);
imshow(L2 == 3)
title('Blob #3', 'FontSize', fontSize);
end
if numBlobs2 >= 4
subplot(2, 3, 4);
imshow(L2 == 4)
title('Blob #4', 'FontSize', fontSize);
end
subplot(2, 3, 5);
imshow(BW3)
title('BW3 - your selection', 'FontSize', fontSize);
hold on
% Put text labels on top of the image in the overlay.
for k=numel(props3)
x = props3(k).Centroid(1);
y = props3(k).Centroid(2);
text(x, y, sprintf('%d', k), ...
'Color', 'r', ...
'FontWeight', 'bold', ...
'HorizontalAlignment', 'left', ...
'VerticalAlignment', 'bottom');
plot(x, y, 'r*', 'MarkerSize', 10)
end
hold off
subplot(2, 3, 6);
imshow(BW4)
title('BW4 - your selection', 'FontSize', fontSize);
hold on
% Put text labels on top of the image in the overlay.
for k = 1:numel(props4)
x = props4(k).Centroid(1);
y = props4(k).Centroid(2);
text(x, y, sprintf('%d', k), ...
'Color', 'r', ...
'FontWeight', 'bold', ...
'HorizontalAlignment', 'left', ...
'VerticalAlignment', 'bottom');
plot(x, y, 'r*', 'MarkerSize', 10)
end
hold off
% Calibrate to real world units:
xyCentroidsInCm3 = zeros(size(xyCentroidsInPixels3)); % Initialize to the same size.
xyCentroidsInCm4 = zeros(size(xyCentroidsInPixels4)); % Initialize to the same size.
xyCentroidsInCm3(:, 1) = xyCentroidsInPixels3(:, 1) ./ image_res_x;
xyCentroidsInCm3(:, 2) = xyCentroidsInPixels3(:, 2) ./ image_res_y
xyCentroidsInCm4(:, 1) = xyCentroidsInPixels4(:, 1) ./ image_res_x;
xyCentroidsInCm4(:, 2) = xyCentroidsInPixels4(:, 2) ./ image_res_y
% Get distance of every centroid to every other centroid:
distances = pdist2(xyCentroidsInCm3, xyCentroidsInCm4)
% s1 = sqrt((centroid_2_x - centroid_1_x)^2 + (centroid_2_y - centroid_1_y)^2);
% s2 = sqrt((centroid_3_x - centroid_1_x)^2 + (centroid_3_y - centroid_1_y)^2);
% s3 = sqrt((centroid_4_x - centroid_1_x)^2 + (centroid_4_y - centroid_1_y)^2);
%
% s1d = sqrt((centroid_2d_x - centroid_1d_x)^2 + (centroid_2d_y - centroid_1d_y)^2);
% s2d = sqrt((centroid_3d_x - centroid_1d_x)^2 + (centroid_3d_y - centroid_1d_y)^2);
% s3d = sqrt((centroid_4d_x - centroid_1d_x)^2 + (centroid_4d_y - centroid_1d_y)^2);
%
%
% strain_1 = (s1d - s1)/s1;
% strain_2 = (s2d - s2)/s2;
% strain_3 = (s3d - s3)/s3;
%
% cos_x1 = (centroid_2_x - centroid_1_x)/s1;
% cos_x2 = (centroid_3_x - centroid_1_x)/s2;
% cos_x3 = (centroid_4_x - centroid_1_x)/s3;
% cos_y1 = (centroid_2_y - centroid_1_y)/s1;
% cos_y2 = (centroid_3_y - centroid_1_y)/s2;
% cos_y3 = (centroid_4_y - centroid_1_y)/s3;
%
% a = [cos_x1^2 cos_y1^2 2*cos_x1*cos_y1
% cos_x2^2 cos_y2^2 2*cos_x2*cos_y2
% cos_x3^2 cos_y3^2 2*cos_x3*cos_y3]
%
% b = [strain_1*(1+0.5*(strain_1))
% strain_2*(1+0.5*(strain_2))
% strain_3*(1+0.5*(strain_3))]
%
% c = inv(a)*b
% str_1 = ['Exx =' num2str(c(1))];
% disp(str_1)
% str_2 = ['Eyy =' num2str(c(2))];
% disp(str_2)
% str_3 = ['Exy =' num2str(c(3))];
% disp(str_3)
fprintf('Done running %s.m ...\n', mfilename);
msgbox('Done!');
2 comentarios
Ver también
Categorías
Más información sobre Particle & Nuclear Physics en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!