%% pwgpread.m is designed to allow a user to determine the required
%% co-ordinates of the points of interest from a jpeg or bitmap file.
%% This could come from scanning in a image from a journal paper, for
%% example.
%% Once the required file has been loaded, the user can select
%% as many points as required with the mouse, the co-ordinates
%% of each are recorded. When finsihed, terminate by clicking
%% the return key. Results are stored in xv and yv vectors.
%%

close all
clear all

stn=input('Name of file? ','s');
filet=menu('Type of file','jpeg','bitmap');
if filet==1
 stn2=[stn,'.jpg'];
else
 stn2=[stn,'.bmp'];
end

%Load in required file
if filet==1
 rgb=imread(stn2,'jpeg');
else
 rgb=imread(stn2,'bmp');
end
rgb2=double(rgb)+1;
rgb2o=rgb2;

%Determine from the user the required lower limit cut off
hisv=hist(rgb2o(:),1:256);
hisv(hisv==0)=1;
figure(1)
plot(1:256,log10(hisv),'.b')
grid
xlabel('Colour Index (lower -> darker)')
ylabel('Occurence')
title('Use mouse to select cut off value (points removed above this)')
[xvc,yvc]=ginput(1);

rgb2(rgb2>xvc)=256;
%rgb2(rgb2<=xvc)=1;
rgb3=uint8(round(rgb2-1));
rgb4=flipud(rgb2);
poi=rgb4~=256;
xv=1:size(rgb4,2);
yv=1:size(rgb4,1);
[xvm,yvm]=meshgrid(xv,yv);

figure(2)
plot(xvm(poi),yvm(poi),'.k','markersize',1)

%Record the required points of interest of user.
[xv,yv]=ginput;

%Save file the 'cleaned' version of the file
imwrite(rgb3,'test.jpg','jpg')

