title: Reading a text file with OpenCV in Python
body: <p>I have tens of thousands of text files to analyze, where each text file represents a snapshot in time of the physical state of a system. The micro-state of each "pixel" is represented by floats from 0 to 1. <strong>Is it possible for OpenCV to directly read a text file without first having to convert the text file to an image format?</strong> I do not want to create tens of thousands of image files every time I carry out this analysis.</p>
  
  <p><strong>Context/goal:</strong> I am analyzing a thermal simulation of a nano-magnetic system, and will eventually need to use OpenCV to calculate the contour areas of clusters formed above a certain threshold value.</p>
  
  <p>I've included my code attempt below, using a test text file. The system is a square system of side length 40, and I am analyzing the column of 40^2 = 1600 data points which I call <em>mag</em> (for magnetization, as this is from a scientific research project). I multiply each "pixel" by 255 to mimic grayscale. As soon as the program reaches the cv2.threshold line, I get an error: </p>
  
  <blockquote>
    <p>~/anaconda/conda-bld/work/opencv-2.4.8/modules/imgproc/src/thresh.cpp:783: error: (-210) in function threshold</p>
  </blockquote>
  
  <p>which I suspect arises from my mimicking grayscale instead of reading an actual grayscale image file.</p>
  
  <pre><code>import numpy as np
  import cv2
  SideDim = 40
  
  dud, mag = np.loadtxt('Aex_testfile.txt', unpack=True, usecols=(4,5), skiprows=2)
  
  mag = np.reshape(mag, (SideDim,SideDim))
  
  for row in range(SideDim):
      for col in range(SideDim):
          mag[row][col] = round(255 * mag[row][col])
  mag = mag.astype(np.int)
  
  ret,thresh = cv2.threshold(mag,0,255,cv2.THRESH_BINARY)
  plt.imshow(thresh,'gray')
  </code></pre>