Created
June 18, 2012 13:30
-
-
Save alexstorer/2948385 to your computer and use it in GitHub Desktop.
labelImages - a small tool to do image labeling in Matlab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% labelImages(impath) | |
% --------------------------------------- | |
% Places labels on an image, and saves the marker locations and a 3x3 | |
% square pixel neighborhood of RGB values. | |
% | |
% Usage: | |
% labelImages('/Users/astorer/Desktop/test.jpg') | |
% LEFT CLICK - Add a point | |
% RIGHT CLICK - Delete a point | |
% 's' - Save progress | |
% 'Esc' - Save and quit | |
% | |
% The following files will be written: | |
% /Users/astorer/Desktop/test.mat | |
% /Users/astorer/Desktop/test.csv | |
function labelImages(impath) | |
[pathstr, fname, ext] = fileparts(impath); | |
loadfile = [pathstr filesep fname '.mat']; | |
try | |
load(loadfile) | |
catch | |
disp(['No data present for image ' impath]) | |
imdat = {}; | |
end | |
nPts = length(imdat); | |
imshow(impath) | |
hold on; | |
imdat = redraw(imdat); | |
imdat = getPoints(impath,imdat); | |
save([pathstr filesep fname '.mat'],'imdat'); | |
writeCSV(impath,imdat) | |
end | |
function imdat = getPoints(impath,imdat) | |
[pathstr, fname, ext] = fileparts(impath); | |
button = -1; | |
while (button ~= 27) | |
[x,y,button] = ginput(1); | |
if button==1 | |
% left click, add a point | |
p = struct; | |
p.x = x; | |
p.y = y; | |
i = length(imdat)+1; | |
p.h2 = plot(x,y,'wo','MarkerSize',5,'MarkerFaceColor','k'); | |
p.h = plot(x,y,'ko','MarkerSize',4,'MarkerFaceColor',[255 56 255]./256); | |
%p.text = text(x+3,y,num2str(length(imdat)+1),'Color','k','FontSize',12,'FontWeight','bold'); | |
p.text2 = text(x+6,y+3,num2str(i),'Color',[255 56 255]./256,'FontSize',16,'FontWeight','bold'); | |
imdat{end+1} = p; | |
elseif button==115 % s to save | |
save([pathstr filesep fname '.mat'],'imdat'); | |
writeCSV(impath,imdat) | |
elseif button==3 | |
cutoff = 25; | |
for i = 1:length(imdat) | |
oldx = imdat{i}.x; | |
oldy = imdat{i}.y; | |
d = sqrt((x - oldx).^2 + (y - oldy).^2); | |
%disp(['Distance to point ' num2str(i) ' is ' num2str(d)]); | |
if d < cutoff | |
disp(['Removing point ' num2str(i)]) | |
set(imdat{i}.h,'visible','off'); | |
set(imdat{i}.h2,'visible','off'); | |
%set(imdat{i}.text,'visible','off'); | |
set(imdat{i}.text2,'visible','off'); | |
imdat = remove(imdat,i); | |
imdat = redraw(imdat); | |
break; | |
end | |
end | |
end | |
end | |
close(gcf) | |
end | |
function newc = remove(c,i) | |
newc = {}; | |
for ind = 1:length(c) | |
if (ind<i) | |
newc{ind} = c{ind}; | |
end | |
if (ind>i) | |
newc{ind-1} = c{ind}; | |
end | |
end | |
end | |
function imdat = redraw(imdat) | |
for i = 1:length(imdat) | |
try | |
set(imdat{i}.h,'visible','off'); | |
end | |
try | |
set(imdat{i}.h2,'visible','off'); | |
end | |
try | |
%set(imdat{i}.text,'visible','off'); | |
end | |
try | |
set(imdat{i}.text2,'visible','off'); | |
end | |
imdat{i}.h2 = plot(imdat{i}.x,imdat{i}.y,'wo','MarkerSize',5,'MarkerFaceColor','k'); | |
imdat{i}.h = plot(imdat{i}.x,imdat{i}.y,'ko','MarkerSize',4,'MarkerFaceColor',[255 56 255]./256); | |
x = imdat{i}.x; | |
y = imdat{i}.y; | |
%imdat{i}.text = text(x+3,y,num2str(i),'Color','k','FontSize',16,'FontWeight','bold'); | |
imdat{i}.text2 = text(x+6,y+3,num2str(i),'Color',[255 56 255]./256,'FontSize',16,'FontWeight','bold'); | |
end | |
end | |
function writeCSV(impath,imdat) | |
n = 3; | |
RGB = imread(impath); | |
[pathstr, fname, ext] = fileparts(impath); | |
fid = fopen([pathstr filesep fname '.csv'],'w+'); | |
for i = 1:length(imdat) | |
x = imdat{i}.x; | |
y = imdat{i}.y; | |
dat = [i getMeanNbd(x,y,RGB(:,:,1),n) getMeanNbd(x,y,RGB(:,:,2),n) getMeanNbd(x,y,RGB(:,:,3),n)]; | |
fprintf(fid,'%d, %3.4f , %3.4f, %3.4f\n',dat); | |
end | |
fclose(fid); | |
end | |
% flip x and y! | |
function m = getMeanNbd(y,x,mat,nbd) | |
x = round(x); | |
y = round(y); | |
if mod(nbd,2)==0 | |
error('Please give an odd neighborhood') | |
end | |
tot = 0; | |
for xi = -floor(nbd/2):floor(nbd/2) | |
for yi = -floor(nbd/2):floor(nbd/2) | |
tot = tot + double(mat(x+xi,y+yi)); | |
end | |
end | |
m = tot/(nbd*nbd); % e.g., a 3x3 nbd | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment