OpenGL(Python)-将STMap(UVmap)应用于图像?
我有以下代码(使用PyQt)在屏幕上加载和显示图像:OpenGL(Python)-将STMap(UVmap)应用于图像?,python,opengl,Python,Opengl,我有以下代码(使用PyQt)在屏幕上加载和显示图像: gVShader = """ attribute vec4 position; attribute vec2 texture_coordinates; varying vec2 v_texture_coordinates; void main() { v_texture_coordinates =
gVShader = """
attribute vec4 position;
attribute vec2 texture_coordinates;
varying vec2 v_texture_coordinates;
void main() {
v_texture_coordinates = texture_coordinates;
v_texture_coordinates.y = 1.0 - v_texture_coordinates.y;
gl_Position = position;
}"""
gFShader = """
uniform sampler2D texture1;
uniform sampler2D texture2;
varying vec2 v_texture_coordinates;
void main() {
gl_FragColor = texture2D(texture1, texture2.rg);
}"""
class ProjectiveGLViewer(QtOpenGL.QGLWidget):
def __init__(self, parent=None):
super(ProjectiveGLViewer, self).__init__(parent)
def initializeGL(self):
vshader = QtOpenGL.QGLShader(QtOpenGL.QGLShader.Vertex, self)
if not vshader.compileSourceCode(gVShader):
print vshader.log()
fshader = QtOpenGL.QGLShader(QtOpenGL.QGLShader.Fragment, self)
if not fshader.compileSourceCode(gFShader):
print fshader.log()
self._program = QtOpenGL.QGLShaderProgram()
self._program.addShader(vshader)
self._program.addShader(fshader)
self._program.link()
self._program.bind()
# data array (2 [position], 4 [color])
data = np.array([-1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=np.float32)
self.bufferID = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.bufferID)
glBufferData(GL_ARRAY_BUFFER, data.nbytes, data, GL_DYNAMIC_DRAW)
loc = self._program.attributeLocation("position")
glEnableVertexAttribArray(loc)
glVertexAttribPointer(loc, 2, GL_FLOAT, False, 16, ctypes.c_void_p(0))
loc = self._program.attributeLocation("texture_coordinates")
glEnableVertexAttribArray(loc)
glVertexAttribPointer(loc, 2, GL_FLOAT, False, 16, ctypes.c_void_p(8))
image = QtGui.QImage("image.jpg")
ptr = image.bits()
ptr.setsize(image.byteCount())
image_data = np.asarray(ptr).reshape(image.width(), image.height(), 4)
self._imageTextureID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self._imageTextureID)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, image_data)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glActiveTexture(GL_TEXTURE0)
self._program.setUniformValue('texture1', 0)
image2 = QtGui.QImage("st_map.tif")
ptr2 = image2.bits()
ptr2.setsize(image2.byteCount())
image_data2 = np.asarray(ptr).reshape(image2.width(), image2.height(), 4)
self._stTextureID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self._stTextureID)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image2.width(), image2.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, image_data2)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
self._program.setUniformValue('texture2', 1)
def paintGL(self):
glBindBuffer(GL_ARRAY_BUFFER, self.bufferID)
glClearColor(0, 0.2, 0.3, 1.0)
glClearDepth(1.0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
def resizeGL(self, w, h):
glViewport(0, 0, w, h)
现在,我想使用STMap(UVmap)重新映射该图像的像素。
对于不熟悉STMap的人,STMap如下所示:
但是可以有很多形状。
它与我的原始图像大小相同,只包含红色和绿色通道(蓝色为空)。红色表示X轴,绿色表示Y轴。
例如,为了获得原始图像中[100100]位置的像素的新坐标,我将在STMap中的同一位置查找该像素的红色通道值(即新的X值)和绿色通道值(新的Y值)
那我该怎么做呢?我不太确定。。
我尝试添加STMap纹理,但它不允许我使用它的红色和绿色通道值(请参见片段着色器)。谢谢
编辑:
我用一些我尝试过但不起作用的东西编辑了我的代码。因为我没有所有的资源,所以我不能在这里运行代码 从着色器的角度来看,可以使用
texture2
坐标修改texture1
坐标。
有很多方法可以做到这一点,例如,您可以通过向其添加UV贴图值来转换它们。此处,texture2
包含UVMap数据:
uniform sampler2D texture1;
uniform sampler2D texture2;
varying vec2 v_texture_coordinates;
vec2 my_new_coords;
vec4 uvmap;
void main() {
// uv map data at current position
uvmap = texture2D(texture2, v_texture_coordinates);
// map current position by using uv map data
my_new_coords.x = v_texture_coordinates.x + uvmap.r;
my_new_coords.y = v_texture_coordinates.y + uvmap.g;
// sample texture 2 with new mapped coordinates
gl_FragColor = texture2D(texture1, my_new_coords);
}
或者,如果您只想使用UVMap中的坐标,您可以这样做
gl_FragColor = texture2D(texture1, uvmap);