⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frameseries.cpp

📁 机器人程序
💻 CPP
字号:
// FrameSeries.cpp - by Robin Hewitt, 2004
// http://www.robinhewitt.com/mavis
// This is free software. See license at the bottom
// of this file for terms of use.
//

//////////////////////////////////////////////////////////////
// Implementation of the FrameSeries class
//


#include "FrameSeries.h"
#include "Mavis.h"
#include <assert.h>


const int FrameSeries::MAXFRAMES = 1000;
const char * FrameSeries::DEFAULT_FRAME_NAME = "frame";


FrameSeries::FrameSeries(Mavis * pMavis, int skipCount)
{
	this->pMavis = pMavis;
	this->skipCount = skipCount;
	FrameArr = new BYTE *[MAXFRAMES];
	bufsize  = pMavis->getFrameSize();
	width    = pMavis->getImgWidth();
	height   = pMavis->getImgHeight();
	nFrames  = 0;
}


FrameSeries::~FrameSeries()
{
	deleteFrames();
	if(FrameArr) delete[] FrameArr;
}


void FrameSeries::deleteFrames()
{
	for(int i=0; i<nFrames; i++) delete[] FrameArr[i];
	nFrames = 0;
}


void FrameSeries::captureFrames(int n)
{
	if(n > MAXFRAMES) n = MAXFRAMES;

	deleteFrames();
	for(int i=0; i<n; i++)
	{
		// skip skipCount frames
		//for(int s=0; s<skipCount; s++) pMavis->nextFrame();
		for(int s=0; s<skipCount; s++) pMavis->getNextFrame();

		// capture next one, copy, and store it
		//BYTE * pBytes = pMavis->nextFrame();
		VideoFrame * pFrame = pMavis->getNextFrame();
		FrameArr[i] = new BYTE[bufsize];
		//memcpy(FrameArr[i], pBytes, bufsize);
		memcpy(FrameArr[i], pFrame->getData(), bufsize);
		nFrames++;
	}
}


MVImg<int> * FrameSeries::getIntensityImage(int iFrame)
{
	assert( iFrame >= 0 );
	assert( iFrame < MAXFRAMES );
	//return MVImgUtil::rgb2grayscale(FrameArr[iFrame], height, width);

	ImgSize_t sz;
	sz.w = width; sz.h = height;
	VideoFrame frame(sz);
	memcpy(frame.getData(), FrameArr[iFrame], bufsize);
	MVImg<int> * pImg = 0;
	MVImgUtils::frame2grayscaleImg(frame, &pImg);
	return pImg;
}

/*
IntImg * FrameSeries::getIntensityDiff(int iFrame1, int iFrame2)
{
	IntImg * pImg1 = MVImgUtil::rgb2grayscale(FrameArr[iFrame1], height, width);
	IntImg * pImg2 = MVImgUtil::rgb2grayscale(FrameArr[iFrame2], height, width);
	IntImg * pDiffImg = new IntImg(height, width);
	int nPixels = height*width;
	int * diffData = pDiffImg->getData();
	int * data1    = pImg1->getData();
	int * data2    = pImg2->getData();

	for(int iPx=0; iPx<nPixels; iPx++)
		diffData[iPx] = data1[iPx] - data2[iPx];


	delete pImg1;
	delete pImg2;

	return pDiffImg;
}


IntImg * FrameSeries::getAbsIntensityDiff(int iFrame1, int iFrame2)
{
	IntImg * pImg1 = MVImgUtil::rgb2grayscale(FrameArr[iFrame1], height, width);
	IntImg * pImg2 = MVImgUtil::rgb2grayscale(FrameArr[iFrame2], height, width);
	IntImg * pDiffImg = new IntImg(height, width);
	int nPixels = height*width;
	int * diffData = pDiffImg->getData();
	int * data1    = pImg1->getData();
	int * data2    = pImg2->getData();

	for(int iPx=0; iPx<nPixels; iPx++)
	{
		if( data1[iPx] >= data2[iPx] )
			diffData[iPx] = data1[iPx] - data2[iPx];
		else
			diffData[iPx] = data2[iPx] - data1[iPx];
	}


	delete pImg1;
	delete pImg2;

	return pDiffImg;
}
*/

void FrameSeries::writeFrames(const char * namePrefix)
{
	writeFrames(nFrames, namePrefix);
}

void FrameSeries::writeFrames(int n, const char * namePrefix, int startIndex)
{
	if(n+startIndex > nFrames) n = nFrames-startIndex;

	int iStop = n + startIndex;
	for(int i=startIndex; i<iStop; i++)
	{
		char szFilename[MAX_PATH];
		if(i < 10)
			sprintf(szFilename, "bitmap000%d.bmp", i);
		else if(i < 100)
			sprintf(szFilename, "bitmap00%d.bmp", i);
		else if(i < 100)
			sprintf(szFilename, "bitmap0%d.bmp", i);
		else
			sprintf(szFilename, "bitmap%d.bmp", i);

		// Create a file to hold the bitmap
		HANDLE hf = CreateFile(
			szFilename,
			GENERIC_WRITE,
			FILE_SHARE_READ,
			NULL,
			CREATE_ALWAYS,
			NULL,
			NULL
		);

		if( INVALID_HANDLE_VALUE == hf )
		{
			// todo: something better than this
			return;
		}

		// Write out the file header
        BITMAPFILEHEADER bfh;
        memset( &bfh, 0, sizeof( bfh ) );
        bfh.bfType    = 'MB';
        bfh.bfSize    = sizeof( bfh ) + bufsize + sizeof( BITMAPINFOHEADER );
        bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER );
		DWORD written = 0;
        WriteFile( hf, &bfh, sizeof( bfh ), &written, NULL );

		// Write the bitmap format
        BITMAPINFOHEADER bih;
        memset( &bih, 0, sizeof( bih ) );
        bih.biSize     = sizeof( bih );
        bih.biWidth    = width;
        bih.biHeight   = height;
        bih.biPlanes   = 1;
        bih.biBitCount = 24;
        written = 0;
        WriteFile( hf, &bih, sizeof( bih ), &written, NULL );

		// Write the bitmap bits
        written = 0;
        WriteFile( hf, FrameArr[i], bufsize, &written, NULL );

        CloseHandle( hf );
	}
}

///////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this
// license. If you do not agree to this license, do not download, install, copy or
// use the software.
//
//
//                        Mavis License Agreement
//
// Copyright (c) 2004, Robin Hewitt (http://www.robin-hewitt.com).
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
// This software is provided "as is" and any express or implied warranties, including,
// but not limited to, the implied warranties of merchantability and fitness for a
// particular purpose are disclaimed. In no event shall the authors or contributors be
// liable for any direct, indirect, incidental, special, exemplary, or consequential
// damages (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused and on any
// theory of liability, whether in contract, strict liability, or tort (including
// negligence or otherwise) arising in any way out of the use of this software, even
// if advised of the possibility of such damage.
///////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -