histeq.cpp

来自「barcode readers [ from Image]」· C++ 代码 · 共 101 行

CPP
101
字号
//
// Histogram equalization
//
// Copyright (C) 2003, 2006 by Jon A. Webb (Contact via GMail; username is jonawebb)
//
// 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.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//


#include "histeq.h"

#include "Debug.h"
#include "GrayImageIter.h"
#include "Image.h"
#include "ImageSize.h"

using namespace Core;

namespace Algorithm
{
	EXPORT_C CHistEq* CHistEq::NewL()
	{	
		return new (ELeave) CHistEq();	
	}

	CHistEq::CHistEq(void) :
		ibEmpty(true)
	{
	}

	CHistEq::~CHistEq(void)
	{
	}

	CImage CHistEq::FrontL() 
	{
		if (ibEmpty) {
			User::Leave(KErrGeneral);
			return CImage();
		}
		ibEmpty = true;
		return iImage;
	}

	void CHistEq::PushL(CImage image)
	{
		IImageSize rSize(image);
		int nHist[256] = {0};
		// build a histogram
		image.LockLC();
		IGrayImageIter rGray(image);
		for (; !rGray.End(); rGray.NextRow()) {
			for (; !rGray.REnd(); rGray++) {
				nHist[rGray()] ++;
			}
		}
		CleanupStack::PopAndDestroy(); // unlock bitmaps
		// transform the histogram into a cumulative distribution
		int i=1;
		for (; i<256; i++) {
			nHist[i] = nHist[i-1] + nHist[i];
		}
		unsigned int nPerBin = rSize.Width() * rSize.Height() / 256;
		// use the cumulative distribution to make a lookup table
		unsigned char ucLut[256];
		for (i=0; i<256; i++) {
			ucLut[i] = (unsigned char) Min(255, nHist[i]/nPerBin);
		}
		// apply the lookup table
		image.LockLC(); 
		for (rGray = IGrayImageIter(image);  // reinitialize in case unlock-lock moved bitmap
			!rGray.End(); 
			rGray.NextRow()) {
			for (; !rGray.REnd(); rGray++) {
				rGray() = ucLut[rGray()];
			}
		}	
		CleanupStack::PopAndDestroy(); // unlock bitmaps
		iImage = image;
		CDebug::ShowImage(iImage);
		ibEmpty = false;
	}

	bool CHistEq::Empty() const
	{
		return ibEmpty;
	}
};

⌨️ 快捷键说明

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