perClass Documentation
version 5.1 (31-May-2017)

kb9: How to build a detector from a custom region in an image?

Keywords: detectors, image data, interactive tools, PRTools

Problem: How to create a detector for a custom region of interest in an image, using the PRTools classifiers?

Solution: First identify the region, second train a detector, third apply the detector to a new image.

Let's first load and visualize an image.

>> a=imread('roadsign09.bmp'); 
>> sdimage(a) 
>> a=im2feat(a);    %  convert the image into a dataset object

We are interested in detecting the road area. In the Image menu go to Paint class and select Create new class. A pop up window will ask for the name of the class, e.g. road. Paint a region inside the road area. Note that you may set the size of the brush yourself by clicking the right mouse button or using the Image menu. Click the right mouse button and select Stop painting. In this way we have labelled a region of interest in the image.

Save the data with the labels by pressing the s key on the keyboard. A pop up window will ask for the name of the new dataset, e.g. a2. We can now select a smaller dataset and train the quadratic classifier:

>> b=gendat(a2,[400 400]); 
>> w=qdc(b); 
>> out=b*w; 

The quadratic classifier provides soft output for each of the two classes. We are interested in building a road detector operating only on the road output. In order to adjust the detector threshold, we will use the ROC analysis on the road output only (for more details on ROC on a single output (thresholding) click here )

>> r=sdroc(out(:,'road')) 
>> sddrawroc(r) 

Select an appropriate operating point minimizing error on the road class and save the chosen operating point in the roc r (press s key). In the example below we choose the operating point number 94, which has 0.02 error on the class road, as indicated in the figure title.

Now we can construct the road detector. The PRTool mappings always provide 2 or more outputs. In order to have a detector, only one of the outputs has to be thresholded. Therefore we need to pass only the road output of the PRTools classifier w to the roc r. To this end, we select the corresponding feature output using featsel mapping, and then convert the mapping into the pipeline ready for execution:

>> getlab(w) 
ans = 
>> wroad=w*featsel(2,2);    %  select the output of the target class 
>> p=[sdconvert(wroad) r]
sequential pipeline     3x1 'Feature Selection'
 1  sdp_normal          3x2  Bayes-Normal-2, 2 classes, 2 components
 2  sdp_fsel            2x1  Feature Selection
 3  sdp_decide          1x1  Threshold-based decision on road at op 94

We can now apply the detector to the entire image a and visualize the decisions:

>> c=setlabels(a,a*p) 
>> sdimage(c) 

The detector wroad can be applied to any new image d. Let's inspect the results on the image roadsign12.bmp.

>> d=imread('roadsign12.bmp'); 
>> d=im2feat(d); 
>> d=setlabels(d,d*p) 
>> sdimage(d)