Ask Your Question
0

Changing brightness and contrast cv2.cuda python

asked 2020-02-03 03:10:38 -0600

Imran B gravatar image

updated 2020-02-03 03:11:50 -0600

Hi all, I'm trying to modify brightness and contrast of an Image but it returns the error

cv2.error: OpenCV(4.2.0-dev) F:\Dev\Repos\opencv_fork_1\modules\core\src\matrix_wrap.cpp:359: error: (-213:The function/feature is not implemented) getGpuMat is available only for cuda::GpuMat and cuda::HostMem in function 'cv::_InputArray::getGpuMat'

my code,

import time, traceback
import numpy
import cv2 as cv
import numpy as np
img = cv.imread('test.jpeg')
raw = cv.cuda_GpuMat()
raw.upload(img)
alpha = 50
beta = 45
b = raw.convertTo(cv.CV_8U,alpha,beta)
img = b.download()
cv.imshow("Corrected",img)
edit retag flag offensive close merge delete

Comments

1

opencv version ? from where ? did you build it with CUDA support ?

(i don't think, there's any prebuilt cv2, that supports it)

berak gravatar imageberak ( 2020-02-03 03:14:53 -0600 )edit
1

I downloaded a built from https://jamesbowley.co.uk/downloads/ this site with cuda support opencv 4.2

Imran B gravatar imageImran B ( 2020-02-03 03:17:25 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-02-03 05:30:13 -0600

updated 2020-02-03 06:18:50 -0600

Because of the way the python bindings are generated, currently all CUDA functions (except cudacodec.nextFrame()) require at least one argument to be of type GpuMat(). If not the bindings will assume that the input (in your case raw) is a Mat, hence the error.

Anyway they are still a work in progress but in the meantime just pass in the dst array as shown below

sz = (1080,1920,3)
npMat = (np.random.random(sz)*255).astype(np.float)
cuMat = cv.cuda_GpuMat(npMat)
cuMat8U = cv.cuda_GpuMat(sz[0],sz[1],cv.CV_8UC3)
alpha = 50
beta = 45
b = cuMat.convertTo(cv.CV_8U,alpha,beta,cv.cuda.Stream_Null(),cuMat8U)

Note: If you don't pass in the dst arrays to OpenCV python functions, the functions will allocate a new array on every invocation. Therefore you probably always want to pre-allocate the dst arrays to avoid this allocation cost.

edit flag offensive delete link more

Comments

I got this error when I tried your solution,

cuMat8U = cv.cuda_GpuMat(img[:2],cv.CV_8UC3)
TypeError: Expected Ptr<cv::cuda::GpuMat::Allocator> for argument 'allocator'
Imran B gravatar imageImran B ( 2020-02-03 06:02:31 -0600 )edit

I have corrected a typo in my previous example.

You need to pre-allocate an empty array, in your example this would be

cuMat8U = cv.cuda_GpuMat(img.shape[0],img.shape[1],cv.CV_8UC3)

cudawarped gravatar imagecudawarped ( 2020-02-03 06:20:23 -0600 )edit

Your previous answer itself correct,I made a mistake in typo now corrected and executed it works.Thanks it's working good !!!!

Imran B gravatar imageImran B ( 2020-02-03 06:36:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-02-03 03:10:38 -0600

Seen: 1,320 times

Last updated: Feb 03 '20