Repository: opencv
Issue: OpponentColorDescriptorExtractor::computeImpl  vector subscript out of range error
Labels: bug,auto-transferred,priority: normal,category: features2d

Transferred from http://code.opencv.org/issues/1916

```
|| Elisavet Chatzilari on 2012-05-10 11:51
|| Priority: Normal
|| Affected: None
|| Category: features2d
|| Tracker: Bug
|| Difficulty: None
|| PR: None
|| Platform: None / None
```
## OpponentColorDescriptorExtractor::computeImpl  vector subscript out of range error

```
I am trying to extract opponent sift features of images using PyramidDense detectors for keypoints. The code doesnt crash when calculating SIFT but only with opponent SIFT. I have seen that the error happens in one of these two lines:
<pre>
const int maxInitIdx = std::max( channelKeypoints[0][idxs[0][cp[0]]].class_id,
                                         std::max( channelKeypoints[1][idxs[1][cp[1]]].class_id,
                                                   channelKeypoints[2][idxs[2][cp[2]]].class_id ) );

        while( channelKeypoints[0][idxs[0][cp[0]]].class_id < maxInitIdx && cp[0] < channelKeypoints[0].size() ) { cp[0]++; }.
</pre>

Also it does not crash with all images. There are images that cause this error only in debug mode and other that cause it in both debug and release. The code is quite simple, here is the corresponding part:
<pre>

// load the image
    string FullPathName = featureFolder + this->imageName;
    this->image=cv::imread(FullPathName);

    // find and load segmentation mask
    if (this->segmentName=="")
    {
        int count = this->imageName.find_last_of(".",imageName.size());
        this->segmentName = this->imageName.substr(0,count) + ".seg";
    }
    string FullSegName = featureFolder + this->segmentName;
    this->segment = cv::imread(FullSegName,-1);

    // Extract keypoints and features
    Ptr<FeatureDetector> featureDetector = FeatureDetector::create( "PyramidDense" );
    PyramidAdaptedFeatureDetector PyramidDetector(featureDetector,2);
    Ptr<DescriptorExtractor> descExtractor = DescriptorExtractor::create(descriptorType);

    vector<vector<KeyPoint>> imageKeypoints;
// find the unique segment IDs
    this->RegionIDs = this->unique(segment,segment.at<unsigned __int8>(0,0));

    for (size_t i=0;i<this->RegionIDs.size();i++)
    {
        imageKeypoints.push_back( vector<KeyPoint> () );
        cv::Mat outMask = this->createMask(segment,this->RegionIDs[i]);
        PyramidDetector.detect( image, imageKeypoints[i], outMask );
    }

    Ptr<DescriptorMatcher> descMatcher = DescriptorMatcher::create( "FlannBased" );
    Ptr<BOWImgDescriptorExtractor> bowExtractor = new BOWImgDescriptorExtractor( descExtractor, descMatcher );
    bowExtractor->setVocabulary(this->Vocabulary);
    Mat temp;
    for (size_t i=0;i<this->RegionIDs.size();i++)
    {
        descriptor.push_back( vector<double> () );
        temp.create(imageKeypoints[i].size(),384,CV_32FC1);
        cout << i << "\tKeypoints before: " << imageKeypoints.at(i).size() << "\t";
        descExtractor->compute(this->image, imageKeypoints[i], temp);
        temp.release();
        cout << "Keypoints after: " << imageKeypoints.at(i).size() << "\n";
    }
</pre>

The code crashes inside the compute only for specific regions.
```
## History
##### Kirill Kornyakov on 2012-05-16 06:58

```
-   Description changed from I am trying to extract opponent sift
    features of images using PyramidDense de... to I am trying to
    extract opponent sift features of images using PyramidDense de...
    More
-   Category set to features2d
```