def vglCheckContext(img, context): if (not vglIsContextUnique(context)): print("vglCheckContext: Error: context =", context, "is not unique or invalid") exit() if (vglIsInContext(img, context)): print("vglCheckContext: image already in context", context) return context """ HERE STARTS THE CASE-LIKE SEQUENCE FROM vglContext.vglCheckContext(VglImage img, int context) """ # IF THE CONTEXT IS IN RAM if (context is vl.VGL_RAM_CONTEXT()): # AND IS A BLANK IMAGE, IT IS ON RAM. JUST SET IT IN CONTEXT. if (vglIsInContext(img, vl.VGL_BLANK_CONTEXT())): vglAddContext(img, vl.VGL_RAM_CONTEXT()) # AND THE CONTEXT IS IN CL-DEVICE, DOWNLOAD IT BACK TO RAM. if (vglIsInContext(img, vl.VGL_CL_CONTEXT())): vl.vglClDownload(img) # IF THE CONTEXT IS IN CL-DEVICE elif (context is vl.VGL_CL_CONTEXT()): # AND IS A BLANK-IMAGE, IT IS ON RAM. UPLOAD IT TO CL-DEVICE! if (vglIsInContext(img, vl.VGL_BLANK_CONTEXT())): vl.vglClUpload(img) # AND THE CONTEXT IS IN RAM, UPLOAD IT TO CL-DEVICE! if (vglIsInContext(img, vl.VGL_RAM_CONTEXT())): vl.vglClUpload(img) else: print("vglCheckContext: Error: Trying to copy to invalid context =", context) exit() return img.inContext
def vglLoadImage(img, filename=""): if( img.filename == "" ): if( filename == "" ): print("vglImage: vglLoadImage Error: Image file path not defined! Empty string received!") exit() else: img.filename = filename try: img.ipl = io.imread(img.filename) vl.vglAddContext(img, vl.VGL_RAM_CONTEXT()) except FileNotFoundError as fnf: print("vglImage: vglLoadImage Error: loading image from file:", img.filename) print(str(fnf)) exit() except Exception as e: print("vglImage: vglLoadImage Error: Unrecognized exception was thrown.") print(str(e)) exit() if( isinstance(img.ipl, np.ndarray) ): #print("vglImage: Image loaded! VGL_RAM_CONTEXT.") vl.create_vglShape(img) img.depth = img.getVglShape().getNFrames() img.nChannels = img.getVglShape().getNChannels()
def vglClDownload(img): if (vl.vglIsInContext(img, vl.VGL_CL_CONTEXT())): if (img.clForceAsBuf == vl.IMAGE_CL_OBJECT()): vglClImageDownload(img) elif (img.clForceAsBuf == vl.IMAGE_ND_ARRAY()): vglClNdImageDownload(img) vl.vglAddContext(img, vl.VGL_RAM_CONTEXT()) else: print("vglClDownload: Error: image context is not in VGL_CL_CONTEXT.") exit()
def salvando2d(img, name): # SAVING IMAGE img ext = name.split(".") ext.reverse() #vl.vglClDownload(img) vl.vglCheckContext(img, vl.VGL_RAM_CONTEXT()) if (ext.pop(0).lower() == 'jpg'): if (img.getVglShape().getNChannels() == 4): vl.rgba_to_rgb(img) vl.vglSaveImage(name, img)
vl.vglLoadImage(img_input) vl.vglClUpload(img_input) # OUTPUT IMAGE img_output = vl.create_blank_image_as(img_input) img_output.set_oclPtr( vl.get_similar_oclPtr_object(img_input) ) vl.vglAddContext(img_output, vl.VGL_CL_CONTEXT()) # STRUCTURANT ELEMENT window = vl.VglStrEl() window.constructorFromTypeNdim(vl.VGL_STREL_CROSS(), 2) inicio = t.time() vglClNdCopy(img_input, img_output) fim = t.time() vl.vglCheckContext(img_output, vl.VGL_RAM_CONTEXT()) vl.vglSaveImage("img-vglNdCopy.jpg", img_output) msg = msg + "Tempo de execução do método vglClNdCopy:\t" +str( round( (fim-inicio), 9 ) ) +"s\n" inicio = t.time() vglClNdConvolution(img_input, img_output, window) fim = t.time() vl.vglCheckContext(img_output, vl.VGL_RAM_CONTEXT()) vl.vglSaveImage("img-vglNdConvolution.jpg", img_output) msg = msg + "Tempo de execução do método vglClNdConvolution:\t" +str( round( (fim-inicio), 9 ) ) +"s\n" inicio = t.time() vglClNdDilate(img_input, img_output, window) fim = t.time() vl.vglCheckContext(img_output, vl.VGL_RAM_CONTEXT()) vl.vglSaveImage("img-vglNdDilate.jpg", img_output)