3

谁能帮我解决这个问题:?

我正在使用下面的 imoverlay 功能。

我覆盖了一个盒子的图像(磁性图像),以便使用“canny”边缘检测显示绿色边界边缘。

我想扫描图像(.jpg 文件)并找到绿色边缘的像素位置,以便我可以在 2D 表面图(磁性图像)上获取该位置并将其转换为 x、y 位置。

在下面的程序中,'pixelColor' 不会超过 64,但 'get(gca, 'CLim')' 显示 256 种颜色

我怎样才能改变这个程序,以便它可以找到绿色“canny”边缘的像素位置?我需要为每个职位加一分。

提前谢谢你,

布伦丹·达雷尔

============================== 我第一次尝试使用“for循环”和图像的“数组”不是成功,虽然它适用于 k = [0 0 1],但不适用于 k = [0 1 0] => green。我认为绿色在 pixelColor > 64 中:

figure,
       I1 = imread('cigarTinNoGridGrayscale6f.jpg');
       I2 = rgb2gray(I1);
       I = imresize(I2, [500 500])
       bw = edge(I, 'canny', 0.61, sqrt(1000));
       rgb = imoverlay(I, bw, [0 1 0]);
       imshow(rgb)

h = imagesc(rgb);
get(h, 'CDataMapping')
get(gca, 'CLim')
%ans =  0   255
map = get(gcf, 'Colormap');

c = 0;
cannyXYZ=zeros(15500,3);
for i=1:500 % pixel columns of image
    for j=1:500 % pixel rows
        pixelColor = rgb(i,j);
        if pixelColor == 0   % won't allow pixelColor = 0
            pixelColor = 1;
        end
        %%{
        if pixelColor >= 65 % won't allow pixelColor > 64
            pixelColor = 64;
        end
        %%}
        k = map(pixelColor,:);
        if k(1,1) == 0 && k(1,2) == 1 && k(1,3) == 0   %(=> green)
            c = c + 1;
            cannyXYZ(c,1) = i*117/500;
            cannyXYZ(c,2) = j*117/500;
            cannyXYZ(c,3) = fo(cannyXYZ(c,1),cannyXYZ(c,2));
        end
    end
end

dlmwrite('cannyXYZ3.txt', cannyXYZ, 'delimiter', '\t', ...
         'precision', 6)


function out = imoverlay(in, mask, color)
%IMOVERLAY Create a mask-based image overlay.
%   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 should be a grayscale or an RGB image of class uint8, uint16, int16,
%   logical, double, or single.  If IN is double or single, it should be in
%   the range [0, 1].  If it is not in that range, you might want to use
%   mat2gray to scale it into that range.
%
%   MASK should be a two-dimensional logical matrix.
%
%   COLOR should be a 1-by-3 vector of values in the range [0, 1].  [0 0 0]
%   is black, and [1 1 1] is white.
%
%   OUT is a uint8 RGB image.
%
%   Examples
%   --------
%   Overlay edge detection result in green over the original image.
%       
%       I = imread('cameraman.tif');
%       bw = edge(I, 'canny');
%       rgb = imoverlay(I, bw, [0 1 0]);
%       imshow(rgb)
%
%   Treating the output of peaks as an image, overlay the values greater than
%   7 in red.  The output of peaks is not in the usual grayscale image range
%   of [0, 1], so use mat2gray to scale it.
%
%       I = peaks;
%       mask = I > 7;
%       rgb = imoverlay(mat2gray(I), mask, [1 0 0]);
%       imshow(rgb, 'InitialMagnification', 'fit')

%   Steven L. Eddins
%   Copyright 2006-2012 The MathWorks, Inc.

% If the user doesn't specify the color, use white.
DEFAULT_COLOR = [1 1 1];
if nargin < 3
    color = DEFAULT_COLOR;
end

% Force the 2nd input to be logical.
mask = (mask ~= 0);

% Make the uint8 the working data class.  The output is also uint8.
in_uint8 = im2uint8(in);
color_uint8 = im2uint8(color);

% Initialize the red, green, and blue output channels.
if ndims(in_uint8) == 2
    % Input is grayscale.  Initialize all output channels the same.
    out_red   = in_uint8;
    out_green = in_uint8;
    out_blue  = in_uint8;
else
    % Input is RGB truecolor.
    out_red   = in_uint8(:,:,1);
    out_green = in_uint8(:,:,2);
    out_blue  = in_uint8(:,:,3);
end

% Replace output channel values in the mask locations with the appropriate
% color value.
out_red(mask)   = color_uint8(1);
out_green(mask) = color_uint8(2);
out_blue(mask)  = color_uint8(3);

% Form an RGB truecolor image by concatenating the channel matrices along
% the third dimension.
out = cat(3, out_red, out_green, out_blue);

=================================================

4

2 回答 2

2

我通常不会使用用于显示 matlab 图像的颜色图值来查找具有已知 rgb 值的像素。

相反,您可以选择绿色像素 (rgb [0 1 0]),如下所示

I2 = imread('cameraman.tif'); % imread('cigarTinNoGridGrayscale6f.jpg');
I = imresize(I2, [500 500]);
bw = edge(I, 'canny', 0.61, sqrt(1000));
rgb = imoverlay(I, bw, [0 1 0]);

indx = find(rgb(:,:,1)==0 & rgb(:,:,2)==255 & rgb(:,:,3)==0); % <-- pick green pixels    
nm = numel(rgb)/3;

% change green pixels to red
rgb2=rgb;
rgb2(indx) = 255;
rgb2(indx + nm) = 0;
rgb2(indx + 2*nm) = 0;

figure
subplot(121)
imshow(rgb)
subplot(122)
imshow(rgb2)

在此处输入图像描述

于 2013-08-29T12:28:02.183 回答
0

用于将金属盒的磁性/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')
于 2017-06-20T13:23:25.080 回答