title: Setting pixels values in OpenCV Python
body: <p>How fast change pixels values? In C# what i need to do is only use <code>GetPixel()</code> to get pixel value and <code>SetPixel()</code> to change it (its pretty easy to use but slow, MarshallCopy and Lock/UnlockBits is much faster). </p>
  
  <p>In this code, i marking black pixels as 1 and white pixels as 0</p>
  
  <pre><code>import tkFileDialog
  import cv2
  import numpy as np
  from matplotlib import pyplot as plt
  
  path = tkFileDialog.askopenfilename()
  bmp = cv2.imread(path) #reading image
  height, width, channels = bmp.shape
  
  if channels == 3:
      bmp = cv2.cvtColor(bmp, cv2.COLOR_BGR2GRAY) #if image have 3 channels, convert to BW
      bmp = bmp.astype('uint8')
  
  bmp = cv2.adaptiveThreshold(bmp,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
              cv2.THRESH_BINARY,11,2) #Otsu thresholding
  
  imageData = np.asarray(bmp) #get pixels values
  
  pixelArray = [[0 for y in range(height)] for x in range(width)] #set size of array for pixels
  
  for y in range(len(imageData)):
      for x in range(len(imageData[0])):
          if imageData[y][x] == 0:
              pixelArray[y][x] = 1 #if black pixels = 1
          else:
              pixelArray[y][x] = 0 #if white pixels = 0
  </code></pre>
  
  <p>In c#, it can looks like this:</p>
  
  <pre><code>for (y = 0; y &lt; bmp.Height-1; y++)
              {
                  for (x = 0; x &lt; bmp.Width-1; x++)
                  {
                      if (pixelArray[y, x] == 1)
                          newImage.SetPixel(x, y, Color.Black); //printing new bitmap
                      else
                          newImage.SetPixel(x, y, Color.White);
                  }
              }
              image2.Source = Bitmap2BitmapImage(newImage);
  </code></pre>
  
  <p>In the next step i will marking countour pixels as "2", but now i want to ask you, how to set new image in python from my specific value and then, display it? For experimental purpose, i want to invert image (from B&amp;W to W&amp;B) only by byte valuse. Can you help me how to do it?</p>
  
  <p><strong>EDIT1</strong></p>
  
  <p>I think i found a solution, but i have GREYSCALE image with one channel (i think thats how it works when i using <code>cv2.cvtColor</code> to convert 3 channels image to greyscale image). The function like this:</p>
  
  <pre><code>im[np.where((im == [0,0,0]).all(axis = 2))] = [0,33,166]
  </code></pre>
  
  <p>Could work pretty well, but how to make that function work with greyscale image? I want to set some black pixels (0) into White (255)</p>