gvideo.cpp
来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C++ 代码 · 共 132 行
CPP
132 行
/* Copyright (C) 2006, Mike Gashler This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. see http://www.gnu.org/copyleft/lesser.html*/#include "GVideo.h"#include "GArray.h"#include "GImage.h"GVideo::GVideo(int nWidth, int nHeight){ m_nWidth = nWidth; m_nHeight = nHeight; m_pFrames = new GPointerArray(256);}GVideo::~GVideo(){ int i; for(i = 0; i < m_pFrames->GetSize(); i++) delete(GetFrame(i)); delete(m_pFrames);}int GVideo::GetFrameCount(){ return m_pFrames->GetSize();}GImage* GVideo::GetFrame(int index){ return (GImage*)m_pFrames->GetPointer(index);}bool GVideo::LoadFrame(const char* szFilename){ GImage* pNewFrame = new GImage(); if(!pNewFrame->LoadPNGFile(szFilename)) { delete(pNewFrame); return false; } GAssert((int)pNewFrame->GetWidth() == m_nWidth && (int)pNewFrame->GetHeight() == m_nHeight, "the frame is the wrong size"); m_pFrames->AddPointer(pNewFrame); return true;}void GVideo::AddBlankFrame(){ GImage* pNewFrame = new GImage(); pNewFrame->SetSize(m_nWidth, m_nHeight); m_pFrames->AddPointer(pNewFrame);}void GVideo::SetSize(int width, int height){ int i; for(i = 0; i < m_pFrames->GetSize(); i++) delete(GetFrame(i)); m_pFrames->Clear(); m_nWidth = width; m_nHeight = height;}void GVideo::MakeGradientMagnitudeVideo(GVideo* pVideo, bool bForDisplay){ SetSize(pVideo->GetWidth(), pVideo->GetHeight()); const int sobel[] = { 1, 3, 1, 3, 6, 3, 1, 3, 1 }; GImage* pFrames[3]; GImage* pCurrentFrame; int x, y, z, i, j, m, c1, c2; int sums[9]; int nFrameCount = pVideo->GetFrameCount(); for(z = 0; z < pVideo->GetFrameCount(); z++) { if(GetFrameCount() <= z) AddBlankFrame(); pCurrentFrame = GetFrame(z); pFrames[0] = pVideo->GetFrame((z + nFrameCount - 1) % nFrameCount); pFrames[1] = pVideo->GetFrame(z); pFrames[2] = pVideo->GetFrame((z + 1) % nFrameCount); for(y = 0; y < m_nHeight; y++) { for(x = 0; x < m_nWidth; x++) { memset(sums, '\0', sizeof(int) * 9); for(j = -1; j < 2; j++) { for(i = -1; i < 2; i++) { m = sobel[3 * j + i + 4]; c1 = pFrames[j + 1]->SafeGetPixel(x + 1, y + i); c2 = pFrames[j + 1]->SafeGetPixel(x - 1, y + i); sums[0] += m * (gRed(c1) - gRed(c2)); sums[1] += m * (gGreen(c1) - gGreen(c2)); sums[2] += m * (gBlue(c1) - gBlue(c2)); c1 = pFrames[j + 1]->SafeGetPixel(x + i, y + 1); c2 = pFrames[j + 1]->SafeGetPixel(x + i, y - 1); sums[3] += m * (gRed(c1) - gRed(c2)); sums[4] += m * (gGreen(c1) - gGreen(c2)); sums[5] += m * (gBlue(c1) - gBlue(c2)); c1 = pFrames[2]->SafeGetPixel(x + i, y + j); c2 = pFrames[0]->SafeGetPixel(x + i, y + j); sums[6] += m * (gRed(c1) - gRed(c2)); sums[7] += m * (gGreen(c1) - gGreen(c2)); sums[8] += m * (gBlue(c1) - gBlue(c2)); } } m = MAX( sums[0] * sums[0] + sums[3] * sums[3] + sums[6] * sums[6], MAX( sums[1] * sums[1] + sums[4] * sums[4] + sums[7] * sums[7], sums[2] * sums[2] + sums[5] * sums[5] + sums[8] * sums[8]) ); if(bForDisplay) { m = ClipChan(m / 50000); pCurrentFrame->SetPixel(x, y, gARGB(0xff, m, m, m)); } else pCurrentFrame->SetPixel(x, y, m); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?