gpt4 book ai didi

opencv - cvCaptureFromAvi:图像在x和y轴上翻转

转载 作者:行者123 更新时间:2023-12-02 17:47:34 25 4
gpt4 key购买 nike

下面的代码将打开.avi文件。问题是:为什么图像在x和y轴上翻转,为什么?

#include <stdio.h>
#include <string.h>

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/imgproc/imgproc.hpp>

#include <GLFW/glfw3.h>
#include <GL/glut.h>

#define VIEWPORT_WIDTH 1280
#define VIEWPORT_HEIGHT 800
#define KEY_ESCAPE 27

CvCapture* capture;
GLFWwindow* window1;
GLFWwindow* window2;
IplImage *image;
static GLuint texName;

void initTexture(IplImage* Image);
void applyTexture(int img_width, int img_height);
void loadImage(IplImage*, GLFWwindow* window);

int main(int argc, char* argv[])
{

if (!glfwInit())
exit(EXIT_FAILURE);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1000,1000);

int count;
GLFWmonitor** monitors = glfwGetMonitors(&count);
window1 = glfwCreateWindow(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, "Simple example1", NULL, NULL);

capture = cvCaptureFromAVI("foo.avi");
assert(capture);

// Initialize OpenGL
glfwMakeContextCurrent(window1);

while (!glfwWindowShouldClose(window1))
{
image = cvQueryFrame(capture);

if(!cvGrabFrame(capture)){ // capture a frame
printf("Could not grab a frame\n\7");
exit(0);
}

glfwMakeContextCurrent(window1);
loadImage(image, window1);

}

return 0;
}

void initTexture(IplImage *Image)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);

glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, 4 , Image->width, Image->height, 0, GL_BGR, GL_UNSIGNED_BYTE, Image->imageData);
}

void applyTexture(int img_width, int img_height)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(VIEWPORT_WIDTH /2, VIEWPORT_HEIGHT/2, 0);
glTexCoord2f(0, 1); glVertex3f(VIEWPORT_WIDTH /2, VIEWPORT_HEIGHT/2+img_height, 0);
glTexCoord2f(1, 1); glVertex3f(VIEWPORT_WIDTH /2+img_width, VIEWPORT_HEIGHT/2+img_height, 0);
glTexCoord2f(1, 0); glVertex3f(VIEWPORT_WIDTH /2+img_width, VIEWPORT_HEIGHT/2, 0);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}

void loadImage(IplImage *Image, GLFWwindow* window)
{
initTexture(Image);
glViewport(0, 0, VIEWPORT_WIDTH , VIEWPORT_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, VIEWPORT_WIDTH , 0, VIEWPORT_HEIGHT, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
applyTexture(Image->width,Image->height);
glfwSwapBuffers(window);
glfwPollEvents();
}

最佳答案

这是因为OpenCV假定图像的原点(0,0)位于顶部左上角。相反,OpenGL必须起源于底部左上角。您可以按以下方式修复代码:

glTexCoord2f(0, 1); glVertex3f(VIEWPORT_WIDTH /2, VIEWPORT_HEIGHT/2, 0);
glTexCoord2f(0, 0); glVertex3f(VIEWPORT_WIDTH /2, VIEWPORT_HEIGHT/2+img_height, 0);
glTexCoord2f(1, 0); glVertex3f(VIEWPORT_WIDTH /2+img_width, VIEWPORT_HEIGHT/2+img_height, 0);
glTexCoord2f(1, 1); glVertex3f(VIEWPORT_WIDTH /2+img_width, VIEWPORT_HEIGHT/2, 0);

关于opencv - cvCaptureFromAvi:图像在x和y轴上翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31631262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com