Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Parallel implementation of per pixel calculations

Hi, I am using OpenCV 3.0 .

In my serial implementation I use the Mat_::iterator quite often to access and edit each pixel of an image (As in the example below).

What is the best practice to accelerate these calculations ? Unfortunately I cannot find ways to access particular pixels in a UMat matrix.

Mat createFGMask(Mat &depthMap){
Mat fgMask = depthMap.clone();

// obtain iterator at initial position
Mat_<uchar>::iterator it= fgMask.begin<uchar>();
// obtain end position
Mat_<uchar>::iterator itend= fgMask.end<uchar>();

// loop over all pixels
for ( ; it!= itend; ++it) {
    //IF VALUE GREATER THEN 120, SET TO FOREGROUND ( ie. 1):
    if( (*it)> 120 ) (*it)=255;
    else (*it)= 0;
}

return fgMask;

}

Parallel implementation of per pixel calculations

Hi, I am using OpenCV 3.0 .

In my serial implementation I use the Mat_::iterator quite often to access and edit each pixel of an image (As in the example below).

What is the best practice to accelerate these calculations ? Unfortunately I cannot find ways to access particular pixels in a UMat matrix.

Mat createFGMask(Mat &depthMap){
Mat fgMask = depthMap.clone();

// obtain iterator at initial position
Mat_<uchar>::iterator it= fgMask.begin<uchar>();
// obtain end position
Mat_<uchar>::iterator itend= fgMask.end<uchar>();

// loop over all pixels
for ( ; it!= itend; ++it) {
    //IF VALUE GREATER THEN 120, SET TO FOREGROUND ( ie. 1):
    if( (*it)> 120 ) (*it)=255;
    else (*it)= 0;
}

return fgMask;

}

A more complex scenario where values of each pixel is required:

Mat pixelShifting(Mat &refImage, Mat &depthValue, ){

Mat warpedImage = Mat::zeros( 768, 1024, CV_8UC3 );

int height = refImage.rows;
int width = refImage.cols;

double newX, newY;
Mat newCoord;


for( int col = 0; col < refImage.cols; ++col ){
    for( int row = 0; row < refImage.rows; ++row ){

        //FIRST SHIFT BACKGROUND PIXELS:
        if( depthValue.at<uchar>(row,col) <= 120 ){

            newCoord = calcNewCoord(col, row, depthValues.at<double>(row,col));  // ALGORITHM THAT COMPUTES 
                                                                                        //    NEW PIXEL COORDINATES


            newX = roundCoord(newX);
            newY = roundCoord(newY);

            if( 0<= newY < height && 0<= newX < width ) {

                warpedImage.at<Vec3b>(newY, newX) = refImage.at<Vec3b>(row, col);

            }
        }
    }
}
return warpedImage;

}

This code basically takes 2 images, and populates a 3rd image with pixels of image1 by calculating new coordinates using values from image 2.

Parallel implementation of per pixel calculations

Hi, I am using OpenCV 3.0 .

In my serial implementation I use the Mat_::iterator quite often to access and edit each pixel of an image (As in the example below).

What is the best practice to accelerate these calculations ? Unfortunately I cannot find ways to access particular pixels in a UMat matrix.

Mat createFGMask(Mat &depthMap){
Mat fgMask = depthMap.clone();

// obtain iterator at initial position
Mat_<uchar>::iterator it= fgMask.begin<uchar>();
// obtain end position
Mat_<uchar>::iterator itend= fgMask.end<uchar>();

// loop over all pixels
for ( ; it!= itend; ++it) {
    //IF VALUE GREATER THEN 120, SET TO FOREGROUND ( ie. 1):
    if( (*it)> 120 ) (*it)=255;
    else (*it)= 0;
}

return fgMask;

}

A more complex scenario where values of each pixel is required:

Mat pixelShifting(Mat &refImage, Mat &depthValue, ){

Mat warpedImage = Mat::zeros( 768, 1024, CV_8UC3 );

int height = refImage.rows;
int width = refImage.cols;

double newX, newY;
Mat newCoord;


for( int col = 0; col < refImage.cols; ++col ){
    for( int row = 0; row < refImage.rows; ++row ){

        //FIRST SHIFT BACKGROUND PIXELS:
        if( depthValue.at<uchar>(row,col) <= 120 ){

            newCoord = calcNewCoord(col, row, depthValues.at<double>(row,col));   // ALGORITHM THAT COMPUTES 
                                                                                        //    NEW PIXEL COORDINATES
COORDINATES.
            newCoord = calcNewCoord(col, row, depthValues.at<double>(row,col));                                                                                  

            newX = roundCoord(newX);
newCoord.at<double>(0, 0);
            newY = roundCoord(newY);
newCoord.at<double>(1, 0);

            if( 0<= newY < height && 0<= newX < width ) {

                warpedImage.at<Vec3b>(newY, newX) = refImage.at<Vec3b>(row, col);

            }
        }
    }
}
return warpedImage;

}

This code basically takes 2 images, and populates a 3rd image with pixels of image1 by calculating new coordinates using values from image 2.