c++ - OpenCV 错误 : Assertion failed when using fitLine

标签 c++ opencv

我想使用 fitLine 函数在我的源图像 src_crop 上绘制一条线。我在 main() 中加载框架并调用 drawLine()。 但是代码中止并出现以下错误:

代码:

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

/// Global variables

Mat src_gray;
Mat src_crop;
Mat dst, detected_edges;

int edgeThresh = 1;
int lowThreshold = 27;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
char* window_name = "Edge Map";
int i,j;

void drawLine(int, void*)
{
   vector<Vec4f> outline;
   vector<Point2f> ssline;

   int flag2 = 0;


   /// Reduce noise with a kernel 3x3
   blur(src_gray, detected_edges, Size(3, 3));



   /// Canny detector
   Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size);

  /// Using Canny's output as a mask, we display our result
  dst.create(detected_edges.size(), detected_edges.type());
  dst = Scalar::all(0);
  src_crop.copyTo(dst, detected_edges);
  //namedWindow("Detected Edges", CV_WINDOW_AUTOSIZE);
  //imshow("Detected Edges", detected_edges);


  cvtColor(dst, dst, CV_BGR2GRAY);


for (j = 0; j < dst.cols; j++)
{
    for (i = 0; i < dst.rows; i++)
    {

        if (Scalar(dst.at<uchar>(i,j)).val[0] >= 90)
        {
            //cout << "Hi";
            flag2 = 1;
            break;
        }

    }
    if (flag2 == 1)
        break;
}
int k = j;
int l = i;


for (j = k; j < dst.cols; j++)
{ 

    Point2f ss = Point2f(l,j);
    ssline.push_back(ss);

}

fitLine(ssline, outline, CV_DIST_L1, 0, 0.01, 0.01);

//imshow("Result", src_crop);


}

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

    /// Load an image
    src = imread(s);


    if (!src.data)
    {
        return -1;
    }

    /// Create a matrix of the same type and size as src (for dst)
    //dst.create(src.size(), src.type());


    src_crop = src;
    /// Convert the image to grayscale
    cvtColor(src_crop, src_gray, CV_BGR2GRAY);


    /// Create a window
    namedWindow(window_name, CV_WINDOW_AUTOSIZE);

    /// Create a Trackbar for user to enter threshold
    createTrackbar("Min Threshold:", window_name, &lowThreshold, max_lowThreshold, drawLine);

    /// Show the image
    drawLine(0, 0);

    if (waitKey(30) >= 0) break;

    return 0;

}

代码在调用 fitLine() 时停止工作。这是我通过使用 printf 语句测试代码发现的。

谁能帮我解决这个问题?

最佳答案

撇开您的代码无法编译这一事实不谈,问题是您要传递给 fitLine参数 outline作为vector<Vec4f> , 虽然它应该是 Vec4f .

更改 outline声明为:

Vec4f outline;

关于c++ - OpenCV 错误 : Assertion failed when using fitLine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35780007/

相关文章:

python - OpenCV深度学习人脸检测断言函数 'cv::dnn::ConvolutionLayerImpl::getMemoryShapes'错误

c++ - 使用 boost::asio::generic::raw_protocol::socket 时 OS (Linux) 不添加第 2 层 header

c++ - 添加到 vector 的函数完成后, vector 失去对条目的引用

C++ 返回一个数组

当我不希望它被调用时,C++ 析构函数被调用

c++ - 如何在没有 'new' 的情况下将新对象实例添加到 std::list

opencv - OpenCV中像素值的上下限和类型是什么?

c# - 相机旋转 SolvePnp

python - 在忽略边界的颜色反卷积后识别正像素

c - “ Hello World ”视频程序不工作