question concerning asMat

7 views
Skip to first unread message

stefan

unread,
Jul 13, 2010, 2:05:55 PM7/13/10
to ctypes-opencv
Hi,
I have a question about the asMat() function. Look at the following
code:

import pyopencv as cv
import numpy as np

data = np.array((1,2,3,4))
data.resize(2,2)
print data.shape #this gives (2, 2)
cvData = cv.asMat(data)
print cvData.rows, cvData.cols #this gives 1,2 shouldn´t this be also
2,2
print cvData.ndarray.shape #this gives (1, 2, 2) why 3d?
data = np.transpose(data)
cvData = cv.asMat(data) # this gives the following exception:

Traceback (most recent call last):
File "C:\Dokumente und Einstellungen\stefan\workspace\test\src
\test.py", line 12, in <module>
cvData = cv.asMat(data)
File "C:\Python26\lib\site-packages\pyopencv\core.py", line 1886, in
asMat
out_mat =
eval("_PE.asMat(inst_vector_Type=vector_Type.fromlist(obj))" .replace("Type",
z.__class__.__name__))
File "<string>", line 1, in <module>
NameError: name 'vector_ndarray' is not defined

Thanks for your help
stefan



Minh-Tri Pham

unread,
Jul 13, 2010, 2:19:22 PM7/13/10
to ctypes...@googlegroups.com
Hi stefan,

Please check the docstring as well as previous discussions. This has been
discussed before. Basically, asMat() needs the user to tell if they want a
single-channel or a multi-channel Mat instance. Try asMat(data, True) if you
want a single-channel Mat instance.

As for the last error, I will take a look. It seems to be a bug. Thanks for
raising. :)

Cheers,
Minh-Tri

--
Dr. Minh-Tri Pham
Research Fellow, University of Surrey, UK
Email: t.p...@surrey.ac.uk or pmt...@gmail.com
Mob: +44 (0) 778 774 1089 | Tel: +44 (0) 148 368 4711

stefan

unread,
Jul 14, 2010, 2:00:27 AM7/14/10
to ctypes-opencv
Thanks,
I didn't think of rgb channels. Sorry for this. Its to hot for
programming in Germany at the moment ;-)
However another question. I tried this:

data = np.zeros((1000, 1))
cvData = cv.asMat(data, True)
print data.shape
print cvData.size()
data = np.transpose(data)
cvData = cv.asMat(data, True)
print data.shape
print cvData.size()

The output is:
(1000, 1)
Size2i(width=1000, height=1)
(1, 1000)
Size2i(width=1000, height=1)

Is this right?
stefan
> Email: t.p...@surrey.ac.uk or pmtr...@gmail.com
> Mob: +44 (0) 778 774 1089 | Tel: +44 (0) 148 368 4711- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Guy K. Kloss

unread,
Jul 14, 2010, 4:05:03 AM7/14/10
to ctypes...@googlegroups.com
On Wed, 14 Jul 2010 18:00:27 stefan wrote:
> Sorry for this. Its to hot for programming in Germany at the moment ;-)

Lucky one. I want it to become finally spring here again. Sunny, but cold
right now here (~10 degrees during the day).

> However another question. I tried this:

I suppose that cv.asMat is returning a NumPy ndarray object, that is accessing
the native OpenCV data block referenced from its array structure. NumPy does
go about many things by recording the data arrangement *without* performing
actual data operations on the data block if not necessary. So I'd expect that
cvData.shape returns the current *representation* of the array, whereas
cvData.size() will probably give you information on the physical layout of the
memory block.

But that's just guesswork, but based on common practice in NumPy.

HTH.

Viele Gruesse in den Sommer,

Guy

--
Guy K. Kloss
Institute of Information and Mathematical Sciences
Te Kura Pūtaiao o Mōhiohio me Pāngarau
Room 2.63, Quad Block A Building
Massey University, Auckland, Albany
Private Bag 102 904, North Shore Mail Centre
voice: +64 9 414-0800 ext. 9266 fax: +64 9 441-8181
eMail: G.K...@massey.ac.nz http://iims.massey.ac.nz

signature.asc

Minh-Tri Pham

unread,
Jul 16, 2010, 3:32:10 AM7/16/10
to ctypes...@googlegroups.com
Yeah, Guy's guess is correct. NumPy is very flexible. The endianness of the
sequence of dimensions can be changed by flipping one bit and that is what
happens when a 2D ndarray is transposed. My conversion code, unfortunately,
does not match the the endianness correctly between a dense array in OpenCV
(e.g. CvMat, CvMatND) and an ndarray. It always assumes the ndarray is
little-endian. I am going to fix the bug soon.

Cheers,
Minh-Tri

On 7/14/2010 9:05 AM, Guy K. Kloss wrote:
> On Wed, 14 Jul 2010 18:00:27 stefan wrote:
>> Sorry for this. Its to hot for programming in Germany at the moment ;-)
>
> Lucky one. I want it to become finally spring here again. Sunny, but cold
> right now here (~10 degrees during the day).
>
>> However another question. I tried this:
>
> I suppose that cv.asMat is returning a NumPy ndarray object, that is accessing
> the native OpenCV data block referenced from its array structure. NumPy does
> go about many things by recording the data arrangement *without* performing
> actual data operations on the data block if not necessary. So I'd expect that
> cvData.shape returns the current *representation* of the array, whereas
> cvData.size() will probably give you information on the physical layout of the
> memory block.
>
> But that's just guesswork, but based on common practice in NumPy.
>
> HTH.
>
> Viele Gruesse in den Sommer,
>
> Guy
>

--

Dr. Minh-Tri Pham
Research Fellow, University of Surrey, UK

Email: t.p...@surrey.ac.uk or pmt...@gmail.com

Reply all
Reply to author
Forward
0 new messages