用于将金属盒的磁性/MIT 图像(2D 表面图)的 Canny 检测边缘叠加到原始图像上的代码。
麻省理工学院 - 磁感应断层扫描。
% 2D MIT surface plots of p.d. phase-difference against x-y coordinates.
% For overlaying edge of a metallic tin box onto 2D MIT surface plot.
% Written by Brendan Darrer
% Date 5th October 2013.
% oscillator = 2.6 V at f = 500 Hz, lock-in amplifier: sensitivity = 50 mV, time constant = 500 ms.
% Load .txt file arrays of, 2D positional data, background image phase and sample
% object phase (e.g. Copper(Cu) Disks).
B = load('C:\work\PhD_in_MIT\LabVIEW\labviewData\positionsData2_20x20_3.txt'); % position data: 2 x 400 array
C = load('C:\work\PhD_in_MIT\LabVIEW\labviewData\helm2Coils124.txt') % background: 10 x 40 array
D = load('C:\work\PhD_in_MIT\LabVIEW\labviewData\helm2Coils119.txt'); % sample object -metal box: 10 x 40 array
% Correcting phase offset, before sensor error correction.
for i=1:10 % columns
for j=1:40 % rows
if (D(j,i) < 0) % correct offset, if e.g. phase = -179 when it should be 181.
D(j,i) = 360 + D(j,i);
end
if (C(j,i) < 0) % correct offset, if e.g. phase = -179 when it should be 181.
C(j,i) = 360 + C(j,i);
end
end
end
% Transposing array D twice.
pD = D'
D2 = pD(:)' % make 2D array 'D' = image of sample object, into single row = D2
% Transposing array C twice.
pBackgrd = C'
C2 = pBackgrd(:)' % make 2D array 'C' = image of background, into single row = C2
% Subtracting background phases from sample object phases
for i=1:400 % 400 columns of one row
D3(i) = D2(i) - C2(i); % subtract background from D2 (= sample object)
end
% Find the lowest value in D3, so as to zero data in - single row array, D4
% 400 columns long.
[D4,I]=min(D3(:));
[ID,JD] = ind2sub(size(D3),I)
display(D4)
display(ID);display(JD)
for i=1:400 % columns for one row
D3(i) = D3(i) - D4; % subtract minimum value D4 to zero the data
end
% Transpose D3 to make it a single column array
D = D3'
% Concatenate position data array, B (2 columns 400 rows) and phase data,
% D (1 column 400 rows) to make position and phase array, B2 (3 columns 400
% rows).
B2 = [B D]
%==========================================================================
% Creating Piecewise cubic interpolation function, fo(x,y), to fit MIT image showing
% contours; fo(x,y) is to be used latter on to overlay edge onto MIT plot.
% Assign array 'B2' to x, y position and z as phase.
x=B2(:,1); y=B2(:,2); z=B2(:,3);
% Make figure the size stated below.
FigHandle = figure('Position', [100, 100, 1049, 910]);
% Create 100 linearly spaced vectors between minimum x and y. i.e. fitting
% function to 100 divisions in x and y.
% From: http://www.mathworks.co.uk/help/matlab/ref/linspace.html
xlin=linspace(min(x),max(x),100); % was 50
ylin=linspace(min(y),max(y),100); % was 50
% Fitting Piecewise cubic interpolation function, fo(x,y,z), to the data.
% From: http://www.mathworks.co.uk/help/curvefit/fit.html
fo = fit( [x, y], z, 'cubicinterp', 'normalize', 'on' );
% 'meshgrid' replicates the grid vectors xlin and ylin to produce a full grid.
% This grid is represented by the output coordinate arrays X and Y.
% From: http://www.mathworks.co.uk/help/matlab/ref/meshgrid.html
[X,Y]=meshgrid(xlin,ylin);
% Plot fitted cubic piecewise interpolation function with contours.
plot( fo, 'Style', 'Contour' );
colormap( copper )
colorbar
%==========================================================================
% Creating 2D surface plot => MIT image, and save as a grayscale .jpg image,
% convert to a true garyscale image in next section.
% Make figure the size stated below.
Fig2Handle = figure('Position', [100, 100, 1049, 910]);
% Z = griddata(x,y,z,X,Y,'cubic') fits a surface of the form z = f(x,y) to the scattered
% data in the vectors (x,y,z). The griddata function interpolates the surface at the
% query points specified by (X,Y) and returns the interpolated values, Z. The
% surface always passes through the data points defined by x and y.
% Z = griddata(..., 'cubic') uses a specified interpolation 'cubic' to compute Z.
% From: http://www.mathworks.co.uk/help/matlab/ref/griddata.html
Z=griddata(x,y,z,X,Y,'cubic');
% surf(X,Y,Z) creates a three-dimensional shaded surface, uses Z for the color data
% and surface height. X and Y are vectors or matrices defining the x and y components
% of a surface. If X and Y are vectors, length(X) = n and length(Y) = m,
% where [m,n] = size(Z). In this case, the vertices of the surface faces are
% (X(j), Y(i), Z(i,j)) triples. To create X and Y matrices for arbitrary domains,
% use the meshgrid function, already run in the above code.
% From: http://www.mathworks.co.uk/help/matlab/ref/surf.html
surf(X,Y,Z)
% 'axis tight' sets the axis limits to the range of the data.
% From: http://www.mathworks.co.uk/help/matlab/ref/axis.html
axis tight;
% 'hold on' retains the current graph and adds another graph to it.
% MATLAB adjusts the axes limits, tick marks, and tick labels as necessary
% to display the full range of the added graph.
% From: http://www.mathworks.co.uk/help/matlab/ref/hold.html
hold on
% View plot 2D surface from on top, looking down.
view(0,90);
% Removing grid lines from plot and smoothing colour boundaries.
% From: http://www.mathworks.co.uk/help/matlab/ref/shading.html
shading flat
shading interp
% The plot3 function displays a three-dimensional plot of a set of data points.
% From: http://www.mathworks.co.uk/help/matlab/ref/plot3.html
plot3(x,y,z,'.','Marker','none')
% Remove axes labels and make figure fill the whole window.
% From: http://stackoverflow.com/questions/7561999/how-to-set-the-plot-in-matlab-to-a-specific-size
set(gca, 'XTickLabel',[], 'YTickLabel',[], ...
'Units','normalized', 'Position',[0 0 1 1])
% Set plot figure to 1000 by 1000 pixels.
set(Fig2Handle, 'Position', [0 0 1000 1000])
% Set color map to grayscle.
colormap (gray)
%***********CHANGE FILE NAME HERE...
% Save above plot image as .jpg file.
saveas(gcf,'C:\work\PhD_in_MIT\LabVIEW\labviewData\CuDiskGrayscale.jpg')
%***********CHANGE FILE NAME HERE...
% Open .jpg file image saved above.
open('C:\work\PhD_in_MIT\LabVIEW\labviewData\CuDiskGrayscale.jpg')
%==========================================================================
% Appying 'Canny' edge detection to grayscale image saved and opened above.
% Make figure the size stated below.
Fig3Handle = figure('Position', [100, 100, 1049, 910]);
% Applying edge detection to 'sample object image' and then overlaying
% 'detected edge' result in green over the original image. Using imoverlay
% function downloaded from:
% https://www.mathworks.co.uk/matlabcentral/fileexchange/10502-image-overlay/content/imoverlay.m
% A = imread(filename, fmt) reads a grayscale or color image from the file
% specified by the string filename. If the file is not in the current folder,
% or in a folder on the MATLAB® path, specify the full pathname.
% From: http://www.mathworks.co.uk/help/matlab/ref/imread.html
I1 = imread('CuDiskGrayscale.jpg');
% Convert grayscale image 'CuDiskGrayscale.jpg' to 'true' grayscale.
I2 = rgb2gray(I1);
% Resize 'CuDiskGrayscale.jpg' as I2, to 1000 by 1000 pixels.
I = imresize(I2, [1000 1000]);
% Find edge of object in image, I, using matlab's canny edge detection
% algorithm, with thresholding = 0.61 (= thresh) as high threshold
% => 0.4*thresh is therefore used for the low threshold.
% Using sigma = sqrt(1000) - as the standard deviation of the Gaussian filter
% From: http://www.mathworks.co.uk/help/images/ref/edge.html
bw = edge(I, 'canny', 0.61, sqrt(1000));
% OUT = IMOVERLAY(IN, MASK, COLOR) takes an input image, IN, and a binary
% image, MASK, and produces an output image whose pixels in the MASK
% locations have the specified COLOR, in this case green = [0 1 0].
% Therefore, overlay edge detection result in green over the original image.
% From: https://www.mathworks.co.uk/matlabcentral/fileexchange/10502-image-overlay/content/imoverlay.m
rgb = imoverlay(I, bw, [0 1 0]);
% Display resultant image.
imshow(rgb)
% find(bw) -> y, x coordinates of bw, size(find(bw)) gives e.g. sy = 966 &
% sx = 1. So it is the number of x and y's i.e. twice the number of (x,y) values in bw.
[sy, sx] = size(find(bw));
% Setting row in cannyXYZ(row,column) to zero.
c = 0;
% Setting cannyXYZ array to zero values. cannyXYZ is the array produced to plot
% the 'canny edge' of the 'sample object' onto the 2D MIT surface plot in the next
% section. cannyXYZ is made from scanning each pixel (1000 x 1000) from the
% overlayed Canny edge in image, rgb, obtained above.
cannyXYZ=zeros(sy,3);
% Nested for loop to check every pixel in 1000 by 1000 pixel image of rgb.
for i=1:1000 % pixel rows of image
for j=1:1000 % pixel columns of image
% Finding green edge in rgb(i,j,color),
% see: http://stackoverflow.com/questions/15406816/finding-1st-red-255-0-0-pixel-possition-using-matlab
if squeeze( rgb(i,j,:) ) == [0;255;0]
c = c + 1;
% Scaling pixels to match MIT plot of 0 to 242 mm in x and y.
% Filling array x values scaled as 242 mm = 1000 pixels.
cannyXYZ(c,1) = j*242/1000;
% Filling array y values scaled as 242 mm = 1000 pixels.
cannyXYZ(c,2) = 242 - i*242/1000;
% Filling array phase values = fo(x,y) => cubic piecewise
% interpolation function of MIT image defined and implemented
% earlier in the code.
cannyXYZ(c,3) = fo(cannyXYZ(c,1),cannyXYZ(c,2));
end
end
end
%***********CHANGE FILE NAME HERE...
% Write array cannyXYZ to a text file.
dlmwrite('C:\work\PhD_in_MIT\LabVIEW\labviewData\cannyXYZ8c.txt', cannyXYZ, 'delimiter', '\t', ...
'precision', 6)
%***********CHANGE FILE NAME HERE...
% load cannyXYZ text file saved above above.
E = load('C:\work\PhD_in_MIT\LabVIEW\labviewData\cannyXYZ8c.txt'); % canny edge as x,y,z points
% Assign array 'E'(= cannyXYZ) to xC, yC position and zC as phase.
xC=E(:,1); yC=E(:,2); zC=E(:,3);
%==========================================================================
% Plotting edge of sample object onto MIT surface plot.
% Make figure the size stated below.
Fig4Handle = figure('Position', [100, 100, 1049, 910]);
% Fitting surface of the form z = f(x,y) to the scattered data in the
% vectors (x,y,z) from the array 'B2' of the MIT surface image, run earlier
% in the code. The griddata function interpolates the surface at the
% query points specified by (X,Y) and returns the interpolated values, Z;
% using a specified interpolation 'cubic piecewise function' to compute Z.
% From: http://www.mathworks.co.uk/help/matlab/ref/griddata.html
Z=griddata(x,y,z,X,Y,'cubic');
% surf(X,Y,Z) creates a three-dimensional shaded surface, uses Z for the color data
% and surface height. X and Y are vectors or matrices defining the x and y components
% of a surface. To create X and Y matrices for arbitrary domains, the
% 'meshgrid' function is used, already run earlier in the code.
% From: http://www.mathworks.co.uk/help/matlab/ref/surf.html
surf(X,Y,Z)
% 'axis tight' sets the axis limits to the range of the data.
axis tight;
% 'hold on' retains the current graph and adds another graph to it.
% MATLAB adjusts the axes limits, tick marks, and tick labels as necessary
% to display the full range of the added graph.
hold on
view(0,90);
% Remove gridlines.
shading flat
shading interp
% Plotting MIT 2D surface plot of sample object
% The plot3 function displays a three-dimensional plot of a set of data points.
% surf(X,Y,Z) needed has already been called earlier on.
% From: http://www.mathworks.co.uk/help/matlab/ref/plot3.html
plot3(x,y,z,'.','Marker','none');
hold on
% Plotting 'Canny edge' of sample object, as markers of '.' in yellow on
% top of MIT surface plot
plot3(xC,yC,zC,'.','MarkerSize',1,'MarkerEdgeColor',[1 1 0]);
% 'hold off' resets hold state to the default behaviour, in which MATLAB
% clears the existing graph and resets axes properties to their defaults
% before drawing new plots.
hold off
% Set plot figure to 1000 by 1000 pixels.
set(Fig4Handle, 'Position', [0 0 1000 1000])
% colormap = grayscale.
colormap (gray)
% Labelling x, y and z (=phase) axes.
xlabel('x / mm')
ylabel('y / mm')
zlabel('phase \Delta\phi / degrees')