Small Image Manipulation¶

Linux¶

It is very important that any program that you develop is first tested on very small images so that you can check the actual values computed by the algortihm for numerical correctness and accuracy. This is very diffuclt to achive with usual (large) images.

Test images may be created in python by explitily setting elements in an array with assignment statements or by equations. There are also gaphics programs that may be used for creating images; these are typically mroe useful for large test iamges. Presented here is a simple method of creating arbitrary small test images by using a text editor.

One method of creating a test image is to create a text file that looks like a 2D matrix and using a v4 utility vraw to convert that to a v4 format image.

The text "image" file may be creaeted in Jupyter Lab with the built-in text edior; in Linux any text editor such as gedit is fine. The format is a matrix of numerical values (space is the separator and each row must have the same number of pixel values).

You can use Linux commands cat or less to show the contents such a matrix:

Example:

In [1]:
! cat smallim.a
0 0 0 0 0 0 0  0 0 0 
0 0 1 1 0 1 2 200 0 0 
0 0 0 0 0 0 100 0 0 0 

In Linux you can generate a VisionX image from a text image

vrawtovx -t smallim.a | vdim -c of=smallim.vx

Display the VisionX image

vppr smallim.vx

In [3]:
! vppr smallim.vx
      0   1   2   3   4   5   6   7   8   9
  2   0   0   0   0   0   0   0   0   0   0
  1   0   0   1   1   0   1   2 200   0   0
  0   0   0   0   0   0   0 100   0   0   0

v4 commands:

Command Action
vrawtovx -t Convert an ASCII image into a VisionX image
vppr Print the contents of an image to the screen

Python¶

In Python a test visionx image file may be read directly

In [5]:
import numpy as np
from v4 import vx

smallim = vx.Vx('smallim.vx')
print (smallim.i)
[[  0   0   0   0   0   0   0   0   0   0]
 [  0   0   1   1   0   1   2 200   0   0]
 [  0   0   0   0   0   0 100   0   0   0]]

As an alternative, one can use the python vxsh function to avoid the explicit .vs file

In [6]:
smallim = vx.Vx()
exec(vx.vxsh( 'vrawtovx -t smallim.a | vdim -c of=$smallim' ));
print (smallim.i)
[[  0   0   0   0   0   0   0   0   0   0]
 [  0   0   1   1   0   1   2 200   0   0]
 [  0   0   0   0   0   0 100   0   0   0]]

For small image display use vd.dispsv

In [7]:
from v4 import vd
vd.dispsvx ('smallim.vx', capt='small image display from a file')
vd.dispsvx (smallim, capt='small image display from within python')
small image display from a file
<scaled size: (3 x 10) >
small image display from within python
<scaled size: (3 x 10) >

Numpy can also read text files

In [10]:
img = np.loadtxt('smallim.a', dtype='uint8')
vd.dispsvx (smallim, capt='small image display from an np array')
small image display from an np array
<scaled size: (3 x 10) >