Ask Your Question
0

Efficient pointer-scanning on image

asked 2017-04-27 06:52:57 -0600

Wurstpelle gravatar image

updated 2017-04-27 07:06:07 -0600

Hi everyone, I´m new in C++ and OpenCv and have a little problem with scanning an image with pointers.

I want to switch my running (slowly) Source into faster code by using pointers instead of the iteration through the loop.

The actual code is listed below:

for (int i = 0; i < Image.rows; i++)
 {
    for (int j = 0; j < Image.cols; j++)
     {
       int y = x - Image.at<cv::Vec3b>(i, j)[2];

       if ( (y - Image.at<cv::Vec3b>(i, j)[1] < Threshold1) && (x - Image.at<cv::Vec3b>(i, j)[0] < Threshold2))
           {
               BinaryImage.at<cv::Vec3b>(i, j)[0] = 255;
               BinaryImage.at<cv::Vec3b>(i, j)[1] = 255;
               BinaryImage.at<cv::Vec3b>(i, j)[2] = 255;                            
            }
      }
    }

I´m not sure how to replace the part for computing the y-value and the if-line:

 int y = x - Image.at<cv::Vec3b>(i, j)[2];

and

if ( (y - Image.at<cv::Vec3b>(i, j)[1] < Threshold1) && (x - Image.at<cv::Vec3b>(i, j)[0] < Threshold2))

My first solution doesn´t work.

 uchar* ptr_Image = Image.ptr<uchar>(i);
uchar* ptr_BinaryImage = BinaryImage.ptr<uchar>(i);
    for (int j = 0; j < Image.cols; ++j)
     {
        int y = x - *ptr_Image+2;

    if ( (y - *ptr_Image++ < Threshold1) && (x - *ptr_Image < Threshold2))
         {   *ptr_BinaryImage++ = 255;
             *ptr_BinaryImage++ = 255;
             *ptr_BinaryImage++ = 255;
          }
     }

I know, that an binary-image is ordinarily type CV_8U not CV_8UC3 but I need this with three channels ;)

Thank you!

Daniel S.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2017-04-27 07:53:37 -0600

LBerger gravatar image

I think it is better like this :

uchar* ptr_Image = Image.ptr<uchar>(i);
uchar* ptr_BinaryImage = BinaryImage.ptr<uchar>(i);
for (int j = 0; j < Image.cols; ++j,ptr_image++)// three channel = 3 X ptrchannel++
 {
    int y = x - *(ptr_Image+2);// parenthesis i don't remmber priority order

if ((x - *ptr_Image++ < Threshold2) && (y - *ptr_Image++ < Threshold1) && ) // right order here
     {   *ptr_BinaryImage++ = 255;
         *ptr_BinaryImage++ = 255;
         *ptr_BinaryImage++ = 255;
      }
 }

Ok now you can try to use mask :

vector<Mat> chImage(3);
split(Image,chImage);
Mat y=x-chImage[2];
Mat z= y-chImage[1];
Mat mask=(z < Threshold1) && (x - chImage[0]< Threshold2);

BinaryImage(mask)=Vec3b(255,255,255);

I havent check my answer

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-04-27 06:52:57 -0600

Seen: 550 times

Last updated: Apr 27 '17