peterbraden / node-opencv

OpenCV Bindings for node.js
MIT License
4.38k stars 857 forks source link

How do I use houghLinesP? #123

Closed zachrose closed 8 years ago

zachrose commented 10 years ago

I'm trying to figure some basic things about houghLinesP, both by poking around in a debugger and trying to read/understand the header files and Python/C++ APIs.

I can tell it's a method of a Matrix object. When I call it with no arguments, I get this error:

OpenCV Error: Assertion failed (src.type() == CV_8UC1) in equalizeHist, file /tmp/opencv-OOOq/opencv-2.4.8.2/modules/imgproc/src/histogram.cpp, line 3128
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv-OOOq/opencv-2.4.8.2/modules/imgproc/src/histogram.cpp:3128: error: (-215) src.type() == CV_8UC1 in function equalizeHist

Abort trap: 6

This error is Greek to me, unfortunately. Should I be using this method differently, or with arguments? Is there a way to read the OpenCV C++/Python API and know what the equivalent JavaScript method signature would look like? (I'm guessing it's more like the Python interface, except that it's a method of the image? Are there default parameters for rho, theta, and threshold?)

salmanulhaq commented 10 years ago

houghLinesP() expects the image to be grayscale (single channel) but what you're feeding it is a color image. This should have been handled within the code but is not (I'm updating it). What you need to do is call convertGrayscale() before calling houghLinesP(). Here's a quick example:

cv.readImage('/home/salman/image.jpg', function(err, img){

    img.convertGrayscale()
    var lines = img.houghLinesP()
})

Hope this helps.

peterbraden commented 10 years ago

The error messages are pretty bad, sorry about that.

danschultzer commented 8 years ago

@salmanulhaq has the correct solution. I'll close this for now.

pijaginw commented 7 years ago

Hi, I'm also trying to use houghLinesP() in my code (my goal is to detect lines from a stream of images) but nothing seems to work. Does the code which @salmanulhaq wrote should work? Or do I need to pass some additional arguments? Any help would be appreciated. Here's my simple code:

const cv = require('opencv');
cv.readImage('./car1.jpg', function (err, im) {
     if (err) {
        throw err;
    }

    const width = im.width();
    const height = im.height();
    if (width < 1 || height < 1) {
        throw new Error('Image has no size');
    }
    im.convertGrayscale();
    var lines = im.houghLinesP();

    // for (var i=0; i < lines.size(); i++) {
        // can I access the lines this way?
        // and then draw them using im.line()?
    // }
    im.save('./car1_res.jpg');
});
ghost commented 7 years ago

This took me a while to figure out so just in case someone else comes looking for how to draw the lines:

var outImage = new cv.Matrix(height, width);
for (var i = 0; i < lines.length; i++) {
    var line = lines[i];
    var pt1 = [line[0], line[1]];
    var pt2 = [line[2], line[3]];
    outImage.line(pt1, pt2, RED);
}  
outImage.save('tmp/whatever.jpg')