本文介绍了OpenCV:将std :: vector传递给需要OutputArray的函数时,vec包含垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对需要OutputArray作为参数的OpenCV(3.1.0)函数有问题.

I have an issue with the OpenCV(3.1.0) functions that require a OutputArray as parameter.

当我将std::vec传递给此参数时,该向量包含垃圾,或者稍后使用该向量时程序甚至崩溃(我认为OpenCV会以某种错误的内存填充它……).传递cv::Mat作为参数而不是std::vec,它似乎可以正常工作.但是,它应同时适用于两种类型,如示例中所述文档.

When I pass a std::vec to this parameter, the vector contains garbage or the program even crashes when using the vector later(I suppose OpenCV somehow fills it with wrong memory...). Passing a cv::Mat as parameter instead of std::vec, it seems to work fine. However it should work with both types as noted in the example in the documentation.

这是一个带有输出的简单程序. VEC大小应为0,但是它是一些随机值.

Here is a simple program with its output. VEC size should be 0, however it is some random value.

#include <iostream>
#include <opencv2/opencv.hpp>

int main(int argc, char * argv[]) {

    std::cout << "OpenCV version : " << CV_VERSION << std::endl;

    cv::Mat referenceFrame = cv::Mat::zeros(10, 10, CV_8U);

    std::vector<cv::Point> nonZeroCoordinatesVec;
    cv::Mat nonZeroCoordinatesMat;
    cv::findNonZero(referenceFrame, nonZeroCoordinatesVec);
    cv::findNonZero(referenceFrame, nonZeroCoordinatesMat);

    std::cout << "VEC size: " << nonZeroCoordinatesVec.size() << std::endl;
    std::cout << "MAT size: " << nonZeroCoordinatesMat.total() << std::endl;

    return 0;
}

输出:

OpenCV version : 3.1.0
VEC size: 18446743853362306490
MAT size: 0

不仅是findNonZero函数失败,还有其他失败,例如使用SimpleBlobDetector查找关键点.

It's not only the findNonZero function that fails, but also other ones like finding keypoints with the SimpleBlobDetector.

使用的编译器:Visual Studio 2017附带的编译器

Compiler used: The one provided with Visual Studio 2017

推荐答案

我将发布库链接到调试构建库.链接调试库(world_310d.dll)而不是发布库(world_310.dll)可以解决此问题.

I linked the Release Libraries to the Debug Build Libraries. Linking the Debug library(world_310d.dll) instead of the Release library(world_310.dll) fixed it.

这篇关于OpenCV:将std :: vector传递给需要OutputArray的函数时,vec包含垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 04:10