MFC配置imagingSource工业摄像头和反色问题解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012839256/article/details/80790767

废话不多说,开始配置imagingSource摄像头

(一)配置imagingSource摄像头

(1)首先下载驱动程序,我这里用的是38Ux版本的摄像头,驱动如下:

https://www.theimagingsource.cn/%E6%94%AF%E6%8C%81/%E8%BD%AF%E4%BB%B6%E4%B8%8B%E8%BD%BD-windows/%E9%A9%B1%E5%8A%A8%E7%A8%8B%E5%BC%8F/icwdmuvccamtis33u/

如果使用其他版本的摄像头,请自己选择驱动程序:

https://www.theimagingsource.cn/%E6%94%AF%E6%8C%81/%E8%BD%AF%E4%BB%B6%E4%B8%8B%E8%BD%BD-windows/

(2)图像获取软件安装

非必需   下载该软件,测试imagingSource摄像头驱动是否安装成功,非必需安装,与接下来的sdk配置没有一点关系。软件下载路径:

https://www.theimagingsource.cn/%E6%94%AF%E6%8C%81/%E8%BD%AF%E4%BB%B6%E4%B8%8B%E8%BD%BD-windows/%E7%BB%88%E7%AB%AF%E7%94%A8%E6%88%B7%E8%BD%AF%E4%BB%B6/iccapture/

(3)下载用于MFC中的sdk,网址如下:

https://www.theimagingsource.cn/%E6%94%AF%E6%8C%81/%E8%BD%AF%E4%BB%B6%E4%B8%8B%E8%BD%BD-windows/%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7%E5%8C%85/icimagingcontrol/

(4)配置sdk

【属性管理器】->【VC++目录】->

【包含目录】中添加路径  D:\\IC Imaging Control 3.4\classlib\include;D:\\IC Imaging Control 3.4\classlib\win32\debug;

【库目录】中添加路径 D:\\IC Imaging Control 3.4\classlib\win32\debug;

【链接器】->【输入】引入  TIS_UDSHL11d.lib;imagingModule.lib

(5)将D:\\IC Imaging Control 3.4\classlib\win32\debug下的TIS_UDSHL11d.dll文件复制到项目文件夹下的Debug文件夹下

(6)所有准备工作完成,在你的项目中引入Listener.h和Listener.cpp两个文件,基本语句已经通过他俩给实现了。

listener.h
#if !defined(AFX_LISTENER_H__3E017E1D_6B0A_472C_9F9C_0C5F9A8DFB23__INCLUDED_)
#define AFX_LISTENER_H__3E017E1D_6B0A_472C_9F9C_0C5F9A8DFB23__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include "tisudshl.h"
#define MESSAGEDEVICELOST WM_USER+90
using namespace DShowLib;
class CListener : public GrabberListener  
{
	public:
		void SetViewCWnd( CWnd *pView);
		CListener();
		virtual ~CListener();
		virtual void deviceLost( Grabber& param); 
		void SetParent(CWnd* pParent);
		virtual void frameReady( Grabber& param, smart_ptr<MemBuffer> pBuffer, DWORD FrameNumber); 		

	protected:
		CWnd* m_pParent;
		CWnd* m_pDrawCWnd;
		SIZE m_WindowSize;		// Size of the window in which to draw the buffer.
		void DrawBuffer( smart_ptr<MemBuffer> pBuffer);
		void DoImageProcessing( smart_ptr<MemBuffer> pBuffer);				
};
#endif
Listener.cpp

#include "stdafx.h"
#include "Listener.h"
CListener::CListener()
{
	m_pParent = NULL;
	m_pDrawCWnd = NULL;
}
CListener::~CListener() 
{
}
void CListener::SetParent( CWnd *pParent)
{
	m_pParent = pParent;
}
void CListener::deviceLost( Grabber& param) 
{
	if( m_pParent != NULL)
		m_pParent->PostMessage(MESSAGEDEVICELOST,0,0);
}
void CListener::SetViewCWnd(CWnd *pView)
{
	m_pDrawCWnd = pView;
	RECT r;
	m_pDrawCWnd->GetClientRect(&r);
	m_WindowSize.cx = r.right;
	m_WindowSize.cy = r.bottom;
}
void CListener::frameReady( Grabber& param, smart_ptr<MemBuffer> pBuffer, DWORD FrameNumber)
{
	pBuffer->lock();
	DoImageProcessing( pBuffer );
	DrawBuffer(pBuffer);
	pBuffer->unlock();
}
void CListener::DrawBuffer( smart_ptr<MemBuffer> pBuffer)
{
	if( m_pDrawCWnd != NULL)
	{
		if( pBuffer != 0 )
		{ 
			CDC *pDC = m_pDrawCWnd->GetDC();
			pDC->SetStretchBltMode(COLORONCOLOR);
			smart_ptr<BITMAPINFOHEADER> pInf = pBuffer->getBitmapInfoHeader();
			int nLines = StretchDIBits( 	pDC->GetSafeHdc(),// Handle to the device
											0, 
											0,
											m_WindowSize.cx,	// Dest. rectangle width.
											m_WindowSize.cy,	// Dest. rectangle height.
											0,				// X-coordinate of lower-left corner of the source rect.
											0,				// Y-coordinate of lower-left corner of the source rect.
											pInf->biWidth,	// Source rectangle width.
											pInf->biHeight, // Number of scan lines.
											pBuffer->getPtr(),	// Modified address of array with DIB bits.
											reinterpret_cast<LPBITMAPINFO>( &*pInf ),	// Address of structure with bitmap info.
											DIB_RGB_COLORS,	// RGB or palette indices.
											SRCCOPY
											);
			m_pDrawCWnd->ReleaseDC(pDC);
		}	
	}	
}
void CListener::DoImageProcessing( smart_ptr<MemBuffer> pBuffer)
{
	smart_ptr<BITMAPINFOHEADER> pInf = pBuffer->getBitmapInfoHeader();
	BYTE* pImageData = pBuffer->getPtr();
	int iImageSize = pInf->biWidth * pInf->biHeight * pInf->biBitCount / 8 ;	
	for( int i = 0; i < iImageSize; i++)
	{
		pImageData[i] = pImageData[i];
	}
}

(7)在项目Dlg.h头文件下引入

#include "Listener.h"
(8)在Dlg.cpp文件下,通过下面代码进行摄像头的调用
#include <tisudshl.h>//必备头文件
using namespace DShowLib;//一定要引用该命名空间
CWnd* m_bmpShow=GetDlgItem(picture_concorl_id);//获取控件句柄
CDC *pdc = m_bmpShow->GetDC();
CRect rect;
m_bmpShow->GetWindowRect(&rect);	
//m_cStaticVideoWindow.SetWindowPos(NULL,0,0,300,220,SWP_FRAMECHANGED);//设置显示窗口大小
m_cGrabber.addListener(&m_cListener,GrabberListener::eFRAMEREADY|  
                                    GrabberListener::eOVERLAYCALLBACK);
m_cListener.SetParent(this);
m_cListener.SetViewCWnd(&m_cStaticVideoWindow);
m_pSink = FrameHandlerSink::create( DShowLib::eRGB24, 3);
m_pSink->setSnapMode( false ); 
m_cGrabber.setSinkType( m_pSink );
if( m_cGrabber.loadDeviceStateFromFile( "device.xml"))
{
	m_cGrabber.startLive(false); // The live video will be displayed by the CListener object.
}
SetButtonStates();

(二)反色问题


在这群草泥马奔腾而过之后,得出一下两个问题:

(1)RGB图像绘制区域,颜色取反

(2)分别对RGB三通道的像素用255去减

然后认为肯定是自己代码出错。

解决方案:

在Listener.cpp文件的最后有一个语句,导致反色问题。

pImageData[i] = 255-pImageData[i];

原因可能是:在写Listener.cpp代码时可能是在非vs环境下,因为vs和部分图像处理软件的0和255代表的颜色刚好相反,因此产生了反色,因此将上述代码的255-给去掉即可,而且效果也刚好是理想的情况。

pImageData[i] = pImageData[i];

至此,成功解决!!!

大笑大笑大笑

猜你喜欢

转载自blog.csdn.net/u012839256/article/details/80790767