Pregunta

Estoy trabajando con Qt y OpenCV y me gustaría crear un iterador con std::vector <cv::Rect_<int>> para tener acceso a todos los cv::Rect_.
Este vector es parte de un QMap < int, std::vector <cv::Rect_<int>> > _facesframe;

Así es como estoy tratando de tener acceso a estos vectores:

                foreach (unsigned int frame , _imageGItem->_faceSampler._facesframe.keys() )
                {
                    std::vector <cv::Rect_<int>>::const_iterator it = _imageGItem->_faceSampler._facesframe.value(frame).begin();
                    if( it != _imageGItem->_faceSampler._facesframe.value(frame).end())
                    {
                        qDebug()<<"here";
                    }

                }

Pero el programa se bloquea en la línea de if... debido a un incompatibles iterador.

¿Alguien sabe cómo llegar a todos los cv::Rect_<int> de un QMap < int, std::vector <cv::Rect_<int>> > por favor?

¿Fue útil?

Solución

Esto se debe a que está comparando los iteradores a diferentes vectores.

const T QMap::value(const Key & key, const T & defaultValue = T()) const

vector se devuelve por valor, por lo que esto se copia.

debe usar

T & QMap::operator[](const Key & key)

Para corregir esto:

foreach (unsigned int frame , _imageGItem->_faceSampler._facesframe.keys() )
  {
    std::vector <cv::Rect_<int>>::const_iterator it =
                       _imageGItem->_faceSampler._facesframe[frame].begin();
    if( it != _imageGItem->_faceSampler._facesframe[frame].end())
      {
        qDebug()<<"here";
      }

  }

o (bacausial menos eficiente de 1 copia que se está realizando):

std::vector <cv::Rect_<int>> v =   // this will copy
                            _imageGItem->_faceSampler._facesframe.value(frame);
std::vector <cv::Rect_<int>>::const_iterator it = v.begin();
if( it != v.end())
  {
    qDebug()<<"here";
  }

Otros consejos

QMap::value devuelve por valor. Lo que significa que es la devolución de un copia del vector, y por supuesto, su iterador que apunta a un vector diferente (una copia diferente).Cambiar el uso de la no-const versión operator[] en lugar de (como el const la versión de que también devuelve un valor).O, simplemente, utilizar std::map, que ofrece una mejor interfaz en este sentido.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top