Ask Your Question
2

how to compile forEach doc example?

asked 2016-10-25 04:17:47 -0500

LBerger gravatar image

updated 2016-10-25 04:18:42 -0500

Hi,

I want to try forEach method. I can compile and execute example using C++11 lambda. NowI want to try example given in doc. It is more readable than C++11 lambda I cannot compile this example :

Mat image(1920, 1080, CV_8UC3);
typedef cv::Point3_<uint8_t> Pixel;
// Parallel execution with function object.
struct Operator {
    void operator ()(Pixel &pixel, const int * position) {
        pixel.x = 255;
    }
};
image.forEach<Pixel>(Operator());

error message (windows 10 gcc version 6.2.0 (x86_64-posix-sjlj-rev1, Built by MinGW-W64 project)):

[In file included from F:/lib/opencv/modules/core/include/opencv2/core.hpp:3197:0,
                 from F:/lib/opencv/modules/imgproc/include/opencv2/imgproc.hpp:46,
                 from F:/lib/opencv/modules/imgproc/include/opencv2/imgproc/imgproc.hpp:48,
                 fromch3ex1/main.cpp:1:
F:/lib/opencv/modules/core/include/opencv2/core/utility.hpp: In instantiation of 'void cv::Mat::forEach_impl(const Functor&) [with _Tp = cv::Point3_<unsigned char>; Functor = main(int, char**)::Operator]':
F:/lib/opencv/modules/core/include/opencv2/core/mat.inl.hpp:1067:5:   required from 'void cv::Mat::forEach(const Functor&) [with _Tp = cv::Point3_<unsigned char>; Functor = main(int, char**)::Operator]'
ch3ex1/main.cpp:80:36:   required from here
F:/lib/opencv/modules/core/include/opencv2/core/utility.hpp:484:18: error: no match for call to '(const main(int, char**)::Operator) (cv::Point3_<unsigned char>&, int*)'
         operation(*reinterpret_cast<_Tp*>(0), reinterpret_cast<int*>(0));
         ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ch3ex1/main.cpp:76:14: note: candidate: void main(int, char**)::Operator::operator()(Pixel&, const int*) <near match>
         void operator ()(Pixel &pixel, const int * position) {
              ^~~~~~~~
ch3ex1/main.cpp:76:14: note:   passing 'const main(int, char**)::Operator*' as 'this' argument discards qualifiers
In file included from F:/lib/opencv/modules/core/include/opencv2/core.hpp:3197:0,
                 from F:/lib/opencv/modules/imgproc/include/opencv2/imgproc.hpp:46,
                 from F:/lib/opencv/modules/imgproc/include/opencv2/imgproc/imgproc.hpp:48,
                 from ch3ex1/main.cpp:1:
F:/lib/opencv/modules/core/include/opencv2/core/utility.hpp:497:9: warning: 'cv::Mat::forEach_impl(const Functor&)::PixelOperationWrapper::PixelOperationWrapper(cv::Mat_<_Tp>*, const Functor&) [with _Tp = cv::Point3_<unsigned char>; Functor = main(int, char**)::Operator]' used but never defined
         PixelOperationWrapper(Mat_<_Tp>* const frame, const Functor& _operation)
         ^~~~~~~~~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/ch3ex1.dir/build.make:64: CMakeFiles/ch3ex1.dir/main.cpp.obj] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/ch3ex1.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-10-25 04:28:57 -0500

berak gravatar image

updated 2016-10-25 04:36:08 -0500

same compiler / os here, it will compile, if you make your () operation const:


typedef cv::Point3_<uint8_t> Pixel;
// Parallel execution with function object.
struct Operator {
    void operator ()(Pixel &pixel, const int * position) const {
        pixel.x = 255;
    }
};

int main(int argc, char** argv) {
    Mat image(1920, 1080, CV_8UC3);
    image.forEach<Pixel>(Operator());
    return 0;
}
edit flag offensive delete link more

Comments

1

Thanks a lot and I have found another solution (just after writting my question..) :

void MyFunction(Pixel &pixel, const int * position) 
 {
     pixel.x = 255;
 }
int main(int argc, char* argv[])
{

    Mat img = imread("f:/images/20150618_185237.jpg", CV_LOAD_IMAGE_ANYCOLOR);
    img.forEach<Pixel>(&MyFunction);....

and I have got an answer to this question using forEach

LBerger gravatar imageLBerger ( 2016-10-25 04:45:56 -0500 )edit

Thank you @berak for your answer! Could you please tell me How to access the Pixel Index of the current pixel? So that i can Interpolate the current pixel with the neighboring pixels?

Balaji R gravatar imageBalaji R ( 2016-10-25 05:28:33 -0500 )edit
1

@Balaji, unfortunately, you cannot access any neighbour pixels using this construct. (already the underlying parallel_for implementation(over cols) forbids this).

can you ask a new question ? imho, it's interesting ;)

berak gravatar imageberak ( 2016-10-25 05:35:31 -0500 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2016-10-25 04:17:47 -0500

Seen: 511 times

Last updated: Oct 25 '16