cv.Save and cv.Load (array) - arrays
I need to save and load an array, but I get this error:
cv.Save('i.xml',i)
TypeError: Cannot identify type of 'structPtr'
This is the code:
import cv
i = [[1,2],[3,4],[5,6],[7,8]]
cv.Save('i.xml',i)
That's because cv.Save needs to receive the object to be stored in the file as an OpenCV object. For example, the following is a minimal workable example that saves a numpy array in a file using cv.Save:
import cv2
import numpy as np
i = np.eye(3)
cv2.cv.Save('i.xml', cv2.cv.fromarray(i))
As you can see here, arrays should be converted back to numpy from OpenCV as well after reading.
Regards.
Related
Rasterio Creating TIFF file
I tried to use the code on the Rasterio-doc website to write an array as a TIFF to disk https://rasterio.readthedocs.io/en/latest/topics/writing.html with rasterio.Env(): profile = src.profile profile.update( dtype=rasterio.uint8, count=1, compress='lzw') with rasterio.open('example.tif', 'w', **profile) as dst: dst.write(array.astype(rasterio.uint8), 1) When I run the code the following error occurs: 'name 'array' is not defined'. I tried in the last line with 'np.array' instead of 'array' to say that it is a numpy-array but it didn't worked.
The variable 'array' stands for the data that should be written to disk. Create a numpy array as for example: import numpy as np array = np.array(my_array_data) Then you can write this data to disk as described in the tutorial.
Pycharm typehint for Numpy array of objects
I have multiple numpy arrays with different objects. How can I give 'type-hint' to the Pycharm so that I can get methods of that class while coding? For example, import numpy as np class Test: def do_task(): pass data = [Test(), Test(), Test()] my_array = np.array(data, dtype=Test) def test_hint(array:np.ndarray): # <-- I can give typehint as array but I also want to give hint about what kind of array it is. for a in array: a.... # Here I want Pycharm to give me list of methods from 'Test' class I can always explicitly mention what kind of object it is like following, for a in array: temp = a # type: Test temp... # Here Pycharm will give me all the suggestions. Is there any way I can provide type-hint in the constructor so that I do not need to write additional line of code to declare type of data? I tried looking at python's type module for some clue, but couldn't find any.
Converting RGB data into an array from a text file to create an Image
I am trying to convert txt RGB data from file.txt into an array. And then, using that array, convert the RGB array into an image. (RGB data is found at this github repository: IR Sensor File.txt). I am trying to convert the .txt file into an array which I could use the PIL/Image library and convert the array into an Image, and then put it through the following script to create my image. My roadblock right now is converting the arrays in file.txt into an appropriate format to work with the Image function. from PIL import Image import numpy as np data = [ARRAY FROM THE file.txt] img = Image.fromarray(data, 'RGB') img.save('my.png') img.show() The RGB data looks like as follows, and can also be found at the .txt file from that github repository linked above: [[(0,255,20),(0,255,50),(0,255,10),(0,255,5),(0,255,10),(0,255,25),(0,255,40),(0,255,71),(0,255,137),(0,255,178),(0,255,147),(0,255,158),(0,255,142),(0,255,163),(0,255,112),(0,255,132),(0,255,137),(0,255,153),(0,255,101),(0,255,122),(0,255,122),(0,255,147),(0,255,66),(0,255,66),(0,255,30),(0,255,61),(0,255,0),(0,255,0),(0,255,40),(0,255,66),(15,255,0),(0,255,15)], [(0,255,40),(0,255,45),(15,255,0),(20,255,0),(10,255,0),(35,255,0),(0,255,5),(0,255,56),(0,255,173),(0,255,168),(0,255,153),(0,255,137),(0,255,158),(0,255,147),(0,255,127),(0,255,117),(0,255,142),(0,255,142),(0,255,122),(0,255,122),(0,255,137),(0,255,137),(0,255,101),(0,255,66),(0,255,71),(0,255,61),(0,255,25),(0,255,25),(0,255,61),(0,255,35),(0,255,0),(35,255,0)], [(0,255,15),(0,255,25),(51,255,0),(71,255,0),(132,255,0),(101,255,0),(35,255,0),(0,255,20),(0,255,91),(0,255,153),(0,255,132),(0,255,147),(0,255,132),(0,255,158),(0,255,122),(0,255,132),(0,255,142),(0,255,158),(0,255,122),(0,255,137),(0,255,142),(0,255,147),(0,255,101),(0,255,101),(0,255,86),(0,255,86),(0,255,50),(0,255,45),(0,255,50),(0,255,56),(0,255,30),(56,255,0)], [(0,255,45),(0,255,10),(76,255,0),(127,255,0),(132,255,0)]]
I think this should work - no idea if it's decent Python: #!/usr/local/bin/python3 from PIL import Image import numpy as np import re # Read in entire file with open('sensordata.txt') as f: s = f.read() # Find anything that looks like numbers l=re.findall(r'\d+',s) # Convert to numpy array and reshape data = np.array(l).reshape((24,32,3)) # Convert to image and save img = Image.fromarray(data, 'RGB') img.save('result.png') I enlarged and contrast-stretched the image subsequently so you can see it!
IdentationError in python when using RBF
#1-d interpolation import numpy as np from scipy import interpolate import pylab as py import pandas as pd #Read the Dataset from Excel File def func(x1): return x1*np.exp(-5.0*x1**2) dataset=pd.read_excel('Messwerte_FIBRE1.xlsx') dataset=dataset.drop([0]) index=[1] index2=[9] x=dataset.iloc[:, index] y=dataset.iloc[:, index2] x1=np.array(x,dtype=float) y1=np.array(y,dtype=float) fvals=func(x1) xnew=np.linespace(430,490,800) for kind in ['multiquadric','inverse multiquadric','gaussian', 'linear','cubic','quintic','thin-plate']: newfunc=interpolate.Rbf(x1,fvals,function=kind) fnew=newfunc(xnew) I am getting an error: IndentationError: expected an indented block** Can any1 help me in fixation? I am trying to read variables from excel file and using RBF interpolation technique for prediction estimation My excel file looks like this,please click on itMesswerte_FIBRE1.xlsx
Right here the for loop expects code to be in the block. xnew=np.linespace(430,490,800) for kind in ['multiquadric','inverse multiquadric','gaussian', 'linear','cubic','quintic','thin-plate']: #SOMETHING WOULD GO HERE newfunc=interpolate.Rbf(x1,fvals,function=kind) fnew=newfunc(xnew)
Convert numpy array to MemoryView object
I'm trying to convert a numpy array to a MemoryView object because I have to communicate between two programs. The one can only handle NumPy arrays and the other only MemoryView objects. Converting from MemoryView to numpy array is easily done by: import numpy as np MyNumpyArray=np.array(MyMemoryView) But how do you convert from numpy array to MemoryView? I found here: https://docs.python.org/3/c-api/memoryview.html That there's a PyMemoryView_FromObject(PyObject *obj) function, but I don't know how to call it without an example. Thanks!
memoryview is one of the built-in types and can simply be called as: arr = np.random.rand(5,4) view = memoryview(arr) view <memory at 0x12699c318>
Additionally to accepted answer providing another simple method to get memoryview out of Numpy array: Try it online! a = np.arange(1, 9) view = a.data print(type(view)) # <class 'memoryview'> In other words .data attribute of array variable gives exactly memoryview.