Ilakkiya
Ilakkiya

Reputation: 107

OpenCV Error : Assertion failed when using fitLine

I want to use the fitLine function to come up with a line to draw on my source image src_crop. I load the frame in my main() and call the drawLine(). But the code aborts with the following error :

Code:

#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;

}

The code stops working at the point fitLine() is called. This I found by testing the code with printf statements.

Can anyone kindly help me solve the issue?

Upvotes: 0

Views: 600

Answers (1)

Miki
Miki

Reputation: 41765

Aside the fact the your code won't compile, the issue is that you're passing to fitLine the parameter outline as a vector<Vec4f>, while it should be a Vec4f.

Change outline declaration as:

Vec4f outline;

Upvotes: 2

Related Questions