📄 image.cpp
字号:
/** * cubicles * * This is an implementation of the Viola-Jones object detection * method and some extensions. The code is mostly platform- * independent and uses only standard C and C++ libraries. It * can make use of MPI for parallel training and a few Windows * MFC functions for classifier display. * * Mathias Kolsch, matz@cs.ucsb.edu * * $Id: Image.cpp,v 1.35 2004/11/24 08:41:13 matz Exp $**/// Image.cpp defines a bunch of simple image container functions////////////////////////////////////////////////////////////////////////// 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.//// Copyright (C) 2004, Mathias Kolsch, all rights reserved.// Third party copyrights are property of their respective owners.//// Redistribution and use in binary form, with or without // modification, is permitted for non-commercial purposes only.// Redistribution in source, with or without modification, is // prohibited without prior written permission.// If granted in writing in another document, personal use and // modification are permitted provided that the following two// conditions are met://// 1.Any modification of source code must retain the above // copyright notice, this list of conditions and the following // disclaimer.//// 2.Redistribution's 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 by the copyright holders and // contributors "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 copyright holder 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.//////////////////////////////////////////////////////////////////////#include "cubicles.hpp"#include "Image.h"#include "IntegralImage.h"#include <math.h>// this for loading and writing images only#if defined(IMG_LIB_MAGICK)#pragma warning (disable: 4251)#pragma warning (disable: 4786)#include <Magick++.h>using namespace Magick;#pragma warning (default: 4251)#pragma warning (default: 4786)#elif defined(IMG_LIB_OPENCV)#include <cv.h>#include <highgui.h>#elif defined(IMG_LIB_NONE)// nothing#else#error at least IMG_LIB_NONE must be defined#endif#ifdef _DEBUG#ifdef USE_MFC#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif // USE_MFC#endif // _DEBUG#ifndef M_PI#define M_PI 3.141592653589793#endif /* M_PI */#ifdef USE_MFC#define PREPARE_DIB PrepareDIB()#else // USE_MFC#define PREPARE_DIB #endif // USE_MFC#ifdef DEBUGbool g_printlots = false;#endif // DEBUG#ifdef WIN32#include <malloc.h>#define alloca _alloca#endif// ----------------------------------------------------------------------// class CByteImage// ----------------------------------------------------------------------CByteImage::CByteImage(): m_width(0), m_height(0), m_pData(NULL), m_data_is_local(true){ PREPARE_DIB;}CByteImage::CByteImage(int width, int height): m_width(0), m_height(0), m_pData(NULL), m_data_is_local(true){ Allocate(width, height); PREPARE_DIB;}CByteImage::CByteImage(BYTE* pData, int width, int height): m_width(width), m_height(height), m_pData(pData), m_data_is_local(false){ ASSERT (m_width>0 && m_height>0); PREPARE_DIB;}CByteImage::~CByteImage(){ Deallocate();}void CByteImage::Deallocate(){ if (m_data_is_local) { m_data.resize(0); } m_pData = NULL;}void CByteImage::Allocate(int width, int height){ ASSERT(width>0 && height>0); if (m_data_is_local && (int)m_data.size()>=width*height) { m_width = width; m_height = height; } else { ASSERT(width>=0 && height>=0); m_width = width; m_height = height; m_data.resize(m_width*m_height); if (m_width*m_height>0) { m_pData = &m_data[0]; ASSERT(m_pData); } else { m_pData = NULL; } } m_data_is_local = true;}void CByteImage::Copy(const CByteImage& from){ Allocate(from.m_width, from.m_height); m_data = from.m_data;}void CByteImage::CopyArea(const CByteImage& from, const CRect& from_area, int new_width, int new_height) { Allocate(new_width, new_height); double step_x = (double)from_area.right/(double)m_width; double step_y = (double)from_area.bottom/(double)m_height; double from_y = from_area.top; for (int y=0; y<m_height; y++) { double from_x = from_area.left; for (int x=0; x<m_width; x++) { Pixel(x, y) = (BYTE) from.InterpolateLinear(from_x, from_y); from_x += step_x; } from_y += step_y; }}#ifdef DEBUGBYTE CByteImage::Pixel(int x, int y) const { if (!m_pData) { throw ITException("m_pData zero"); } if (!(0<=x && x<m_width && 0<=y && y<m_height)) { throw ITException("out of bounds"); } return m_pData[x + y*m_width];}BYTE& CByteImage::Pixel(int x, int y){ if (!m_pData) { throw ITException("m_pData zero"); } if (!(0<=x && x<m_width && 0<=y && y<m_height)) { throw ITException("out of bounds"); } return m_pData[x + y*m_width];}#endif // DEBUG#ifdef USE_MFCvoid CByteImage::PrepareDIB(){ m_bmi.pMap[0] = 0xff000000; for (int color=1; color<256; color++) { m_bmi.pMap[color] = m_bmi.pMap[color-1]+0x010101; } m_bmi.header.biSize = sizeof(BITMAPINFOHEADER); m_bmi.header.biPlanes = 1; m_bmi.header.biBitCount = (WORD) 8; m_bmi.header.biCompression = BI_RGB; m_bmi.header.biSizeImage = 0; m_bmi.header.biXPelsPerMeter = 3150; m_bmi.header.biYPelsPerMeter = 3150; m_bmi.header.biClrUsed = 0; m_bmi.header.biClrImportant = 0;}void CByteImage::SetBitsToDevice(CDC* pDC) const{ m_bmi.header.biWidth = m_width; m_bmi.header.biHeight = -m_height; SetDIBitsToDevice( pDC->m_hDC, // handle to DC 0, // x-coord of destination upper-left corner 0, // y-coord of destination upper-left corner m_width, // source rectangle width m_height, // source rectangle height 0, // x-coord of source lower-left corner 0, // y-coord of source lower-left corner 0, // first scan line in array m_height, // number of scan lines m_pData, // array of DIB bits (BITMAPINFO*) &m_bmi, // bitmap information DIB_RGB_COLORS // RGB or palette indexes );}#endif // USE_MFCvoid CByteImage::WriteToFile(FILE* fp){ fprintf(fp, "%dx%d\n", m_width, m_height); for (int y=0; y<m_height; y++) { for (int x=0; x<m_width; x++) { fprintf(fp, "%d ", Pixel(x, y)); } fprintf(fp, "\n"); }}void CByteImage::ReadFromFile(FILE* fp){ int width, height, scanned; scanned = fscanf(fp, "%dx%d\n", &width, &height); if (scanned!=2) { throw ITEFile("-", "file read error in CByteImage::ReadFromFile"); } Allocate(width, height); for (int y=0; y<m_height; y++) { for (int x=0; x<m_width; x++) { int val; scanned = fscanf(fp, "%d ", &val); if (scanned!=1 || val<0 || 255<val) { char buf[128]; sprintf(buf, "file read error in CByteImage::ReadFromFile, at(%d, %d)", x, y); throw ITEFile("-", buf); } Pixel(x, y) = (BYTE) val; } }}int iround(double f) { return (int)(f+0.5);}double CByteImage::InterpolateNearestNeighbor(double x, double y) const{ int rx = iround(x); if (rx<0 || m_width<=rx) { return 0; } int ry = iround(y); if (ry<0 || m_height<=ry) { return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -