Search code examples
matlabpositionpixel

matlab: finding x-y position of green edge in 500x500 pixel .jpg image


Can anyone help me solve this problem:?

I am using the imoverlay function below.

I overlay the image of a box (magnetic image), so that the green boundary edge is shown using the 'canny' edge detection.

I want to scan the image (.jpg file) and find the pixel postion of the green edge, so that I can take this position and convert it to x, y position, on a 2D surface plot (of the magnetic image).

In the program below, 'pixelColor' will not exceed 64, but 'get(gca, 'CLim')' shows 256 colors

How can I alter this program so that it can find the pixel position of the green 'canny' edge? Where I need one point for each a position.

Thanking you in advance,

Brendan Darrer

============================== My 1st attempt using a 'for loop' and an 'array' of the image wasn't successful, although it works for k = [0 0 1], it doesn't for k = [0 1 0] => green. I think green is in 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);

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


Solution

  • I would not usually use colormap values used to display matlab images to find pixels with known rgb values.

    Instead you can pick the green pixels (rgb [0 1 0]) as follows

    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)
    

    enter image description here