右下角图片为模糊效果图
对图像进行模糊处理也就是平滑处理
图像模糊的用途:用来减少图像上的噪点或失真
同样需要对图像做卷积处理
卷积过程
卷积的数学表达式
本文的图像模糊采用的高斯滤波
具体操作:用一个卷积核扫描图像中的每一个像素,用模板确定的邻域内像素的加权
平均灰度值去替代模板中心像素点的值
opengl 中shader实现
varying vec2 V_Texcoord;
uniform sampler2D U_MainTexture;
void main()
{
//高斯滤波核
// 1 2 1
// 2 4 2
// 1 2 1
vec4 color=vec4(0.0);
int coreSize=3;
float texelOffset=1/150.0;
float kernel[9];
kernel[6]=1;kernel[7]=2;kernel[8]=1;
kernel[3]=2;kernel[4]=4;kernel[5]=2;
kernel[0]=1;kernel[1]=2;kernel[2]=1;
//移动高斯核,对原图做卷积计算
int index=0;
for(int y=0;y
#include
#include "glew.h"
#include
#include
#include "utils.h"
#include "GPUProgram.h"
#include "ObjModel.h"
#include "FBO.h"
#include "FullScreenQuad.h"
#include "Glm/glm.hpp"
#include "Glm/ext.hpp"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glew32.lib")
LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
WNDCLASSEX wndClass;
wndClass.cbClsExtra = 0;
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = NULL;
wndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndClass.hIcon = NULL;
wndClass.hIconSm = NULL;
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc=GLWindowProc;
wndClass.lpszClassName = L"OpenGL";
wndClass.lpszMenuName = NULL;
wndClass.style = CS_VREDRAW | CS_HREDRAW;
ATOM atom = RegisterClassEx(&wndClass);
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = 1280;
rect.bottom = 720;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
HWND hwnd = CreateWindowEx(NULL, L"OpenGL", L"RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, rect.right-rect.left, rect.bottom-rect.top, NULL, NULL, hInstance, NULL);
HDC dc = GetDC(hwnd);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DOUBLEBUFFER;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
int pixelFormatID = ChoosePixelFormat(dc, &pfd);
SetPixelFormat(dc,pixelFormatID,&pfd);
HGLRC rc = wglCreateContext(dc);
wglMakeCurrent(dc, rc);
GetClientRect(hwnd, &rect);
int viewportWidth = rect.right - rect.left, viewportHeight = rect.bottom - rect.top;
glewInit();
GPUProgram originalProgram;
originalProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/fullscreenquad.vs");
originalProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/fullscreenquad.fs");
originalProgram.Link();
originalProgram.DetectAttribute("pos");
originalProgram.DetectAttribute("texcoord");
originalProgram.DetectUniform("U_MainTexture");
GPUProgram erosionProgram;
erosionProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/fullscreenquad.vs");
erosionProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/Erosion.fs");
erosionProgram.Link();
erosionProgram.DetectAttribute("pos");
erosionProgram.DetectAttribute("texcoord");
erosionProgram.DetectUniform("U_MainTexture");
GPUProgram dilationProgram;
dilationProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/fullscreenquad.vs");
dilationProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/Dilation.fs");
dilationProgram.Link();
dilationProgram.DetectAttribute("pos");
dilationProgram.DetectAttribute("texcoord");
dilationProgram.DetectUniform("U_MainTexture");
GPUProgram gaussianProgram;
gaussianProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/fullscreenquad.vs");
gaussianProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/Gaussian.fs");
gaussianProgram.Link();
gaussianProgram.DetectAttribute("pos");
gaussianProgram.DetectAttribute("texcoord");
gaussianProgram.DetectUniform("U_MainTexture");
GPUProgram gpuProgram;
gpuProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/x_ray.vs");
gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/x_ray.fs");
gpuProgram.Link();
gpuProgram.DetectAttribute("pos");
gpuProgram.DetectAttribute("texcoord");
gpuProgram.DetectAttribute("normal");
gpuProgram.DetectUniform("M");
gpuProgram.DetectUniform("V");
gpuProgram.DetectUniform("P");
gpuProgram.DetectUniform("NM");
gpuProgram.DetectUniform("U_EyePos");
//init 3d model
ObjModel cube,quad;
cube.Init("Debug/res/model/Sphere.obj");
//cube.Init("res/model/Quad.obj");
float identity[] = {
1.0f,0,0,0,
0,1.0f,0,0,
0,0,1.0f,0,
0,0,0,1.0f
};
float eyePos[] = { -0.5f, 1.5f, -3.0f };
glm::mat4 model1 = glm::translate(-2.0f, 0.0f, -6.0f)*glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
glm::mat4 viewMatrix1 = glm::lookAt(glm::vec3(-0.5f, 1.5f, -3.0f), glm::vec3(-2.0f, 0.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 normalMatrix1 = glm::inverseTranspose(model1);
FullScreenQuad fsq;
fsq.Init();
FBO fbo;
fbo.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
fbo.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
fbo.Finish();
GLuint mainTexture = CreateTextureFromFile("Debug/res/image/stone.bmp");
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
MSG msg;
while (true)
{
if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
if (msg.message==WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
fbo.Bind();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glUseProgram(gpuProgram.mProgram);
glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model1));
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix1));
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix1));
cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
cube.Draw();
glDisable(GL_BLEND);
fbo.Unbind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(originalProgram.mProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbo.GetBuffer("color"));
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
glUseProgram(erosionProgram.mProgram);
fsq.DrawToRightTop(erosionProgram.GetLocation("pos"), erosionProgram.GetLocation("texcoord"));
glUseProgram(dilationProgram.mProgram);
fsq.DrawToLeftBottom(dilationProgram.GetLocation("pos"), dilationProgram.GetLocation("texcoord"));
glUseProgram(gaussianProgram.mProgram);
glBindTexture(GL_TEXTURE_2D, mainTexture);
glUniform1i(gaussianProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToRightBottom(gaussianProgram.GetLocation("pos"), gaussianProgram.GetLocation("texcoord"));
glFlush();
SwapBuffers(dc);
}
return 0;
}