Opecv Cuda GPU 影象處理 (計算結果與CPU不一樣)
阿新 • • 發佈:2021-01-26
技術標籤:Basler Cameraopencv
Opecv Cuda GPU 影象處理 (計算結果與CPU不一樣)
上程式碼
import cv2 import numpy as np gpuImg = cv2.cuda_GpuMat() def cv_show(name, image): cv2.namedWindow(name, cv2.WINDOW_NORMAL) cv2.imshow(name, image) cv2.waitKey(0) cv2.destroyAllWindows() def getGpuResize(src): global gpuImg basePixSize = 1280 height = src.shape[0] width = src.shape[1] print('height:', height) print('width:', width) largeSize = max(height, width) resizeRate = basePixSize/largeSize gpuImg.upload(src) gpuDst = cv2.cuda.resize(gpuImg, (int(width*resizeRate),int(height*resizeRate))) return gpuDst def getGpuCircles(gpuSrc,src): # gpuSrc = cv2.cuda.cvtColor(gpuSrc, cv2.COLOR_BGR2GRAY) ksize = (5,5) gpuFilter = cv2.cuda.createGaussianFilter(srcType=cv2.CV_8UC1, dstType=cv2.CV_8UC1, ksize=ksize, sigma1=0, sigma2=0) gpuSrc = cv2.cuda_Filter.apply(gpuFilter, gpuSrc) help(cv2.cuda.createHoughCirclesDetector) # ret, gpuSrc = cv2.cuda.threshold(gpuSrc, 100, 255, cv2.THRESH_BINARY) detector = cv2.cuda.createHoughCirclesDetector(dp=1, minDist=1000, cannyThreshold=110, votesThreshold=90, minRadius=400, maxRadius=510) # 100/110/50/445/510 or 150/110/50/430/510/MINRADIUS260 cuCircles = detector.detect(gpuSrc) circles = cuCircles.download() print("circles0:", circles) print(circles.shape) if circles is not None: # detect circles circles = np.uint0(np.around(circles)) print("circles1:", circles) for circle in circles[0, :]: dst = cv2.circle(src, (circle[0], circle[1]), circle[2], (255, 0, 255), 2) return dst if __name__ == '__main__': img = cv2.imread('./01.jpeg', cv2.IMREAD_GRAYSCALE) cuImg = getGpuResize(img) img = cuImg.download() # print(img.shape) # cv_show('img', img) dst = getGpuCircles(cuImg, img) cv_show('res', dst)
GPU 有沒有加速不知道,但是檢測的圓還是錯的,設定的引數根本沒有對結果產生約束。
老鐵,怎麼辦?
還是用CPU吧