Ask Your Question
1

Obtaining opencv UMat shape in python

asked 2018-03-22 12:42:06 -0500

MarekR gravatar image

Is there a direct way to obtain shape (number of cols and rows) of UMat array without converting it first to numpy object in Python?

edit retag flag offensive close merge delete

Comments

looking at

>>> help(cv2.UMat)

there clearly is no way to do so, without calling get() (which does a copy to a cpu numpy array)

but: whatever there is in your UMat, it has been a numpy array before, and you probably should have taken the shape from that earlier on.

berak gravatar imageberak ( 2018-03-22 12:51:43 -0500 )edit

Well, not exactly. There are some cases when shape of result is unknown. In my case it is result of function: cv2.findNonZero(). I have to repeat this function few hundred times per second and calling UMat.get() every time cost a lot. It is very inconvenient that UMat class is missing such basic method.

MarekR gravatar imageMarekR ( 2018-03-22 13:37:54 -0500 )edit

well, what about looking at the docs and safely assume:

src.shape == dst.shape

? (but again i do understand your issue here, it's somewhat inconsistent)

berak gravatar imageberak ( 2018-03-22 13:41:09 -0500 )edit
2

Quick test:

x =cv2.UMat(np.array( [1,2,3,0,0,0,1,2,0,0,2,3,0,0,5,0,0,5], dtype=np.uint8))
x.get().shape
(18, 1)
cv2.findNonZero(x).get().shape
(9, 1, 2)

src.shape == dst.shape is not true.

MarekR gravatar imageMarekR ( 2018-03-22 14:19:47 -0500 )edit

ok. your point ;)

berak gravatar imageberak ( 2018-03-23 10:40:01 -0500 )edit
LBerger gravatar imageLBerger ( 2019-03-03 08:08:29 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-03-22 21:37:03 -0500

supra56 gravatar image

updated 2018-03-22 21:39:06 -0500

Try This:

import cv2

img = cv2.UMat(cv2.imread("image.jpg", cv2.IMREAD_COLOR))
imgUMat = cv2.UMat(img)
gray = cv2.cvtColor(imgUMat, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 1.5)
gray = cv2.Canny(gray, 0, 50)

cv2.imshow("edges", gray)
cv2.waitKey()
edit flag offensive delete link more

Comments

@MarekR. U r missing something like,this cv2.UMat(img)

supra56 gravatar imagesupra56 ( 2018-03-22 21:38:46 -0500 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2018-03-22 12:42:06 -0500

Seen: 17,975 times

Last updated: Mar 22 '18