0

I have the following C++ code:

// Load image from vector
        cv::Mat image = imread(filenames[i]);

        //  option = black 
        if (arg == "black" | arg == "all") 
            {

            // Extracting colors - BGR (black backround)
            // Blue
            inRange(image, Scalar(190, 0, 0), Scalar(255, 50, 50), blue);
            // Green
            inRange(image, Scalar(0, 190, 0), Scalar(50, 255, 50), green);
            // Red
            inRange(image, Scalar(0, 0, 190), Scalar(50, 50, 255), red);
            // White
            inRange(image, Scalar(235, 235, 235), Scalar(255, 255, 255), white);
            // Silver
            inRange(image, Scalar(180, 180, 190), Scalar(200, 200, 220), silver);           

            // bitwise OR mask
            mask = blue|green|red|white|silver;

            }

        //  option = wooden 
        else
            {

            // Extracting colors - BGR (wooden backround)
            // Blue
            inRange(image, Scalar(190, 0, 0), Scalar(255, 50, 50), blue);
            // Green
            inRange(image, Scalar(0, 190, 0), Scalar(50, 255, 50), green);
            // Red
            inRange(image, Scalar(0, 0, 190), Scalar(50, 50, 255), red);
            // White
            inRange(image, Scalar(235, 235, 235), Scalar(255, 255, 255), white);
            // Silver
            inRange(image, Scalar(190, 190, 200), Scalar(220, 220, 230), silver);
            // Black
            inRange(image, Scalar(0, 0, 0), Scalar(40, 40, 40), black);


            // bitwise OR mask
            mask = blue|green|red|white|silver|black;


            }



        // Store mask points into a vector
        vector<Point> pts;
        findNonZero(mask, pts);

which I want to translate to Python. I am done with almost every part but not the bitwise OR mask part.

I tried the following:

mask = np.logical_or.reduce((mask == blue, mask == green, mask == red, mask == white, mask == silver))

however it did not work for me.

What's the Python equivalent of this operation?

4
  • As an aside if (arg == "black" | arg == "all") - you want ||, not |. Commented Aug 27, 2018 at 13:22
  • 2
    there is no logical or used in your c++ code, there is only bitwise or. So to do the same in python, you should use bitwise or there as well (which is |, just like in C++), not logical. Commented Aug 27, 2018 at 13:22
  • @Zinki thank you for your clarification, I corrected it in my post. So the syntax remains the same in Python as well? Commented Aug 27, 2018 at 13:24
  • Numpy has bitwise_or as well as logical_or. Commented Aug 27, 2018 at 13:27

1 Answer 1

2

there is no logical or used in your c++ code, there is only bitwise or. So to do the same in python, you should use bitwise or there as well (which is |, just like in C++), not logical.

So

        mask = blue|green|red|white|silver|black;

in C++ should be equivalent to

        mask = blue|green|red|white|silver|black

in python.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.