Fork me on GitHub

【图像算法】彩色图像分割专题二:显示屏幕上任意点颜色值

【图像算法】彩色图像分割专题一:显示屏幕上任意点的颜色值

SkySeraph May 13rd 2011  HQU

Email:zgzhaobo@gmail.com    QQ:452728574

Latest Modified Date:May 13rd 2011 HQU

》说明:

1  分析彩色图像时,不同空间的选择,需要实时知道图像上某点的颜色值,参考网上相关资料,实现实时显示屏幕上任意点的RGB/HSV/YIQ值,当然还可以是其它色彩空间的值,原理类似没在重复。

2  关于颜色空间相关知识,请参考:

http://www.cnblogs.com/skyseraph/archive/2011/05/03/2035643.html

http://www.cnblogs.com/skyseraph/archive/2011/05/05/2038317.html

http://www.cnblogs.com/skyseraph/archive/2011/05/05/2038308.html

3  类似软件

http://www.broadhurst-family.co.uk/lefteye/MainPages/Lab.htm   RGB/Lab/HSV软件

  

》实现(源码):

响应函数

//////////////////////////////////////////////////////////////////////////
void CColorSegDlg::OnFetchColor() 
//  获取屏幕上任意点的RGB值并显示
{
    m_bSnapFlag^=TRUE;
    if (m_bSnapFlag)  //开启屏幕取色
    {
        m_ColorPicker.SetWindowText("Stop Fetch");
        SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
        SetTimer(1, 80, NULL); //80ms触发一次定时器		
    }
    else
    { 
        m_ColorPicker.SetWindowText("Start Fetch");
        SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
        KillTimer(1);
    }
//注:另一种方法:不采用bool标记m_bSnapFlag,
//采用if(m_ColorPicker.GetCheck()) 但这种方法不能实现菜单的控制
}

Time函数

void CColorSegDlg::OnTimer(UINT nIDEvent) 
{
	if (nIDEvent != 1)
        return;
	
    CPoint pt;
    GetCursorPos(&pt);
	
    HDC hDC = ::GetDC(NULL);
    COLORREF clr = ::GetPixel(hDC, pt.x, pt.y);
	int R=GetRValue(clr);
	int G=GetGValue(clr);
	int B=GetBValue(clr);
	
    //  输出RGB值
	CString ClrText;
    ClrText.Format("%d",R);//GetRValue(clr)
	m_EditRed.SetWindowText(ClrText);
	ClrText.Format("%d",G);//GetGValue(clr)
	m_EditGreen.SetWindowText(ClrText);
	ClrText.Format("%d",B);//GetBValue(clr)
	m_EditBlue.SetWindowText(ClrText);	
	
	CString cs;
	
	//  HSV	
	double hsvH,hsvS,hsvV;
	pMyColorSpace.MyRGB2HSV(R,G,B,hsvH,hsvS,hsvV,1);
	
	//  输出HSV值
	cs.Format("%.3f",hsvH);
	GetDlgItem(IDC_H)->SetWindowText(cs);
	cs.Format("%.3f",hsvS);
	GetDlgItem(IDC_S)->SetWindowText(cs);
	cs.Format("%.3f",hsvV);
	GetDlgItem(IDC_V)->SetWindowText(cs);
	
	//  YUV && YIQ
	double yuvY,yuvU,yuvV,yiqY,yiqI,yiqQ;
	pMyColorSpace.MyRGB2YUV(R,G,B,yuvY,yuvU,yuvV);
	pMyColorSpace.MyRGB2YIQ(R,G,B,yiqY,yiqI,yiqQ);
	
	//int nTH=int(atan(fabs(yuvV/yuvU))*180.0/3.1415926);
	
	//  输出YIQ值	
	cs.Format("%.3f",yiqQ);
	GetDlgItem(IDC_yiqQ)->SetWindowText(cs);
	cs.Format("%.3f",yiqI);
	GetDlgItem(IDC_yiqI)->SetWindowText(cs);
	cs.Format("%.3f",yiqY);
	GetDlgItem(IDC_yiqY)->SetWindowText(cs);
	
	//  显示区域
	CRect m_rect;
	m_Color.GetClientRect(m_rect);
	CDC* dc = m_Color.GetDC();
	CBrush m_brush(RGB(GetRValue(clr),GetGValue(clr),GetBValue(clr)));
	dc->FillRect(m_rect,&m_brush);
	
    ::ReleaseDC(NULL, hDC);
	CDialog::OnTimer(nIDEvent);
}

 

》效果

 

完整界面见:

http://www.cnblogs.com/skyseraph/archive/2011/05/05/2038308.html

 

 

Author:         SKySeraph

Email/GTalk: zgzhaobo@gmail.com    QQ:452728574

From:         http://www.cnblogs.com/skyseraph/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,请尊重作者的劳动成果

 

posted @ 2011-05-13 20:29  SkySeraph  阅读(2734)  评论(0编辑  收藏  举报