OpenCV Error: Assertion failed (a_size.width == len)

OpenCV Error: Assertion failed (a_size.width == len)

背景

我目前正在尝试在我的 Rapsberry Pi 上使用 ROS 构建一个自主无人机,它是 运行 一个 Ubuntu MATE 16.04 LTS。目前解决了识别红色圆圈的计算机视觉问题。由于本质上,无人机不稳定(因为有一个内部 PID 控制器稳定无人机)并且由于光照条件,无人机经常检测到相同的圆,但方式非常不稳定。为了解决这个问题,[this][1] 问题的评论建议我尝试视频稳定。

错误

这是我目前正在查看的错误:

OpenCV Error: Assertion failed (a_size.width == len) in gemm, file
/tmp/binarydeb/ros-kinetic-opencv3-3.1.0/modules/core/src/matmul.cpp,
line 900 terminate called after throwing an instance of
'cv::Exception'   what(): 
/tmp/binarydeb/ros-kinetic-opencv3-3.1.0/modules/core/src/matmul.cpp:900:
error: (-215) a_size.width == len in function gemm

代码

class Tracker {

    cv::Mat prevGray;
    vector<cv::Point2f> trackedFeatures;

    void processImage(const sensor_msgs::ImageConstPtr& msg) {

        // ROS declarations 
        cv_bridge::CvImagePtr cv_ptr;

        // Function Initializations
        if (freshStart == true) {
            cv::Mat rightTransform = cv::Mat::eye(3,3,CV_32FC1);
        }
        cv::Mat gray; 
        cv::Mat copy_img;
        vector<cv::Point2f> corners;

        try { 
             cv_ptr = cv_bridge::toCvCopy(msg, 
                        sensor_msgs::image_encodings::BGR8);
        } 
        catch (cv_bridge::Exception& e) {
            ROS_INFO("cv_bridge exception");
            return;
        }

        copy_img = cv_ptr->image;
        cv::cvtColor(cv_ptr->image, gray, cv::COLOR_BGR2GRAY);

        if (trackedFeatures.size() < 200) {
            cv::goodFeaturesToTrack(gray,corners,200,0.01,10);
            for (int i = 0; i < corners.size(); ++i) {
                trackedFeatures.push_back(corners[i]);
            }
        }

        if (!prevGray.empty()) {
            vector<uchar> status;
            vector<float> errors; 

            calcOpticalFlowPyrLK(prevGray, gray, trackedFeatures, corners,
                    status, errors, Size(10,10));

            if (countNonZero(status) < status.size() * 0.8) {
                cout << "cataclysmic error\n";
                rigidTransform = cv::Mat::eye(3,3,CV_32FC1);
                trackedFeatures.clear();
                prevGray.release();
                freshStart = true;
                return;
            } else {
                freshStart = false;
            }

            cv::Mat_<float> newRigidTransform = estimateRigidTransform(
                    trackedFeatures, corners, false);
            cv::Mat_<float> nrt33 = cv::Mat_<float>::eye(3,3);
            newRigidTransform.copyTo(nrt33.rowRange(0,2));
            rigidTransform *= nrt33;

            trackedFeatures.clear();
            for (int i = 0; i < status.size(); ++i) {
                if (status[i]) {
                    trackedFeatures.push_back(corners[i]);
                }
            }
        }

        // Debugging to see the tracked features as of now
        for (int i = 0; i < trackedFeatures.size(); ++i) {
            circle(cv_ptr->image, trackedFeatures[i], 3, Scalar(0,0,255), 
                CV_FILLED);
        }

        imshow(OPENCV_WINDOW, cv_ptr->image); 
        cv::waitKey(3);

        gray.copyTo(prevGray);
    }
};

现在我知道错误出在语句gray.copyTo(prevGray)上了。这是因为当我注释掉它时我没有得到任何错误。

程序的总体结构

class Tracker {

    // The ROS declarations 
    ...

    // Internal Declarations
    ...

    public:
    bool freshStart;
    Mat_<float> rigidTransform;
    ...

    Tracker():it_(nh1_) {
        image_sub_ = it_.subscribe("/ardrone/bottom/image_raw", 1,
                                        &Tracker::processImage, this);
        image_pub_ = it_.advertise("/stabImage", 1);

        cv::namedWindow(OPENCV_WINDOW);
    }

    ~Tracker() {
        cv::destroyWindow(OPENCV_WINDOW);
    }

    void processImage(const sensor_msgs::ImageConstPtr& msg) {
    ...
    }

int main(int argc, char** argv)
{
  ros::init(argc, argv, "video_stabilizer");
  Tracker tr;
  ros::spin();
  return 0;
}

问题好像是matmul的问题。你在哪里初始化rigidTransform?

我认为不是这条线:

cv::Mat rightTransform = cv::Mat::eye(3,3,CV_32FC1);

你需要:

rigidTransform = cv::Mat::eye(3,3,CV_32FC1);

当您注释掉 gray.copyTo(prevGray) 时问题不会发生,因为如果没有它,循环 if if (!prevGray.empty()) 不会 运行.