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

📄 toolsimage.h

📁 C++ image processing.Mainly it occupies some filters to detect some prperties of image. Release.
💻 H
字号:
#ifndef ImageTools_h
#define ImageTools_h

/* 

This "SOFTWARE" is a free software.

You are allowed to download, use, modify and redistribute this software.
The software is provided "AS IS" without warranty of any kind.

Copyright: University of Koblenz-Landau, Dirk Balthasar

*/

#include <vector>
#include <algorithm>
#include <assert.h>
#include <ToolsMath.h>
#include <fstream>

/// tools for manimulation all kinds of images.
/// author Dirk Balthasar, 2002, private
namespace tools
{
	/// defines a region of interest
	class CROI
	{
	public:
		/// returns left coordinate of roi
		int x1()const{return m_Left;}
		/// returns upper coordinate of roi
		int y1()const{return m_Upper;}
		/// returns right coordinate of roi
		int x2()const{return m_Right;}
		/// returns lower coordinate of roi
		int y2()const{return m_Lower;}
		/// returns upper left
		void GetUpperLeft(int &x, int &y)const{x=m_Left; y = m_Upper;}
		/// creates roi
		CROI(int NewLeft = -1, int NewUpper = -1, int NewRight = -1, int NewLower = -1)
		{
			Set(NewLeft, NewUpper, NewRight, NewLower);
		}
		/// set roi to given parameters
		void Set(int NewLeft, int NewUpper, int NewRight, int NewLower)
		{
			m_Left = NewLeft; m_Upper = NewUpper; m_Right = NewRight; m_Lower = NewLower;
		}
		/// copy constructor
		CROI(const CROI &roi)
		{
			Set(roi.m_Left, roi.m_Upper, roi.m_Right, roi.m_Lower);
		}
		/// = operator
		CROI& operator=(const CROI &roi)
		{
			Set(roi.m_Left, roi.m_Upper, roi.m_Right, roi.m_Lower);
			return *this;
		}
		/// width of roi (x2()-x1()+1)
		int GetWidth()const{return m_Right - m_Left + 1;}
		/// height of roi (y2()-y1()+1)
		int GetHeight()const{return m_Lower - m_Upper + 1;}
		/// bring roi to valid order: m_Left <= m_Right, m_Upper < m_Lower
		void Sort()
		{
			if (m_Left > m_Right)
			{
				int tmp = m_Right;
				m_Right = m_Left;
				m_Left = tmp;
			}
			if (m_Upper > m_Lower)
			{
				int tmp = m_Lower;
				m_Lower = m_Upper;
				m_Upper = tmp;
			}
		}
	private:
		int m_Upper, m_Lower, m_Left, m_Right;
	};

	/// euclidian distance * euclidian distance
	inline int DistRGBEuklidPow2(int r1, int g1, int b1, int r2,int g2, int b2)
	{
		return (r1-r2)*(r1-r2)+(g1-g2)*(g1-g2)+(b1-b2)*(b1-b2);
	}

	/// draw a rectangle 
	template <class ImageType, class PixelType>
		static void DrawRectangle(ImageType &Image, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, PixelType Color)
	{
		DrawLine(Image, x1, y1, x2, y2, Color);
		DrawLine(Image, x2, y2, x3, y3, Color);
		DrawLine(Image, x3, y3, x4, y4, Color);
		DrawLine(Image, x4, y4, x1, y1, Color);
	}

	/// draw a filled rectangle
	template <class ImageType, class PixelType>
		static void DrawRectangleFilled(ImageType &Image, int x1, int y1, int x2, int y2, PixelType Color)
	{
		for (int y = y1; y <= y2; y++)
			DrawLine(Image, x1, y, x2, y, Color);
	}

	/// calculate mean in a given roi
	template <class ImageType, class AccessRGB>
		void Mean(ImageType &ImageRGB, const tools::CROI &ROI, float &mr, float &mg, float &mb)
	{ // mean
		mr = 0;
		mg = 0;
		mb = 0;
		for (int y = ROI.y1(); y <= ROI.y2(); y++)
		{	
			for (int x = ROI.x1(); x <= ROI.x2(); x++)
			{
				mr += AccessRGB::GetR(ImageRGB(x,y));
				mg += AccessRGB::GetG(ImageRGB(x,y));
				mb += AccessRGB::GetB(ImageRGB(x,y));
			}
		}
		float c = ROI.GetWidth()*ROI.GetHeight();
		mr /= c;
		mg /= c;
		mb /= c;
	}

	/// count number of pixels that are different between two images
	template <class ImageType>
	int DifferntPixelCount(ImageType &I1, ImageType &I2, const tools::CROI &ROI)
	{
		int different = 0;
		for (int y = ROI.y1(); y <= ROI.y2(); ++y)	
		{
			for (int x = ROI.x1(); x <= ROI.x2(); ++x)
			{
				if (I1(x,y) != I2(x,y))
					different++;
			}
		}
		return different;
	}

	/// calculate variance (and mean) in roi
	template <class ImageType, class AccessRGB>
		void Variance(ImageType &ImageRGB, const tools::CROI &ROI, float &VarR, float &VarG, float &VarB, float &MeanR, float &MeanG, float &MeanB)
	{ // variance, fast version
		float
			mr = 0,
			mg = 0,
			mb = 0,
			c = ROI.GetWidth() * ROI.GetHeight(),
			vr = 0, // r1

⌨️ 快捷键说明

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