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

📄 colorimgdoc.cpp

📁 I used MFC. that code is not perfect and have some problem. But its function is Ok. You just fix
💻 CPP
字号:
// ColorImgDoc.cpp : implementation of the CColorImgDoc class
//

#include "stdafx.h"
#include "ColorImg.h"

#include "ColorImgDoc.h"

#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CColorImgDoc

IMPLEMENT_DYNCREATE(CColorImgDoc, CDocument)

BEGIN_MESSAGE_MAP(CColorImgDoc, CDocument)
	//{{AFX_MSG_MAP(CColorImgDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorImgDoc construction/destruction

CColorImgDoc::CColorImgDoc()
{
	// TODO: add one-time construction code here

}

CColorImgDoc::~CColorImgDoc()
{
}

BOOL CColorImgDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CColorImgDoc serialization

void CColorImgDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar.Write(m_ResultImg, 512 * 512 * 3);		//m_ResultImg 俊 乐绰 康惑阑 颇老肺 历厘
	}
	else
	{
		// TODO: add loading code here
		CFile* fp = ar.GetFile();				//颇老 器牢磐甫 掘绰促.
		if(fp->GetLength() != 512 * 512 * 3)			//颇老狼 农扁甫 眉农
		{			
			AfxMessageBox("512 * 512 Color 捞固瘤啊 酒凑聪促.");
			return;
		}
		
		ar.Read(m_OpenImg, fp->GetLength());	//捞固瘤甫 m_OpenImg 肺 佬绢柯促.
		
		for(int i=0;i<512;i++)
			{
				for(int j=0;j<512;j++)
				{
					m_RImg[i][j]=m_OpenImg[0][i][j];
					m_GImg[i][j]=m_OpenImg[1][i][j];
					m_BImg[i][j]=m_OpenImg[2][i][j];
				}
			}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CColorImgDoc diagnostics

#ifdef _DEBUG
void CColorImgDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CColorImgDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CColorImgDoc commands

void CColorImgDoc::HistoView()
{
	int f=0, sum=0;
	int hist[3][512];
	int value=0;
	unsigned char tmp[3][512][512];
	
	//檬扁拳
	for (int k=0; k<3; k++)
	{
		for (int z=0; z<512; z++)
		{
			hist[k][z]=0;
			
			for (int w=0; w<512; w++)
			{
				tmp[k][z][w]=255;
			}
		}
	}

	//疙档蔼狼 后档荐 炼荤
	for (k=0; k<3; k++)
	{
		for (int i=0; i<512; i++)
		{
			for (int j=0; j<512; j++)
			{
				f=m_OpenImg[k][i][j];
				hist[k][f]=hist[k][f]+1; 
			}
		}
	}

	for (k=0; k<3; k++)
	{
		for (int x=0; x<512; x++)
		{
			value = hist[k][x]/10;
			if(value > 512) value=512;
			
			for (int y=0; y<value; y++)
			{
				tmp[k][y][x]=128;
			}
		}
	}

	int y2=0;

	for (k=0; k<3; k++)
	{
		for (int y=0; y<512; y++)
		{
			for (int x=0; x<512; x++)
			{
				y2=(512 - y);
				m_ResultImg2[k][y2][x] = tmp[k][y][x];
			}
		}
	}
}

void CColorImgDoc::Mean()
{
	int i, j, x, y; 

	int sum=0;

	for (int k=0; k<3; k++)
	{
		for (i=1; i<511; i++)
		{
			for (j=1; j<511; j++)
			{
				for (x=0; x<3; x++)
				{
					for (y=0; y<3; y++)
					{
						sum += (m_OpenImg[k][i+x-1][j+y-1] );
						
					}
				}
				m_ResultImg[k][i][j] = sum / 9;
				m_ResultImg2[k][i][j] = sum / 9;
				sum = 0;
			}
		}
	}
}

void CColorImgDoc::RGBtoHSI()
{
	double angle, i_hsi;
	float s_hsi;
	double h;
	int i, j;
	int r, g, b;
	int min;
	
	for(i=0; i<512; i++)
	{
		for(j=0; j<512; j++)
		{	
			r=m_RImg[i][j];
			g=m_GImg[i][j];
			b=m_BImg[i][j];

			i_hsi=((r+g+b)/3.0);
			m_IImg[i][j]=(unsigned char)i_hsi;
			
			if(r >= g )
			{
				min = g;
			}
			else
			{
				min = r;
			}

			if(min >= b)
			{
				min = b;
			}
			else
			{
				min = min;
			}

			s_hsi = min*255 / (r+g+b);
			s_hsi = 1.0-3 * s_hsi / 255;
			s_hsi = s_hsi * 255;
			
			if (s_hsi > 255)
			{
				s_hsi=255;	
			}
			else if(s_hsi < 0)
			{
				s_hsi = 0;
			}
			
			m_SImg[i][j]=(unsigned char)s_hsi;

			if((r==g)&&(g==b))
				m_HImg[i][j]=0;
			else
			{
				angle=(0.5*((r-g)+(r-b)))/(sqrt(pow((r-g),2.0)+(r-b)*(g-b)));
				h=acos(angle);
				h=h*(57.2958);

				if(b/(int)(m_IImg[i][j]+1)>g/(int)(m_IImg[i][j]+1))
				{
					h=360.0-h;
				}
				m_HImg[i][j]=(unsigned char)((h/360.0)*255.0);
			}

		}
	}
}

⌨️ 快捷键说明

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