Closed zachrose closed 8 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.
The error messages are pretty bad, sorry about that.
@salmanulhaq has the correct solution. I'll close this for now.
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');
});
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')
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:
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?)