I have used the below code to get the Hough lines. But I am not getting the expected output.
Here is the code:
const params = {
PROCESSING: {
name: 'PROCESSING',
dCoef: {
name: 'Downsample',
type: 'constant',
min: 1,
max: 4,
step: 1,
default: 2,
},
},
PCLINES: {
name: 'PC LINES',
count: {
name: 'Lines Count',
type: 'uniform',
min: 1,
max: 100,
step: 1,
default: 10,
},
layers: {
name: 'Layers Count',
type: 'constant',
min: 1,
max: 5,
step: 1,
default: 2,
},
},
};
const width = 500;
const heigth = 400;
// initialize WebRTC stream and session for runing operations on GPU
const stream = new gm.CaptureVideo(width, heigth);
const sess = new gm.Session();
const canvasProcessed = gm.canvasCreate(width, heigth);
// session uses a context for optimize calculations and prevent recalculations
// context actually a number which help algorythm to run operation efficiently
let frameNumber = 0;
// allocate memeory for storing a frame and calculations output
const input = new gm.Tensor('uint8', [heigth, width, 4]);
const context = { line: new gm.Line() }
// construct operation grap which is actially a Canny Edge Detector
let pipeline = gm.grayscale(input);
pipeline = gm.downsample(pipeline, 2, 'max');
pipeline = gm.gaussianBlur(pipeline, 3, 1);
pipeline = gm.dilate(pipeline, [3, 3]);
pipeline = gm.sobelOperator(pipeline);
pipeline = gm.cannyEdges(pipeline, 0.25, 0.75);
pipeline = gm.pcLinesTransform(pipeline, 3, 2, 2);
// initialize graph
sess.init(pipeline);
// allocate output
const output = gm.tensorFrom(pipeline);
// create loop
const tick = () => {
requestAnimationFrame(tick);
// Read current in to the tensor
stream.getImageBuffer(input);
//
const maxP = Math.max(input.shape[0], input.shape[1]);
let lines = [];
// session.runOp(operation, frame, output);
sess.runOp(pipeline, frameNumber, output);
gm.canvasFromTensor(canvasProcessed, input);
for (let i = 0; i < output.size / 4; i += 1) {
const y = ~~(i / output.shape[1]);
const x = i - (y * output.shape[1]);
const value = output.get(y, x, 0);
const x0 = output.get(y, x, 1);
const y0 = output.get(y, x, 2);
if (value > 0.0) {
lines.push([value, x0, y0]);
}
}
lines = lines.sort((b, a) => a[0] - b[0]);
// console.log(lines.length)
lines = lines.slice(0, 50);
// console.log(lines.length)
for (let i = 0; i < lines.length; i += 1) {
context.line.fromParallelCoords(
lines[i][1] * 1, lines[i][2] * 1,
input.shape[1], input.shape[0], maxP, maxP / 2,
);
// console.log(lines[i][1], lines[i][2]);
gm.canvasDrawLine(canvasProcessed, context.line, 'rgba(0, 255, 0, 1.0)');
}
frameNumber += 1;
}
function main() {
// start capturing a camera and run loop
stream.start();
tick();
document.body.children[0].appendChild(canvasProcessed);
}
main()
BTW what is the format of the output from PClinesTransform. My goal is to get the lines obtained from applying hough transform which lie in a certain angle range.
Thanks in advance.