在前面的博客中opengl 显示BMP图像,总结了如何使用opengl显示BMP图像,如何显示BMP图像序列。
在做Object detection的一些工作中,经常会将检测到object用一个框标记出来,这次探索一下如何实现这个功能。
这要使用Opengl的blend功能。
void glBlendFunc(GLenum sfactor,GLenum dfactor);
sfactor
指定如何计算红色,绿色,蓝色和alpha源混合因子。下列符号常量被接受:GL_ZERO,GL_ONE,GL_SRC_COLOR,GL_ONE_MINUS_SRC_COLOR,GL_DST_COLOR,GL_ONE_MINUS_DST_COLOR,GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_DST_ALPHA,GL_ONE_MINUS_DST_ALPHA,GL_CONSTANT_COLOR,GL_ONE_MINUS_CONSTANT_COLOR,GL_CONSTANT_ALPHA,GL_ONE_MINUS_CONSTANT_ALPHA和GL_SRC_ALPHA_SATURATE。初始值为GL_ONE。
dfactor
指定如何计算红色,绿色,蓝色和alpha目标混合因子。接受以下符号常量:GL_ZERO,GL_ONE,GL_SRC_COLOR,GL_ONE_MINUS_SRC_COLOR,GL_DST_COLOR,GL_ONE_MINUS_DST_COLOR,GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_DST_ALPHA,GL_ONE_MINUS_DST_ALPHA。 GL_CONSTANT_COLOR,GL_ONE_MINUS_CONSTANT_COLOR,GL_CONSTANT_ALPHA和GL_ONE_MINUS_CONSTANT_ALPHA。初始值为GL_ZERO。
其实原理还时很简单的,图片是一个图层,将框画到另外一个图层上面,将这两个图层进行alpha blending即可。
能用代码说明的问题,尽量上代码:
// show_yuv422p.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <GLTools.h> // OpenGL toolkit
#include <GLShaderManager.h>
#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <Windows.h>
#include <iostream>
using namespace std;
// settings
const int SCR_WIDTH = 640, SCR_HEIGHT = 480;
GLuint base_prog;
GLuint quad_vbo;
GLuint tex;
unsigned char* image;
GLShaderManager shaderMana