wnr78ta 0 Newbie Poster

I am writing a m-file that simply takes in a picture of a board with 27 LEDS on it and simply tells you which LED is lit up. My problem is with the calibration of the m-file. I am trying to use [x,y] = ginput(27) to have the user click the leds on the image so that matlab knows their cooridinates, but the problem with that is when i click the LEDs I sometimes get numbers with decimals and ginput requires integers. So using floor or round I can make it an integer but doing so moves the location to far from the actual pixel so this will not work. A copy of my code is bellow any suggestions is appreciated I am just really lost on how i can fix this.

function [LEDs] = identify_LEDS
clc
clf 
close all;
clear all;
filename = 'LED_TEST3';

filename = [filename '.tif'];
imshow(filename)
m = imread(filename)';
LEDs = zeros(1,28);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%---------Grid----------------
%    x1 x2 x3 x4 x5 x6 x7
%y1  1  2  3  4  5  6  7
%y2  8  9  10 11 12 13 14 
%y3  15 16 17 18 19 20 21
%y4  22 23 24 25 26 27 28
%-----------------------------
%x =[455,488,528,558,595,631,667];
%y = [387,447,503,559];
zoom(2)
[x,y] = ginput(27);
x = round(x);
y = round(y);
hold on
counter = 0;
for a = 1:4
  for b = 1:7
        counter = counter+1;
        aa = plot(x(b),y(a),'r+');
        if m(x(b),y(a)) > 200
            set(aa,'color','b') 
            LEDs(counter) = 1;

        end
    end
end
fid = fopen('data.txt', 'wt');
fprintf(fid, '%d\n', LEDs);
fclose(fid);

Thanks,
Robert

Dani AI

Generated

Most of the confusion comes from coordinates vs. matrix indexing, not ginput itself. ginput returns floating x (columns) and y (rows) in axes units. When you index a MATLAB image matrix you must use I(row, column) — i.e. I(y,x). Transposing the image or swapping x/y will make rounding appear to jump to the wrong pixels. Also check the click count: a 4×7 grid is 28 cells, so request the same number from ginput.

A simple, robust manual-workflow:

% show image, get N clicks
N = 28;                    % set to number of LEDs you expect
[xs, ys] = ginput(N);      % xs -> column coords, ys -> row coords
xs = round(xs); ys = round(ys);

gray = rgb2gray(I);        % convert RGB to intensity if needed

for k = 1:numel(xs)
    val = gray(ys(k), xs(k));    % note: row = y, col = x
    if val > 200
        plot(xs(k), ys(k), 'b+');
        LEDs(k) = 1;
    else
        plot(xs(k), ys(k), 'r+');
    end
end

If rounding still seems to move you off the LED center, sample the image at subpixel coordinates instead of rounding. That avoids stepping to the nearest integer pixel and uses interpolation:

V = double(gray);                     % numeric matrix
intensity = interp2(V, xs(k), ys(k)); % subpixel sample at (x,y)

If manual clicking is tedious or unreliable, detect bright circular spots automatically: convert to grayscale, denoise, threshold (or use graythresh), remove small objects, then use regionprops(...,'Centroid') or imfindcircles to get LED centers. Finally match found centroids to the expected grid positions (sort by y then x) and verify you have the expected count.

Note for : remove any transpose on the image, ensure you index as I(y,x), and keep the number of clicks consistent with your grid. These fixes will resolve the "rounding moves too far" symptom.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.