Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

asked 2017-06-16 01:44:27 -0600

Nbb gravatar image

How to use C-style scan to check pixel neighbors ?

Hey all,

I am trying to scan an image using the efficient C-style pointer access. For every pixel of a 16-bit unsigned image, i look at its right and bottom neighbor and perform some operations if they are not the same. The problem appears when the pixel is at the border of the image because the neighbor to its right l[j+1] is the first pixel of the next row.

http://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html#the-efficient-way

Is there a solution for this ?

int n_rows = label.rows;
int n_cols  = label.cols;

for (int i = 0; i < 1; ++i)
{
    ushort* l = label.ptr<ushort>(i);
    cv::Vec3b* c = color.ptr<cv::Vec3b>(i);
    for (int j = 0; j < n_cols * n_rows; ++j)
    {
        //labels
        int mid    = l[j], 
            right  = l[j + 1], 
            bottom = l[j + n_cols];

        //do something if (mid label != right label_ || (mid label != bottom label)
        if(mid != right) //do something
        if(mid != bottom) //do something
    }
}

How to use C-style scan to check pixel neighbors ?

Hey all,

I am trying to scan an image using the efficient C-style pointer access. For every pixel of a 16-bit unsigned image, i look at its right and bottom neighbor and perform some operations if they are not the same. The problem appears when the pixel is at the border of the image because the neighbor to its right l[j+1] is the first pixel of the next row.

http://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html#the-efficient-way

Is there a solution for this ?

int n_rows = label.rows;
int n_cols  = label.cols;

for (int i = 0; i < 1; ++i)
{
    ushort* l = label.ptr<ushort>(i);
    cv::Vec3b* c = color.ptr<cv::Vec3b>(i);
    for (int j = 0; j < n_cols * n_rows; ++j)
    {
        //labels
        int mid    = l[j], 
            right  = l[j + 1], 
            bottom = l[j + n_cols];

        //accumulate color
        cluster[mid].color += c[i];

        //do something if (mid label != right label_ || (mid label != bottom label)
        if(mid != right) //do something
 //increment_boundary(mid,right);
        if(mid != bottom) //do something
//increment_boundary(mid,bottom);
    }
}

How to use C-style scan to check pixel neighbors ?

Hey all,

I am trying to scan an image using the efficient C-style pointer access. For every pixel of a 16-bit unsigned image, i

1. look at its right and bottom neighbor and perform some operations if they are not the same. same.
2. accumulate the color information of the pixel i am at

The problem appears when the pixel is at the border of the image because the neighbor to its right l[j+1] is the first pixel of the next row.

http://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html#the-efficient-way

Is there a solution for this ?

int n_rows = label.rows;
int n_cols  = label.cols;

for (int i = 0; i < 1; ++i)
{
    ushort* l = label.ptr<ushort>(i);
    cv::Vec3b* c = color.ptr<cv::Vec3b>(i);
    for (int j = 0; j < n_cols * n_rows; ++j)
    {
        //labels
        int mid    = l[j], 
            right  = l[j + 1], 
            bottom = l[j + n_cols];

        //accumulate color
        cluster[mid].color += c[i];

        //do something if (mid label != right label_ || (mid label != bottom label)
        if(mid != right)  //increment_boundary(mid,right);
        if(mid != bottom) //increment_boundary(mid,bottom);
    }
}

How to use C-style scan to check pixel neighbors ?

Hey all,

I am trying to scan an image using the efficient C-style pointer access. For every pixel of a 16-bit unsigned image, i

1. look at its right and bottom neighbor and perform some operations if they are not the same.
2. accumulate the color information of the pixel i am at

The problem appears when the pixel is at the border of the image because the neighbor to its right l[j+1] is the first pixel of the next row.

http://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html#the-efficient-way

Is there a solution for this ?

int n_rows = label.rows;
int n_cols  = label.cols;

for (int i = 0; i < 1; ++i)
{
    ushort* l = label.ptr<ushort>(i);
    cv::Vec3b* c = color.ptr<cv::Vec3b>(i);
    for (int j = 0; j < n_cols * n_rows; ++j)
    {
        //labels
        int mid    = l[j], 
            right  = l[j + 1], 
            bottom = l[j + n_cols];

        //accumulate color
        cluster[mid].color += c[i];

        //do something if (mid label != right label_ || (mid label != bottom label)
        if(mid != right)  //increment_boundary(mid,right);
        if(mid != bottom) //increment_boundary(mid,bottom);
    }
}

This is the unoptimized version

    //label = ushort, color = cv::Vec3b
    cv::Mat label, color;

    //fill up color and label

    //pad label
    cv::copyMakeBorder(label, label_padded, 0, 2, 0, 2, cv::BORDER_REPLICATE, BACKGROUND);

    //use dimensions of unpadded image
    int n_rows = label.rows,
        n_cols = label.cols;

    for(int row = 0; row < n_rows; row++)
    for(int col = 0; col < n_cols; col++)
    {
        //labels
        int mid    = label_padded.at<ushort>(row, col),
            right  = label_padded.at<ushort>(row, col + 1),
            bottom = label_padded.at<ushort>(row + 1, col);

        //update cluster statistics
        //cluster[mid] += color.at<cv::Vec3b>(row, col);

        if(mid != right) //update boundary
        if(mid != bottom) //update boundary
    }

How to use C-style scan to check pixel neighbors ?

Hey all,

I am trying to scan an image using the efficient C-style pointer access. For every pixel of a 16-bit unsigned image, i

1. look at its right and bottom neighbor and perform some operations if they are not the same.
2. accumulate the color information of the pixel i am at

The problem appears when the pixel is at the border of the image because the neighbor to its right l[j+1] is the first pixel of the next row.

http://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html#the-efficient-way

Is there a solution for this ?

int n_rows = label.rows;
int n_cols  = label.cols;

for (int i = 0; i < 1; ++i)
{
    ushort* l = label.ptr<ushort>(i);
    cv::Vec3b* c = color.ptr<cv::Vec3b>(i);
    for (int j = 0; j < n_cols * n_rows; ++j)
    {
        //labels
        int mid    = l[j], 
            right  = l[j + 1], 
            bottom = l[j + n_cols];

        //accumulate color
        cluster[mid].color += c[i];

        //do something if (mid label != right label_ || (mid label != bottom label)
        if(mid != right)  //increment_boundary(mid,right);
        if(mid != bottom) //increment_boundary(mid,bottom);
    }
}

This is the unoptimized version

    //label = ushort, color = cv::Vec3b
    cv::Mat label, color;

    //fill up color and label

    //pad label
    cv::copyMakeBorder(label, label_padded, 0, 2, 0, 2, cv::BORDER_REPLICATE, BACKGROUND);
cv::BORDER_REPLICATE);

    //use dimensions of unpadded image
    int n_rows = label.rows,
        n_cols = label.cols;

    for(int row = 0; row < n_rows; row++)
    for(int col = 0; col < n_cols; col++)
    {
        //labels
        int mid    = label_padded.at<ushort>(row, col),
            right  = label_padded.at<ushort>(row, col + 1),
            bottom = label_padded.at<ushort>(row + 1, col);

        //update cluster statistics
        //cluster[mid] += color.at<cv::Vec3b>(row, col);

        if(mid != right) //update boundary
        if(mid != bottom) //update boundary
    }