Bit planes of a 1-plane image in OpenCV only work for 1/3 of the image
<p>I'm trying to learn OpenCV by doing a few things on my own. In this particular case, I wanted to take the bit planes of a grayscale image. The code seems to have worked, but it only works well for the bit 7 and 6, not so much for the remaining 6, as it only shows a good result for about 1/3 of the image. I just haven't found what's wrong with it as of yet. I'd greatly appreciate some help on the matter, as I'm just doing my first codes with the libraries.</p>

<p>Here's what I get for the first bit:</p>

<p><a href="https://i.stack.imgur.com/gQENX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gQENX.png" alt="enter image description here"></a></p>

<p>And here is it for the 7th bit:</p>

<p><a href="https://i.stack.imgur.com/Wnwj5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wnwj5.png" alt="enter image description here"></a></p>

<p>And here's my code:</p>

<pre><code>#include &lt;opencv2\opencv.hpp&gt;
#include &lt;math.h&gt;

using namespace cv;
using namespace std;

int main( int argc, char** argv ) {
   Mat m1 = imread("grayscalerose.jpg");
   imshow("Original",m1);
   int cols, rows, x, y;
   cols = m1.cols;
   rows = m1.rows;
   printf("%d %d \n",m1.rows,m1.cols);
   Mat out1(rows, cols, CV_8UC1, Scalar(0));
   out1 = (m1/128); //Here's where I divide by either 1,2,4,8,16,32,64, or 128 to get the corresponding bit planes

   for (int y = 0; y &lt; rows; y++){
        for (int x = 0; x &lt; cols; x++){
            out1.at&lt;uchar&gt;(y,x) = (out1.at&lt;uchar&gt;(y,x) % 2);
   } }

   out1 = out1*255;
   imshow("out1",out1);
   waitKey(0);
   destroyWindow( "out1" );

}
</code></pre>

<p>Thanks in advance. I hope my explanation wasn't too messy.</p>