function goodedges=checkrange(edgemat, narrow, wide);

% This function runs through the matrix returned by the edge detector,
% looking for edges which match the size we're looking for.

% Is edgemat 255 or 1's?

[I,J]=find(edgemat>0);
sz=size(J);
goodv=[];
goodh=[];

for j=1:sz(1)-1
   if J(j+1) == J(j)
      if inrange(I(j), I(j+1), narrow, wide)
         goodv=[goodv;[I(j),J(j)];[I(j+1),J(j+1)]];
      end
   end
end

%K=zeros(size(edgemat));
%K(goodv(:,1),goodv(:,2))=255;

goodvm=zeros(size(edgemat));
if prod(size(goodv)) > 0
   goodvlin=sub2ind(size(edgemat), goodv(:,1), goodv(:,2));
   goodvm(goodvlin)=1;
end
   
[orderedI,index]=sort(I);
matchedJ=J(index);

for i=1:sz(1)-1
   if orderedI(i+1) == orderedI(i)
      if inrange(matchedJ(i), matchedJ(i+1), narrow, wide)
         goodh=[goodh;[orderedI(i),matchedJ(i)];[orderedI(i+1),matchedJ(i+1)]];
      end
   end
end

goodhm=zeros(size(edgemat));
if prod(size(goodh)) > 0
   goodhlin=sub2ind(size(edgemat), goodh(:,1), goodh(:,2));
   goodhm(goodhlin)=1;
end


goodedges=or(goodvm, goodhm);
