Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for reading image as lines (then get a result of them)?

Is there a algorithm to get the line strokes of a image (ignore curves, circles, etc., everything will be treated as lines, but still similiar to vectors), from their pixels? Then get a result of them, like a Array?

This is how it'd basically work to read

Basic read

In this way, each row of pixel would be read as 1 horizontal line and I'd like to handle vertical lines also; but if there's a round fat line that takes more than 1 row

round fat line

it'll be considered one line. Its line width is the same height of pixels it has.

For instance, let's suppose we've a array containing rows of pixels in the (red, green, blue, alpha) format (JavaScript):

/* formatted ImageData().data */
[
    new Uint8Array([
        /* first pixel */
        255, 0, 0, 255,
        /* second pixel */
        255, 0, 0, 255
    ]),

    new Uint8Array([
        /* first pixel */
        0, 0, 0, 0,
        /* second pixel */
        0, 0, 0, 0
    ])
]

This would be a 2x2px image data, with a straight horizontal red line. So, from this array, I want to get a array containing data of lines, like:

[
    // x, y: start point
    // tx, ty: end point
    // w: line width

    // the straight horizontal red line of 1 pixel
    { x: 0, y: 0, tx: 2, ty: 0, w: 1, rgba: [255, 0, 0, 255] }
]

Note: I'd like to handle anti-aliasing.

This is my function to read pixels in the above format:

var getImagePixels = function(img){
    var canvas = document.createElement('canvas'),
        ctx = canvas.getContext('2d');

    canvas.width = img.width;
    canvas.height = img.height;

    ctx.drawImage(img, 0, 0);

    var imgData = ctx.getImageData(0, 0, img.width, img.height).data;
    var nImgData = [];
    var offWidth = img.width * 4;

    var dataRow = (nImgData[0] = new Uint8Array(offWidth));

    for (var b = 0, i = 0; b++ < img.height;) {
        nImgData[b] = new Uint8Array(offWidth);

        for (var arrI = 0, len = i + offWidth; i < len; i += 4, arrI += 4) {
            nImgData[b][arrI] = imgData[i];
            nImgData[b][arrI + 1] = imgData[i + 1];
            nImgData[b][arrI + 2] = imgData[i + 2];
            nImgData[b][arrI + 3] = imgData[i + 3];
        }
    }

    return nImgData;
};
like image 294
Klaider Avatar asked Aug 29 '16 21:08

Klaider


People also ask

What algorithm is used to detect the lines?

Thus, the Hough Transform algorithm detects lines by finding the (ρ, θ) pairs that have a number of intersections larger than a certain threshold.

What are the algorithm used in image processing?

Feature detection algorithmMarr–Hildreth algorithm: It is an early edge detection algorithm. Canny edge detector algorithm: Canny edge detector is used for detecting a wide range of edges in images. Generalized Hough transform algorithm. Hough transform algorithm.

How does Hough transform used for detecting straight lines?

The Hough transform takes a binary edge map as input and attempts to locate edges placed as straight lines. The idea of the Hough transform is, that every edge point in the edge map is transformed to all possible lines that could pass through that point.


1 Answers

You can find all lines using Hough transform. It will find only lines, no curves or circles. You may need to run edge detection before finding lines. Here is example: enter image description here enter image description here

Here you can find opencv example of implementation.

like image 53
taarraas Avatar answered Sep 18 '22 20:09

taarraas