matlabcomputer-visionobject-detectionmatlab-cvst

Train Cascade Object Detector bounding box out of bound?


I am trying to train a cascade object detector using the built-in function in Matlab (vision toolbox). However, the following message came up after running the command.

*

Error using trainCascadeObjectDetector (line 245)
Error reading instance 1 from image 2, bounding box possibly out of image bounds.

*

I don't understand why the bounding box can be out of bounds. All the parameters for my positive images are set up correctly (starting point x,y, width, and height. I used createMask(h) to create a mask and find the minimum coordinates for x and y to be starting point and max-min for each dimension to be the width and height), and the negative images (as far as I know) are just images without any setup needed.

Anyone ever ran into the same problem? How did you solve it?

EDIT: Here's the code. I don't have the toolbox for training the "data" struct, so I wrote one myself

positive_samples=struct;
list=dir('my_folder_name_which_I_took_out');

L=length(list)-3; %Set L to be the length of the image list.
for i=1:length(list)
    positive_samples(i).imageFilename=list(i).name;
end
positive_samples(:,1)=[]; %first 3 lines do not contain file names
positive_samples(:,1)=[];
positive_samples(:,1)=[];

for j=1:1
    imshow(positive_samples(j).imageFilename);
    title(positive_samples(j).imageFilename);
    h=imrect;
    h1=createMask(h);
    I=imread(positive_samples(j).imageFilename);
    [le, wi, hi]=size(I);
    tempmat=[];
    count=1;
    for l=1:le
        for m=1:wi
            if h1(l,m)==1
                tempmat(count,1)=l;
                tempmat(count,2)=m;
                count=count+1;
            end
        end
    end
    positive_samples(j).objectBoundingBoxes(1,1)=min(tempmat(:,1));
    positive_samples(j).objectBoundingBoxes(1,2)=min(tempmat(:,2));
    positive_samples(j).objectBoundingBoxes(1,3)=max(tempmat(:,2))-min(tempmat(:,2));
    positive_samples(j).objectBoundingBoxes(1,4)=max(tempmat(:,1))-min(tempmat(:,1));
    imtool close all
end
trainCascadeObjectDetector('animalfinder.xml', positive_samples, 'my_neative_folder_name', 'FalseAlarmRate', 0.2, 'NumCascadeStages', 3);

sorry if it's messy......


Solution

  • I did not run the code, because I don't own the toolbox, but the following lines are very "suspicious":

    positive_samples(j).objectBoundingBoxes(1,1)=min(tempmat(:,1));
    positive_samples(j).objectBoundingBoxes(1,2)=min(tempmat(:,2));
    positive_samples(j).objectBoundingBoxes(1,3)=max(tempmat(:,2))-min(tempmat(:,2));
    positive_samples(j).objectBoundingBoxes(1,4)=max(tempmat(:,1))-min(tempmat(:,1));
    

    I would expect:

    positive_samples(j).objectBoundingBoxes(1,1)=min(tempmat(:,2));
    positive_samples(j).objectBoundingBoxes(1,2)=min(tempmat(:,1));
    positive_samples(j).objectBoundingBoxes(1,3)=max(tempmat(:,2))-min(tempmat(:,2));
    positive_samples(j).objectBoundingBoxes(1,4)=max(tempmat(:,1))-min(tempmat(:,1));
    

    Some suggestions to shorten your code, they are not related to the problem:

    You can shorten line 4 to 9 to a single line, avoiding the loop: [positive_samples(1:L).im]=list(4:end).name

    And this loop can be replaced as well:

    tempmat=[];
    count=1;
    for l=1:le
        for m=1:wi
            if h1(l,m)==1
                tempmat(count,1)=l;
                tempmat(count,2)=m;
                count=count+1;
            end
        end
    end
    

    shorter and faster code:

    [y,x]=find(h1);
    tempmat=[y x];