WO2018018941A1 - 利用OpenGL与OpenCL协作实现图像缩放的方法及系统 - Google Patents

利用OpenGL与OpenCL协作实现图像缩放的方法及系统 Download PDF

Info

Publication number
WO2018018941A1
WO2018018941A1 PCT/CN2017/080778 CN2017080778W WO2018018941A1 WO 2018018941 A1 WO2018018941 A1 WO 2018018941A1 CN 2017080778 W CN2017080778 W CN 2017080778W WO 2018018941 A1 WO2018018941 A1 WO 2018018941A1
Authority
WO
WIPO (PCT)
Prior art keywords
image
opengl
opencl
context
scaling
Prior art date
Application number
PCT/CN2017/080778
Other languages
English (en)
French (fr)
Inventor
杨亮
Original Assignee
武汉斗鱼网络科技有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 武汉斗鱼网络科技有限公司 filed Critical 武汉斗鱼网络科技有限公司
Publication of WO2018018941A1 publication Critical patent/WO2018018941A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06TIMAGE DATA PROCESSING OR GENERATION, IN GENERAL
    • G06T3/00Geometric image transformations in the plane of the image
    • G06T3/40Scaling of whole images or parts thereof, e.g. expanding or contracting
    • G06T3/4023Scaling of whole images or parts thereof, e.g. expanding or contracting based on decimating pixels or lines of pixels; based on inserting pixels or lines of pixels
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06TIMAGE DATA PROCESSING OR GENERATION, IN GENERAL
    • G06T3/00Geometric image transformations in the plane of the image
    • G06T3/40Scaling of whole images or parts thereof, e.g. expanding or contracting

Definitions

  • the present invention relates to the field of image scaling technology, and in particular to a method and system for realizing image scaling by using OpenGL (Open Graphics Library) and OpenCL (Open Computing Language).
  • OpenGL Open Graphics Library
  • OpenCL Open Computing Language
  • the method of scaling the image is generally caused by using some algorithms in the CPU to process the image, for example, "an image scaling processing method and apparatus" disclosed in the Chinese Publication No. CN102567949A.
  • This method may take less time to complete an image processing in the case of processing some small resolution images or better computer performance.
  • the time required to complete one image processing may increase, which makes the processing efficiency extremely low.
  • the object of the present invention is to overcome the deficiencies of the above background art, and to provide a method and system for realizing image scaling by using OpenGL and OpenCL, and capable of performing scaling processing on a large resolution image with high efficiency.
  • the technical solution adopted by the present invention is to provide a method for realizing image scaling by using OpenGL and OpenCL, and the method includes the following steps: A. Creating a context environment interoperating with OpenGL in an OpenCL device, and transferring Step B; B. Using the created context, according to the dimension of the image to be scaled, respectively create two OpenGL image objects of corresponding dimensions from the OpenGL textures of the two corresponding dimensions; among the two OpenCL image objects created, One represents the input source image, and the other represents the target image, and proceeds to step C; C, through the bilinear interpolation algorithm of the image, scales the created source image, and writes the processed result to the target image. In the middle, go to step D; D, display the scaled target image by calling the corresponding texture drawing interface in OpenGL, and end.
  • the OpenCL device detection operation is further included: query whether the current OpenCL device supports the interoperability extension with OpenGL, and if yes, proceed to step A; otherwise, directly exit.
  • step A1 the return value of the OpenGL context acquisition function is assigned to the corresponding context field in the OpenGL collaboration creation function, and the process proceeds to step A2; A2, the OpenGL device environment is obtained. The return value of the function is assigned to the corresponding device field in the OpenGL collaboration creation function, and proceeds to step A3; A3, creating a context interoperating with OpenGL by calling OpenCL context creation function.
  • step C is: C1, calculating a wide ratio fw and a high ratio fh between the source image and the target image, and proceeding to step C2; C2, a point for the target image (x, y), based on the calculated fw and fh, calculate the The corresponding coordinates (x0, y0) of the point (x, y) in the source image; the coordinates of the adjacent four points in the source image (x1) are obtained according to the corresponding coordinates (x0, y0) , y1), (x1, y2), (x2, y1), (x2, y2), go to step C3; C3, according to the coordinates (x1, y1), (x1, y2), (x2) of the adjacent four points , y1), (x2, y2), respectively calculate the weight ratio S1, S2, S3, S4 of the four points, and proceeds to step C4; C4, according to S1, S2, S3, S4, calculate the Correspond
  • the present invention also provides a system for realizing image scaling by using OpenGL and OpenCL, the system comprising a context environment creation module, an image object creation module, a scaling processing module and an image display module;
  • the context environment creation module is used for: in an OpenCL device Creating a context interoperating with OpenGL;
  • the image object creation module is configured to: create, by using the created context, two corresponding dimensions from two OpenGL textures of respective dimensions according to the dimension of the image to be scaled OpenCL image object; one of the two OpenCL image objects created, one representing the input source image and the other representing the target image;
  • the scaling processing module is used to: create the source image through the bilinear interpolation algorithm of the image Performing a scaling process and writing the processed result to the target image;
  • the image display module is configured to: display the scaled target image by calling a corresponding texture drawing interface in OpenGL.
  • the system further includes an OpenCL device detection module for querying whether the current OpenCL device supports interoperability extension with OpenGL.
  • the specific workflow of the context environment creation module is: assigning a return value of the OpenGL context acquisition function to a corresponding context field in the OpenGL collaboration creation function; returning the OpenGL device environment acquisition function The value is assigned to the corresponding device field in the OpenGL collaboration creation function; the context that interoperates with OpenGL is created by calling OpenCL's context creation function.
  • the specific workflow of the scaling processing module is: calculating a wide ratio fw and a high ratio fh between the source image and the target image; and calculating a point (x, y) for the target image according to the calculation Obtaining fw and fh, calculating the corresponding coordinates (x0, y0) of the point (x, y) in the source image; acquiring the point (x, y) in the source image according to the corresponding coordinate (x0, y0) The coordinates of adjacent four points (x1, y1), (x1, y2), (x2, y1), (x2, y2); according to the coordinates (x1, y1), (x1, y2) of the adjacent four points, (x2, y1), (x2, y2), respectively calculate the weight ratios S1, S2, S3, S4 occupied by the four points; calculate the corresponding coordinates (x0, according to S1, S2, S3, S4, Y0) pixel value value(x
  • the present invention realizes the purpose of scaling processing a given image on the GPU by using the method of interoperating with OpenGL and OpenCL.
  • the bilinear interpolation algorithm of the image is used to scale the image, and the processing of the algorithm is performed by the corresponding function in the OpenCL device, and therefore, with respect to execution in the CPU Higher computing efficiency.
  • OpenCL and OpenGL collaborate, the image input and operation image output of OpenCL are derived from OpenGL texture, which saves a lot of overhead for image pixel transmission with the host. Improve the efficiency of the algorithm.
  • FIG. 1 is a flowchart of a method for implementing image scaling by using OpenGL and OpenCL in cooperation according to an embodiment of the present invention
  • FIG. 2 is a structural block diagram of a system for implementing image scaling by using OpenGL and OpenCL in cooperation according to an embodiment of the present invention.
  • the present invention utilizes an interoperability method between OpenGL and OpenCL, and implements an OpenCL kernel function (a function with a kernel keyword in an OpenCL program) in an OpenCL device based on a GPU (Graphic Processing Unit).
  • OpenCL kernel function a function with a kernel keyword in an OpenCL program
  • GPU Graphic Processing Unit
  • OpenGL Open Graphics Library
  • 1D texture 2D texture
  • 3D texture can be easily understood as a one-dimensional image, two-dimensional image, three-dimensional image.
  • Textures contain color data, brightness data, and transparency data that maps textures to surfaces that are formed from a set of polygons.
  • OpenCL device is a device for executing OpenCL kernel functions.
  • OpenCL is a framework for writing programs for heterogeneous platforms (CPU, GPU or other types of processors), a language for writing kernel kernels and a set of APIs for defining and controlling the platform (Application Programming Interface) , application programming interface).
  • the core of OpenCL is the context of OpenCL, the context is Associated devices, memory objects (for example, buffers and images), and command queues (providing an interface between context and devices) provide containers and drive communication between the application and associated devices and associated devices.
  • an embodiment of the present invention provides a method for implementing image scaling by using OpenGL and OpenCL, including the following steps:
  • Step S1 Query whether the current OpenCL device supports interoperability extension with OpenGL (ie, query whether the current OpenCL device supports the interoperation function cl_khr_gl_sharing), and if yes, go to step S2; otherwise, it indicates that the current OpenCL device does not support OpenGL and OpenCL. Operation, so that the object of the present invention cannot be achieved, is directly exited.
  • interoperability extension with OpenGL ie, query whether the current OpenCL device supports the interoperation function cl_khr_gl_sharing
  • Step S2 Create a context environment interoperating with OpenGL in the OpenCL device, and proceed to step S3.
  • step S2 specifically includes the following operations:
  • Step S201 In the system of the OpenCL device, assign the return value of the OpenGL context acquisition function wglGetCurrentContext (the function is used to obtain the function of the current OpenGL context environment) to the corresponding context field (CL_GL_CONTEXT_KHR) in the OpenGL collaboration creation function, and transfer Step S202;
  • Step S202 The OpenGL device environment acquisition function wglGetCurrentDC (the function is used to obtain the OpenGL current device environment HDC) return value is assigned to the corresponding device field (CL_WGL_HDC_KHR) in the OpenGL collaboration creation function, and proceeds to step S203;
  • wglGetCurrentDC the function is used to obtain the OpenGL current device environment HDC
  • Step S203 Create a function interoperating with OpenGL by calling OpenCL's context creation function clCreateContext (the function is used to create a context of the corresponding device).
  • Step S3 using the created context, according to the dimension of the image to be scaled as needed (such as two-dimensional images or three-dimensional images), respectively, from two OpenGL textures of corresponding dimensions (such as OpenGL2D texture or OpenGL3D texture), create two OpenCL image objects of corresponding dimensions (such as two-dimensional image object image2d_t or three-dimensional image object) Image3d_t); of the two OpenCL image objects, one represents the input source image, this is used for scaling; the other represents the target image, which is used to save the scaled image, and proceeds to step S4.
  • the dimension of the image to be scaled as needed such as two-dimensional images or three-dimensional images
  • two OpenGL textures of corresponding dimensions such as OpenGL2D texture or OpenGL3D texture
  • create two OpenCL image objects of corresponding dimensions such as two-dimensional image object image2d_t or three-dimensional image object) Image3d_t
  • step S3 when two OpenCL image objects of corresponding dimensions are created, the two-dimensional image creation function clCreateFromGLTexture2D or the three-dimensional image creation function clCreateFromGLTexture3D in OpenCL is implemented.
  • the clCreateFromGLTexture2D function is used to create an OpenCL 2D (2D) image object from an OpenGL 2D texture;
  • the clCreateFromGLTexture3D function is used to create an OpenCL 3D (3D) image object from an OpenGL 3D texture to implement OpenCL and OpenGL. Interoperability of images.
  • Step S4 Using the kernel function of the written OpenCL, the generated source image is scaled by the bilinear interpolation algorithm of the image; and the processed result is written into the target image, and the process proceeds to step S5.
  • the generated source image is scaled by the bilinear interpolation algorithm of the image, and the processed result is written into the target image, which specifically includes the following operations:
  • Step S401 calculating the width ratio fw and the high ratio fh between the source image and the target image, and the calculation result is represented by a floating point number, and proceeds to step S402;
  • Step S402 For a point (x, y) of the target image, x is the abscissa and y is the ordinate. According to the calculated fw and fh, the point (x, y) is calculated in the source image. Corresponding coordinates (x0, y0), and the calculation result is represented by a floating point number; according to the corresponding coordinate (x0, y0), the coordinates (x1, of the corresponding four points in the source image are obtained (x, y), Y1), (x1, y2), (x2, y1), (x2, y2), and the coordinates are integer-type, and the process proceeds to step S403;
  • Step S403 Calculate the weight ratios S1, S2, S3, S4 of the four points according to the coordinates of the adjacent four points, and the calculation result is represented by a floating point number, and the process proceeds to step S404;
  • Step S404 Calculate the pixel value value (x0, y0) of the coordinates (x0, y0) according to S1, S2, S3, S4, and proceed to step S405;
  • the pixel value is obtained by the built-in pixel value acquisition function read_imagef of OpenCL.
  • Step S405 The calculated pixel value value (x0, y0) is written into the scaled target image by the built-in pixel value writing function write_imagef, and the process proceeds to step S406;
  • Step S406 Steps S402 to S405 are repeated until all points of the target image are in place Finished.
  • the newly created pixel value (ie, the coordinate value) in the target image is the value of 4 neighboring pixels in the 2*2 region near the source image position. Weighted average calculation.
  • the bilinear interpolation algorithm has a high image quality after amplification, and there is no case where the pixel values are not continuous.
  • Step S5 Display the scaled target image by ending the drawing texture interface of the corresponding dimension in OpenGL (such as an interface for drawing a 2D texture or an interface for drawing a 3D texture), and ending.
  • the drawing texture interface of the corresponding dimension in OpenGL such as an interface for drawing a 2D texture or an interface for drawing a 3D texture
  • the present invention also provides a system for realizing image scaling by using OpenGL and OpenCL, and the system includes a context environment creation module, an image object creation module, a scaling processing module, and an image display module.
  • the context creation module is used to: create a context environment interoperating with OpenGL in the OpenCL device, the specific workflow is: assign the return value of the OpenGL context acquisition function to the corresponding context field in the OpenGL collaboration creation function; the OpenGL device environment The return value of the get function is assigned to the corresponding device field in the OpenGL collaboration creation function; the context that interoperates with OpenGL is created by calling OpenCL's context creation function.
  • the image object creation module is configured to: create an OpenCL image object of two corresponding dimensions from two OpenGL textures of corresponding dimensions according to the created context, according to the dimension of the image to be scaled respectively (through the corresponding dimension image in OpenCL) Create a function implementation); one of the two OpenCL image objects created, one representing the input source image and the other representing the target image.
  • the scaling processing module is configured to: perform scaling processing on the created source image by using a bilinear interpolation algorithm of the image, and write the processed result into the target image.
  • the specific workflow is: calculating the wide ratio fw and the high ratio fh between the source image and the target image; Calculating the corresponding coordinates (x0, y0) of the point (x, y) in the source image according to the calculated fw and fh for a point (x, y) of the target image; according to the corresponding coordinate (x0, Y0) Get the coordinates (x1, y1), (x1, y2), (x2, y1), (x2, y2) of the neighboring four points in the source image to the point (x, y); according to the neighboring four points
  • the image display module is configured to: display the scaled target image by calling a corresponding texture drawing interface in OpenGL.
  • the system of the present invention further includes an OpenCL device detection module, which is used to query the current OpenCL. Whether the device supports interoperability extension with OpenGL. If it is supported, other function modules are called to perform image scaling processing. If not, the operation is exited.

Landscapes

  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Editing Of Facsimile Originals (AREA)
  • Image Processing (AREA)

Abstract

一种利用OpenGL与OpenCL协作实现图像缩放的方法及系统,涉及图像缩放技术领域。该图像缩放方法包括:在OpenCL设备中创建与OpenGL互操作的上下文环境;利用创建的上下文环境,根据需要缩放的图像的维度,分别从两个相应维度的OpenGL纹理中,创建出两个相应维度的OpenCL图像对象;创建的两个OpenCL图像对象中,一个代表输入的源图像,另一个代表目标图像;通过图像的双线性内插值算法,对创建的源图像进行缩放处理,并将处理后的结果写入到目标图像中;通过调用OpenGL中相应维度的绘制纹理接口,将缩放后的目标图像显示出来。该方法能在较高的效率下完成对较大分辨率图像的缩放处理,运算效率高、处理效果好。

Description

利用OpenGL与OpenCL协作实现图像缩放的方法及系统 技术领域
本发明涉及图像缩放技术领域,具体来讲是一种利用OpenGL(Open Graphics Library,开放图形库)与OpenCL(Open Computing Language,开放运算语言)协作实现图像缩放的方法及系统。
背景技术
如今,对图像进行放大或者缩小在图像处理中非常的频繁,从一般的视频播放器到一些专业的图像处理软件,可能都会对某些图像进行缩放处理。
但目前,对图像进行缩放处理的方法,一般多源于在CPU中利用某些算法,对图像进行处理,例如:中国公开的公告号为CN102567949A的《一种图像缩放处理方法及装置》。这种方法在处理一些小分辨率的图像或者计算机性能比较好的情况下,完成一次图像处理所需要的时间可能比较小。但是在处理分辨率非常大的图像或者计算机性能又一般的情况下,完成一次图像处理所需要的时间可能会集聚的增加,从而使得处理的效率非常的低下。
因此,如何在较高的效率下完成对较大分辨率图像的缩放处理是本领域亟待解决的问题。
发明内容
本发明的目的是为了克服上述背景技术的不足,提供一种利用OpenGL与OpenCL协作实现图像缩放的方法及系统,能在较高的效率下完成对较大分辨率图像的缩放处理。
为达到以上目的,本发明采取的技术方案是:提供一种利用OpenGL与OpenCL协作实现图像缩放的方法,该方法包括以下步骤:A、在OpenCL设备中创建与OpenGL互操作的上下文环境,转入步骤B;B、利用创建的上下文环境,根据需要缩放的图像的维度,分别从两个相应维度的OpenGL纹理中,创建出两个相应维度的OpenCL图像对象;创建的两个OpenCL图像对象中,一个代表输入的源图像,另一个代表目标图像,转入步骤C;C、通过图像的双线性内插值算法,对创建的源图像进行缩放处理,并将处理后的结果写入到目标图像中,转入步骤D;D、通过调用OpenGL中相应维度的绘制纹理接口,将缩放后的目标图像显示出来,结束。
在上述技术方案的基础上,在步骤A之前还包括OpenCL设备检测操作:查询当前的OpenCL设备是否支持与OpenGL的互操作扩展,若是,进入步骤A;否则,直接退出。
在上述技术方案的基础上,步骤A的具体流程为:A1、将OpenGL上下文获取函数的返回值赋值至OpenGL协作创建函数中对应的上下文字段中,转入步骤A2;A2、将OpenGL设备环境获取函数的返回值赋值至OpenGL协作创建函数中对应的设备字段中,转入步骤A3;A3、通过调用OpenCL的上下文环境创建函数,创建与OpenGL互操作的上下文。
在上述技术方案的基础上,步骤B中所述创建出两个相应维度的OpenCL图像对象时,是通过OpenCL中相应维度的图像创建函数实现的。
在上述技术方案的基础上,步骤C的具体流程为:C1、计算源图像与目标图像之间的宽比例fw和高比例fh,转入步骤C2;C2、针对目标图像的一个点(x,y),根据计算得到的fw和fh,计算出该 点(x,y)在源图像中的对应坐标(x0,y0);根据所述对应坐标(x0,y0)获取到点(x,y)在源图像中的邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2),转入步骤C3;C3、根据邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2),分别计算出这四个点所占的权重比值S1、S2、S3、S4,转入步骤C4;C4、根据S1、S2、S3、S4,计算出所述对应坐标(x0,y0)的像素值value(x0,y0),转入步骤C5;C5、通过OpenCL的像素值写入函数,将计算出的像素值value(x0,y0)写入到目标图像中,转入步骤C6;C6:重复步骤C2~C5,直至目标图像的所有点都处理完毕。
本发明还提供一种利用OpenGL与OpenCL协作实现图像缩放的系统,该系统包括上下文环境创建模块、图像对象创建模块、缩放处理模块和图像显示模块;所述上下文环境创建模块用于:在OpenCL设备中创建与OpenGL互操作的上下文环境;所述图像对象创建模块用于:利用创建的上下文环境,根据需要缩放的图像的维度,分别从两个相应维度的OpenGL纹理中,创建出两个相应维度的OpenCL图像对象;创建的两个OpenCL图像对象中,一个代表输入的源图像,另一个代表目标图像;所述缩放处理模块用于:通过图像的双线性内插值算法,对创建的源图像进行缩放处理,并将处理后的结果写入到目标图像中;所述图像显示模块用于:通过调用OpenGL中相应维度的绘制纹理接口,将缩放后的目标图像显示出来。
在上述技术方案的基础上,该系统还包括OpenCL设备检测模块,用于查询当前的OpenCL设备是否支持与OpenGL的互操作扩展。
在上述技术方案的基础上,所述上下文环境创建模块的具体工作流程为:将OpenGL上下文获取函数的返回值赋值至OpenGL协作创建函数中对应的上下文字段中;将OpenGL设备环境获取函数的返回 值赋值至OpenGL协作创建函数中对应的设备字段中;通过调用OpenCL的上下文环境创建函数,创建与OpenGL互操作的上下文。
在上述技术方案的基础上,所述图像对象创建模块创建出两个相应维度的OpenCL图像对象时,是通过OpenCL中相应维度的图像创建函数实现的。
在上述技术方案的基础上,所述缩放处理模块的具体工作流程为:计算源图像与目标图像之间的宽比例fw和高比例fh;针对目标图像的一个点(x,y),根据计算得到的fw和fh,计算出该点(x,y)在源图像中的对应坐标(x0,y0);根据所述对应坐标(x0,y0)获取到点(x,y)在源图像中的邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2);根据邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2),分别计算出这四个点所占的权重比值S1、S2、S3、S4;根据S1、S2、S3、S4,计算出所述对应坐标(x0,y0)的像素值value(x0,y0);通过OpenCL的像素值写入函数,将计算出的像素值value(x0,y0)写入到目标图像中;重复上述操作,直至目标图像的所有点都处理完毕。
本发明的有益效果在于:
与现有技术中通过CPU实现图像处理的方式相比,本发明利用OpenGL与OpenCL互操作的方法,在GPU上实现了对给定图像进行缩放处理的目的。在处理图像缩放时,采用的是图像的双线性内插值算法来对图像进行缩放处理,且该算法的处理过程是在OpenCL设备中通过相应函数执行的,因此,相对于在CPU中执行具有更高的运算效率。同时,也因为OpenCL与OpenGL进行协作,使得我们对OpenCL进行的图像输入与操作后的图像输出都来源于OpenGL的纹理,这样省去了大量的与主机进行图像像素传输方面的开销,进一步 的提高了算法的效率。
附图说明
图1为本发明实施例中利用OpenGL与OpenCL协作实现图像缩放的方法的流程图;
图2为本发明实施例中利用OpenGL与OpenCL协作实现图像缩放的系统的结构框图。
具体实施方式
下面结合附图及具体实施例对本发明作进一步的详细描述。
本发明是利用OpenGL与OpenCL的互操作的方法,在基于GPU(Graphic Processing Unit,图形处理器)的OpenCL设备中通过执行OpenCL的内核函数(OpenCL程序中带有kernel关键字的函数),来实现对给定图像进行缩放处理的目的。
其中,OpenGL(Open Graphics Library,开放图形库)是一个功能强大,调用方便的底层图形库,其定义了跨编程语言、跨平台的编程接口规格的专业的图形程序接口库,用于二维、三维图像。在OpenGL中,包含有多种OpenGL纹理:1D纹理、2D纹理、3D纹理(可简单理解成一维图像、二维图像、三维图像)。纹理包含了颜色数据、亮度数据和透明度数据,可以把纹理映射到由一组多边形构成的表面上形成图像。
另外,OpenCL设备是一种用于执行OpenCL内核函数的设备。其中,OpenCL是一个为异构平台(CPU,GPU或其他类型的处理器)编写程序的框架,由一种用于编写内核kernels的语言和一组用于定义并控制平台的API(Application Programming Interface,应用编程接口)组成。OpenCL的核心是OpenCL的上下文环境,上下文环境为 关联的设备、内存对象(例如,缓冲区和图像)以及命令队列(在上下文和各设备之间提供一个接口)提供容器,并驱动着应用程序与关联设备以及关联设备之间的通信。
基于上述技术原理,参见图1所示,本发明实施例提供一种利用OpenGL与OpenCL协作实现图像缩放的方法,包括以下步骤:
步骤S1:查询当前的OpenCL设备是否支持与OpenGL的互操作扩展(即查询当前OpenCL设备是否支持互操作函数cl_khr_gl_sharing),若是,转入步骤S2;否则,表明当前OpenCL设备不支持OpenGL与OpenCL的互操作,从而不能实现本发明的目的,则直接退出。
步骤S2:在OpenCL设备中创建与OpenGL互操作的上下文环境,转入步骤S3。
实际操作时,步骤S2具体包括以下操作:
步骤S201:在OpenCL设备的系统中,将OpenGL上下文获取函数wglGetCurrentContext(该函数用于获取当前OpenGL上下文环境的函数)的返回值赋值至OpenGL协作创建函数中对应的上下文字段(CL_GL_CONTEXT_KHR)中,转入步骤S202;
步骤S202:将OpenGL设备环境获取函数wglGetCurrentDC(该函数用于获取OpenGL当前的设备环境HDC)的返回值赋值至OpenGL协作创建函数中对应的设备字段(CL_WGL_HDC_KHR)中,转入步骤S203;
步骤S203:通过调用OpenCL的上下文环境创建函数clCreateContext(该函数用于创建对应设备的上下文环境),创建与OpenGL互操作的上下文。
步骤S3:利用创建的上下文环境,根据需要缩放的图像的维度 (如二维图像或三维图像),分别从两个相应维度的OpenGL纹理中(如OpenGL2D纹理或OpenGL3D纹理),创建出两个相应维度的OpenCL图像对象(如二维图像对象image2d_t或三维图像对象image3d_t);两个OpenCL图像对象中,一个代表输入的源图像,这个用来进行缩放;另一个代表目标图像,用来保存缩放后的图像,转入步骤S4。
其中,步骤S3中,创建出两个相应维度的OpenCL图像对象时,是通过OpenCL中的二维图像创建函数clCreateFromGLTexture2D或三维图像创建函数clCreateFromGLTexture3D来实现的。clCreateFromGLTexture2D函数用于从一个OpenGL的2D纹理中创建一个OpenCL的2D(二维)图像对象;clCreateFromGLTexture3D函数用于从一个OpenGL的3D纹理中创建一个OpenCL的3D(三维)图像对象,从而实现OpenCL和OpenGL对图像的互操作。
步骤S4:利用已编写好的OpenCL的内核函数,通过图像的双线性内插值算法,对创建的源图像进行缩放处理;并将处理后的结果写入到目标图像中,转入步骤S5。
具体来说,通过图像的双线性内插值算法,对创建的源图像进行缩放处理,并将处理后的结果写入到目标图像中,具体包括以下操作:
步骤S401:计算源图像与目标图像之间的宽比例fw和高比例fh,且计算结果用浮点数表示,转入步骤S402;
其中,宽比例fw的计算公式为:fw=(w0-1)/(w1-1),w0表示源图像的宽度,w1表示目标图像的宽度;高比例fh的计算公式为:fh=(h0-1)/(h1-1),h0表示源图像的高度,h1表示目标图像的高度。
步骤S402:针对目标图像的一个点(x,y),x为横坐标、y为纵坐标,根据计算得到的fw和fh,计算出该点(x,y)在源图像中 的对应坐标(x0,y0),且计算结果用浮点数表示;根据所述对应坐标(x0,y0)获取到点(x,y)在源图像中对应的邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2),且坐标取整型,转入步骤S403;其中,
float x0=x*fw,float y0=y*fh,float表示结果取浮点数;
int x1=int(x0),int x2=x1+1,int y1=int(y0),int y2=y1+1,int表示结果取整型。
步骤S403:根据邻近四个点的坐标,分别计算出这四个点所占的权重比值S1、S2、S3、S4,且计算结果用浮点数表示,转入步骤S404;
其中,S1=(x0-x1)*(y0-y1);
S2=[1-(x0-x1)]*(y0-y1);
S3=[1-(x0-x1)]*[1-(y0-y1)];
S4=(x0-x1)*[1-(y0-y1)]。
步骤S404:根据S1、S2、S3、S4,计算出坐标(x0,y0)的像素值value(x0,y0),转入步骤S405;
具体来说,像素值value(x0,y0)的计算公式为:value(x0,y0)=value(x2,y2)*s1+value(x1,y2)*s2+value(x1,y1)*s3+value(x2,y1)*s4;其中value(x2,y2)、value(x1,y2)、value(x1,y1)、value(x2,y1)分别表示对应坐标处的源图像像素值,该像素值通过OpenCL内置的像素值获取函数read_imagef获取得到。
步骤S405:通过OpenCL内置的像素值写入函数write_imagef,将计算出的像素值value(x0,y0)写入到缩放后的目标图像中,转入步骤S406;
步骤S406:重复步骤S402~S405,直至目标图像的所有点都处 理完毕。
可以理解的是,图像的双线性内插值算法中,目标图像中新创造的像素值(即坐标值),是由源图像位置在它附近的2*2区域4个邻近像素的值,通过加权平均计算得出的。双线性内插值算法放大后的图像质量较高,不会出现像素值不连续的情况。
步骤S5:通过调用OpenGL中相应维度的绘制纹理接口(如绘制2D纹理的接口或绘制3D纹理的接口),将缩放后的目标图像显示出来,结束。
参见图2所示,本发明还提供了一种利用OpenGL与OpenCL协作实现图像缩放的系统,该系统包括上下文环境创建模块、图像对象创建模块、缩放处理模块和图像显示模块。
上下文环境创建模块用于:在OpenCL设备中创建与OpenGL互操作的上下文环境,具体工作流程为:将OpenGL上下文获取函数的返回值赋值至OpenGL协作创建函数中对应的上下文字段中;将OpenGL设备环境获取函数的返回值赋值至OpenGL协作创建函数中对应的设备字段中;通过调用OpenCL的上下文环境创建函数,创建与OpenGL互操作的上下文。
图像对象创建模块用于:利用创建的上下文环境,根据需要缩放的图像的维度,分别从两个相应维度的OpenGL纹理中,创建出两个相应维度的OpenCL图像对象(通过OpenCL中相应维度的图像创建函数实现);创建的两个OpenCL图像对象中,一个代表输入的源图像,另一个代表目标图像。
缩放处理模块用于:通过图像的双线性内插值算法,对创建的源图像进行缩放处理,并将处理后的结果写入到目标图像中。其具体工作流程为:计算源图像与目标图像之间的宽比例fw和高比例fh;针 对目标图像的一个点(x,y),根据计算得到的fw和fh,计算出该点(x,y)在源图像中的对应坐标(x0,y0);根据所述对应坐标(x0,y0)获取到点(x,y)在源图像中的邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2);根据邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2),分别计算出这四个点所占的权重比值S1、S2、S3、S4;根据S1、S2、S3、S4,计算出所述对应坐标(x0,y0)的像素值value(x0,y0);通过OpenCL的像素值写入函数,将计算出的像素值value(x0,y0)写入到目标图像中;重复上述操作,直至目标图像的所有点都处理完毕。
图像显示模块用于:通过调用OpenGL中相应维度的绘制纹理接口,将缩放后的目标图像显示出来。
进一步地,实际操作时,为了确保当前的OpenCL设备能支持与OpenGL的互操作,保证操作的有效性,因此,本发明的系统中还增设有OpenCL设备检测模块,该模块用于查询当前的OpenCL设备是否支持与OpenGL的互操作扩展,若支持,则调用其他功能模块进行图像缩放处理,若不支持,则退出操作。
需要说明的是:上述实施例提供的利用OpenGL与OpenCL协作实现图像缩放的系统在进行图像缩放时,仅以上述各功能模块的划分进行举例说明,实际应用中,可根据需要将上述功能分配由不同的功能模块完成,即将系统的内部结构划分成不同的功能模块,以完成以上描述的全部或者部分功能。
本发明不局限于上述实施方式,对于本技术领域的普通技术人员来说,在不脱离本发明原理的前提下,还可以做出若干改进和润饰,这些改进和润饰也视为本发明的保护范围之内。本说明书中未作详细描述的内容属于本领域专业技术人员公知的现有技术。

Claims (10)

  1. 一种利用OpenGL与OpenCL协作实现图像缩放的方法,其特征在于,该方法包括以下步骤:
    A、在OpenCL设备中创建与OpenGL互操作的上下文环境,转入步骤B;
    B、利用创建的上下文环境,根据需要缩放的图像的维度,分别从两个相应维度的OpenGL纹理中,创建出两个相应维度的OpenCL图像对象;创建的两个OpenCL图像对象中,一个代表输入的源图像,另一个代表目标图像,转入步骤C;
    C、通过图像的双线性内插值算法,对创建的源图像进行缩放处理,并将处理后的结果写入到目标图像中,转入步骤D;
    D、通过调用OpenGL中相应维度的绘制纹理接口,将缩放后的目标图像显示出来,结束。
  2. 如权利要求1所述的利用OpenGL与OpenCL协作实现图像缩放的方法,其特征在于:在步骤A之前还包括OpenCL设备检测操作:查询当前的OpenCL设备是否支持与OpenGL的互操作扩展,若是,进入步骤A;否则,直接退出。
  3. 如权利要求1所述的利用OpenGL与OpenCL协作实现图像缩放的方法,其特征在于,步骤A的具体流程为:
    A1、将OpenGL上下文获取函数的返回值赋值至OpenGL协作创建函数中对应的上下文字段中,转入步骤A2;
    A2、将OpenGL设备环境获取函数的返回值赋值至OpenGL协作创建函数中对应的设备字段中,转入步骤A3;
    A3、通过调用OpenCL的上下文环境创建函数,创建与OpenGL互操作的上下文。
  4. 如权利要求1所述的利用OpenGL与OpenCL协作实现图像缩放的方法,其特征在于:步骤B中所述创建出两个相应维度的OpenCL图像对象时,是通过OpenCL中相应维度的图像创建函数实现的。
  5. 如权利要求1所述的利用OpenGL与OpenCL协作实现图像缩放的方法,其特征在于,步骤C的具体流程为:
    C1、计算源图像与目标图像之间的宽比例fw和高比例fh,转入步骤C2;
    C2、针对目标图像的一个点(x,y),根据计算得到的fw和fh,计算出该点(x,y)在源图像中的对应坐标(x0,y0);根据所述对应坐标(x0,y0)获取到点(x,y)在源图像中的邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2),转入步骤C3;
    C3、根据邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2),分别计算出这四个点所占的权重比值S1、S2、S3、S4,转入步骤C4;
    C4、根据S1、S2、S3、S4,计算出所述对应坐标(x0,y0)的像素值value(x0,y0),转入步骤C5;
    C5、通过OpenCL的像素值写入函数,将计算出的像素值value(x0,y0)写入到目标图像中,转入步骤C6;
    C6:重复步骤C2~C5,直至目标图像的所有点都处理完毕。
  6. 一种利用OpenGL与OpenCL协作实现图像缩放的系统,其特征在于:该系统包括上下文环境创建模块、图像对象创建模块、缩放处理模块和图像显示模块;
    所述上下文环境创建模块用于:在OpenCL设备中创建与OpenGL互操作的上下文环境;
    所述图像对象创建模块用于:利用创建的上下文环境,根据需要缩放的图像的维度,分别从两个相应维度的OpenGL纹理中,创建出两个相应维度的OpenCL图像对象;创建的两个OpenCL图像对象中,一个代表输入的源图像,另一个代表目标图像;
    所述缩放处理模块用于:通过图像的双线性内插值算法,对创建的源图像进行缩放处理,并将处理后的结果写入到目标图像中;
    所述图像显示模块用于:通过调用OpenGL中相应维度的绘制纹理接口,将缩放后的目标图像显示出来。
  7. 如权利要求6所述的利用OpenGL与OpenCL协作实现图像缩放的系统,其特征在于:该系统还包括OpenCL设备检测模块,用于查询当前的OpenCL设备是否支持与OpenGL的互操作扩展。
  8. 如权利要求6所述的利用OpenGL与OpenCL协作实现图像缩放的系统,其特征在于,所述上下文环境创建模块的具体工作流程为:将OpenGL上下文获取函数的返回值赋值至OpenGL协作创建函数中对应的上下文字段中;将OpenGL设备环境获取函数的返回值赋值至OpenGL协作创建函数中对应的设备字段中;通过调用OpenCL的上下文环境创建函数,创建与OpenGL互操作的上下文。
  9. 如权利要求6所述的利用OpenGL与OpenCL协作实现图像缩放的系统,其特征在于:所述图像对象创建模块创建出两个相应维度的OpenCL图像对象时,是通过OpenCL中相应维度的图像创建函数实现的。
  10. 如权利要求6所述的利用OpenGL与OpenCL协作实现图像缩放的系统,其特征在于,所述缩放处理模块的具体工作流程为:计算源图像与目标图像之间的宽比例fw和高比例fh;针对目标图像的一个点(x,y),根据计算得到的fw和fh,计算出该点(x,y)在源 图像中的对应坐标(x0,y0);根据所述对应坐标(x0,y0)获取到点(x,y)在源图像中的邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2);根据邻近四个点的坐标(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2),分别计算出这四个点所占的权重比值S1、S2、S3、S4;根据S1、S2、S3、S4,计算出所述对应坐标(x0,y0)的像素值value(x0,y0);通过OpenCL的像素值写入函数,将计算出的像素值value(x0,y0)写入到目标图像中;重复上述操作,直至目标图像的所有点都处理完毕。
PCT/CN2017/080778 2016-07-28 2017-04-17 利用OpenGL与OpenCL协作实现图像缩放的方法及系统 WO2018018941A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201610606040.X 2016-07-28
CN201610606040.XA CN106251291A (zh) 2016-07-28 2016-07-28 利用OpenGL与OpenCL协作实现图像缩放的方法及系统

Publications (1)

Publication Number Publication Date
WO2018018941A1 true WO2018018941A1 (zh) 2018-02-01

Family

ID=57604684

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2017/080778 WO2018018941A1 (zh) 2016-07-28 2017-04-17 利用OpenGL与OpenCL协作实现图像缩放的方法及系统

Country Status (2)

Country Link
CN (1) CN106251291A (zh)
WO (1) WO2018018941A1 (zh)

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109872268A (zh) * 2018-12-20 2019-06-11 中国电子科技集团公司第二十七研究所 一种高码速率遥感卫星原始数据实时快视方法
CN111722887A (zh) * 2020-05-30 2020-09-29 南京恒歌零一信息科技有限公司 一种对海量散点数据的处理和显示的方法和系统
WO2022076361A1 (en) * 2020-10-05 2022-04-14 Thrive Bioscience, Inc. Method and apparatus for displaying cultured cells

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN106251291A (zh) * 2016-07-28 2016-12-21 武汉斗鱼网络科技有限公司 利用OpenGL与OpenCL协作实现图像缩放的方法及系统
CN107422953A (zh) * 2017-06-19 2017-12-01 南京巨鲨显示科技有限公司 一种用于医用显示器的图像局部突出缩放方法

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103888771A (zh) * 2013-12-30 2014-06-25 中山大学深圳研究院 基于gpgpu技术的并行视频图像处理方法
US20150002521A1 (en) * 2013-06-28 2015-01-01 Lockheed Martin Corporation Externally directed gpu data
CN105303506A (zh) * 2014-06-19 2016-02-03 Tcl集团股份有限公司 一种基于html5的数据并行处理方法及系统
CN106251291A (zh) * 2016-07-28 2016-12-21 武汉斗鱼网络科技有限公司 利用OpenGL与OpenCL协作实现图像缩放的方法及系统

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20150002521A1 (en) * 2013-06-28 2015-01-01 Lockheed Martin Corporation Externally directed gpu data
CN103888771A (zh) * 2013-12-30 2014-06-25 中山大学深圳研究院 基于gpgpu技术的并行视频图像处理方法
CN105303506A (zh) * 2014-06-19 2016-02-03 Tcl集团股份有限公司 一种基于html5的数据并行处理方法及系统
CN106251291A (zh) * 2016-07-28 2016-12-21 武汉斗鱼网络科技有限公司 利用OpenGL与OpenCL协作实现图像缩放的方法及系统

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
LIU, WENZHI ET AL., OPENCL HETEROGENEOUS PARALLEL COMPUTING: FROM PRINCIPLE TO PRACTICE, 31 January 2016 (2016-01-31), pages 281 - 320 *

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109872268A (zh) * 2018-12-20 2019-06-11 中国电子科技集团公司第二十七研究所 一种高码速率遥感卫星原始数据实时快视方法
CN111722887A (zh) * 2020-05-30 2020-09-29 南京恒歌零一信息科技有限公司 一种对海量散点数据的处理和显示的方法和系统
WO2022076361A1 (en) * 2020-10-05 2022-04-14 Thrive Bioscience, Inc. Method and apparatus for displaying cultured cells

Also Published As

Publication number Publication date
CN106251291A (zh) 2016-12-21

Similar Documents

Publication Publication Date Title
WO2018018941A1 (zh) 利用OpenGL与OpenCL协作实现图像缩放的方法及系统
US10733691B2 (en) Fragment shaders perform vertex shader computations
US11232534B2 (en) Scheme for compressing vertex shader output parameters
JP6185211B1 (ja) 適応的シェーディングによるテクスチャルックアップを使用した帯域幅低減
US8217962B2 (en) Single-pass bounding box calculation
JP6352546B2 (ja) 非整列ブロック転送動作の処理
US20090122083A1 (en) Edge evaluation techniques for graphics hardware
JP2016524242A (ja) 頂点シェーダのフロント・エンドとしてコンピュート・シェーダを使用すること
KR20160051154A (ko) 렌더링 방법, 렌더링 장치 및 전자 장치
CN110675480B (zh) 用于获取纹理操作的采样位置的方法和装置
US10019802B2 (en) Graphics processing unit
CN110807827A (zh) 系统生成稳定的重心坐标和直接平面方程访问
US10825129B2 (en) Eliminating off screen passes using memoryless render target
CN115330986B (zh) 一种分块渲染模式图形处理方法及系统
KR102477265B1 (ko) 그래픽스 프로세싱 장치 및 그래픽스 파이프라인의 텍스쳐링을 위한 LOD(level of detail)를 결정하는 방법
US10192348B2 (en) Method and apparatus for processing texture
WO2023202367A1 (zh) 图形处理器、系统、装置、设备及方法
US11010863B2 (en) Bindpoint emulation
US20140176578A1 (en) Input output connector for accessing graphics fixed function units in a software-defined pipeline and a method of operating a pipeline
US7508397B1 (en) Rendering of disjoint and overlapping blits
KR20220016774A (ko) 작은 프리미티브들에 대한 셰이더 점유를 수행하는 방법
KR20230058460A (ko) 깊이 컬링을 지연호출하기 위한 셰이더 코어 명령
US10387997B2 (en) Information processing device, information processing method, and storage medium
US20200020139A1 (en) Rendering of Graphic Objects With Pattern Paint Using A Graphics Processing Unit
CN116263981B (zh) 图形处理器、系统、装置、设备及方法

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 17833247

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 17833247

Country of ref document: EP

Kind code of ref document: A1

122 Ep: pct application non-entry in european phase

Ref document number: 17833247

Country of ref document: EP

Kind code of ref document: A1