Baumer industrial camera Baumer camera how to combine OpenCV with BGAPI SDK for simple image stitching and display (C++)

Baumer industrial camera

Baumer Industrial Cameras Baumer cameras are high-performance, high-quality industrial cameras that can be used in various application scenarios, such as object detection, counting and recognition, motion analysis and image processing.

Baumer's 10 Gigabit cameras have excellent image processing performance and can transmit high-resolution images in real time. In addition, the camera features fast data transfer, low power consumption, easy integration, and high scalability. ​When
Baumer

The technical background of Baumer industrial camera SDK combined with OpenCVSharp

The Baumer Industrial Camera SDK is a software development kit for communication and image acquisition with industrial cameras. These SDKs usually contain drivers and APIs that allow developers to write applications in multiple programming languages ​​(e.g. C++, C#, Python). They also provide control options for many image parameters and camera parameters in order to meet various application needs.

OpenCV is a popular and widely used computer vision library that provides a large number of image processing and computer vision algorithms, such as image filtering, feature extraction, object detection, etc. OpenCV can be integrated with the industrial camera SDK to process and analyze the images collected from the camera.

Using the Industrial Camera SDK in conjunction with OpenCV, developers can implement higher-level image processing and vision analysis applications. For example, they can use the industrial camera SDK for image acquisition and real-time display, and then use OpenCV for image processing and object detection. They can also use OpenCV's computer vision algorithms for specific applications, such as quality control, robot vision navigation, and automatic recognition.
​Here
mainly describes how to implement the core code of image conversion through BGAPI SDK and OpenCV under the platform of C#. The callback function in this article will realize the function of splicing and displaying four images.

code analysis

This article introduces the function of using BGAPI SDK and OpenCVSharp to stitch and display images when developing Baumer's JPEG industrial cameras using BGAPI SDK

1. Reference the appropriate class file

The core code in the C++ environment is as follows:
.h file

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2\opencv.hpp>

.cpp file

#pragma comment(lib, "opencv_world341.lib")
#pragma comment(lib, "opencv_world341d.lib")

2. Perform Buffer image conversion and splicing in the callback function

The core code for subsequently converting the image into a Mat image of the OpenCV library and splicing and displaying it is as follows:

void BGAPI2CALL BufferHandler( void * callBackOwner, Buffer * pBufferFilled )
{
    
    
	CGigeDemoDlg* pDlg = (CGigeDemoDlg*)callBackOwner;
	unsigned char* imagebuffer = NULL;
	USES_CONVERSION;
	try
	{
    
    
		if(pBufferFilled == NULL)
		{
    
    

		}
		else if(pBufferFilled->GetIsIncomplete() == true)
		{
    
    
			// queue buffer again
			pBufferFilled->QueueBuffer();
		}
		else
		{
    
    
			
			pDlg->FrameID= pBufferFilled->GetFrameID();                                                 //获取当前图像FrameID显示帧率

			int width = 0, height = 0;
			width = (int)pBufferFilled->GetWidth();height = (int)pBufferFilled->GetHeight();			//获取当前图像像素长宽
			CString PixelFormat1 = (CString)pBufferFilled->GetPixelFormat();							//获取当前图像像素格式				
			imagebuffer = (BYTE*)((bo_int64)pBufferFilled->GetMemPtr()+pBufferFilled->GetImageOffset());//获取当前图像数据
					



			#pragma  region //保存图像功能
			if(pDlg->m_bSaveImage &&!pDlg->m_strDirectory.IsEmpty())
			{
    
    
				/*CTime time = CTime::GetCurrentTime(); 
				CString strtime;
				strtime.Format(_T("\\%4d%2d%2d%2d%2d%2d"),time.GetYear(),time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond());
				CString  strpath = pDlg->m_strDirectory+strtime+".jpg";
				pDlg->SaveImageMono(strpath, imagebuffer,width,height);*/
				pDlg->m_bSaveImage = false;

				#pragma region 相机中内存图像数据转换为opencv里的Mat数据
				CTime time = CTime::GetCurrentTime(); 
				CString strtime;
				strtime.Format(_T("\\%4d%2d%2d%2d%2d%2d"),time.GetYear(),time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond());
				CString strpath2 =_T("C:\\Users\\BAUMER\\Desktop\\")+strtime+"Mat.jpg";
				cv::String cvstrpath = W2A(strpath2);
				cv::Mat* imgbuf2 = new cv::Mat((int)pBufferFilled->GetHeight(),(int)pBufferFilled->GetWidth(),CV_8UC1,(char *)pBufferFilled->GetMemPtr());
				cv::Mat imOriginal2 = cv::imdecode(*imgbuf2, CV_LOAD_IMAGE_GRAYSCALE); //将Mat指针数据转换为Mat数据
				cv::imwrite(cvstrpath, *imgbuf2); //保存图片
				#pragma endregion

			}
			#pragma endregion 

			Gdiplus::Rect rc = Gdiplus::Rect(0,0,width,height);

			#pragma region 黑白相机代码:像素格式为mono时转Bitmap的代码,彩色相机此处代码不同
			if(pDlg->m_pBitmap == NULL)
			{
    
    
				pDlg->m_pBitmap = new Gdiplus::Bitmap(width,height,PixelFormat8bppIndexed);
			}
			Gdiplus::BitmapData lockedbits;
			Gdiplus::ColorPalette * pal = (Gdiplus::ColorPalette*)new BYTE[sizeof(Gdiplus::ColorPalette)+255*sizeof(Gdiplus::ARGB)];
			pal->Count=256;
			for(UINT i=0;i<256;i++)
			{
    
    
				UINT color=i*65536+i*256+i;
				color= color|0xFF000000;
				pal->Entries[i]=color;
			}			
			pDlg->m_pBitmap->SetPalette(pal);
			Gdiplus::Status ret = pDlg->m_pBitmap->LockBits(&rc,Gdiplus::ImageLockModeWrite,PixelFormat8bppIndexed,&lockedbits);
			BYTE* pixels = (BYTE*)lockedbits.Scan0;
			BYTE* src = (BYTE*)imagebuffer;//这里将使用转换后的数据imagebuffer2
			for (int row = 0; row < height; ++row) 
			{
    
    
				CopyMemory(pixels, src, lockedbits.Stride);
				pixels += width;
				src += width;
			}
			pDlg->m_pBitmap->UnlockBits(&lockedbits);
			#pragma endregion 
			
			#pragma region //在C++中对图像使用opencv的拼接算法转换
			cv::Mat* imgbufnew = new cv::Mat((int)pBufferFilled->GetHeight(),(int)pBufferFilled->GetWidth(),CV_8UC1,(char *)pBufferFilled->GetMemPtr());
			cv::Mat imOriginalnew = cv::imdecode(*imgbufnew , CV_LOAD_IMAGE_GRAYSCALE); //将Mat指针数据转换为Mat数据
			cv::Mat imConvertnew;
			
			cv::Mat  Matgray2 = imOriginalnew ;
			cv::Mat  Matgray3 = imOriginalnew ;
			cv::Mat  Matgray4 = imOriginalnew ;

			cv::Mat panorama1;
			cv::Mat panorama2;
			
			cv::VConcat(Matgray1, Matgray2, &panorama1);
			cv::VConcat(Matgray3, Matgray4, &panorama2);
			cv::HConcat(panorama1, panorama2, &imConvertnew);
			
			
			// 转换成Gdiplus::Bitmap对象			
			Gdiplus::Bitmap* bitmapImage = new Gdiplus::Bitmap(imConvertnew.cols, imConvertnew.rows,  imConvertnew.cols, PixelFormat8bppIndexed, (BYTE*)imConvertnew.data);
			pDlg->m_pBitmap = bitmapImage;
			#pragma endregion 


			#pragma region //将图像显示在PictureControl控件上
			HDC hDC = ::GetDC(pDlg->m_stcPicture.m_hWnd);
			Gdiplus::Graphics GdiplusDC(hDC);
			CRect rcControl;
			pDlg->m_stcPicture.GetWindowRect(&rcControl);
			Gdiplus::Rect rtImage(0,0,rcControl.Width(),rcControl.Height());
			GdiplusDC.DrawImage(pDlg->m_pBitmap,rtImage,0,0,width,height, Gdiplus::UnitPixel);
		
			delete []pal;
			::ReleaseDC(pDlg->m_stcPicture.m_hWnd,hDC);

			delete pDlg->m_pBitmap ;
			pDlg->m_pBitmap =NULL;
			#pragma endregion 

			// queue buffer again
			pBufferFilled->QueueBuffer();
		}
	}
	catch (BGAPI2::Exceptions::IException& ex)
	{
    
    
		CString str;
		str.Format(_T("ExceptionType:%s! ErrorDescription:%s in function:%s"),ex.GetType(),ex.GetErrorDescription(),ex.GetFunctionName());		
	}	
}

3. The specific application of OpenCV image stitching

The C++ calling code is as follows:

#region//对四张图像进行基础拼接
cv::Mat* imgbufnew = new cv::Mat((int)pBufferFilled->GetHeight(),(int)pBufferFilled->GetWidth(),CV_8UC1,(char *)pBufferFilled->GetMemPtr());
cv::Mat imOriginalnew = cv::imdecode(*imgbufnew , CV_LOAD_IMAGE_GRAYSCALE); //将Mat指针数据转换为Mat数据
cv::Mat imConvertnew;
			
cv::Mat  Matgray2 = imOriginalnew ;
cv::Mat  Matgray3 = imOriginalnew ;
cv::Mat  Matgray4 = imOriginalnew ;

cv::Mat panorama1;
cv::Mat panorama2;
			
cv::VConcat(Matgray1, Matgray2, &panorama1);
cv::VConcat(Matgray3, Matgray4, &panorama2);
cv::HConcat(panorama1, panorama2, &imConvertnew);
#endregion

The advantages of converting industrial camera images to Mat images through OpenCV

Low-level image processing: OPENCV provides a rich set of libraries for low-level image processing. It allows easy access to image characteristics such as contrast, brightness and color correction.

Real-time video processing: With OPENCV, you can process video streams in real-time, allowing instant feedback and adjustments to the processing.

Accurate Object Detection: OPENCV provides advanced object detection and recognition algorithms that can accurately identify and track objects in video streams.

Efficient hardware utilization: OPENCV is designed to maximize hardware utilization, making it an efficient video processing platform.

Cross-platform compatibility: OPENCV is compatible with a variety of operating systems, making it easy to integrate into existing software systems.

In general, converting industrial camera images to Mat images through OPENCV can achieve efficient, accurate, and real-time image processing and analysis, making it a powerful tool for industrial applications.

Industrial application of converting industrial camera images to Mat images through OpenCV

Automated production control: Industrial cameras can be used for automated production control. After converting the images captured by the SDK into MAT images of OPENCV, image processing technology can be used to detect, classify, and count products to realize automated production control.

Intelligent transportation: Industrial cameras can be used for intelligent transportation. After the images captured by them are converted into MAT images of OPENCV through SDK, image processing technology can be used to identify, count, and track vehicles to realize intelligent traffic management.

Medical imaging: Industrial cameras can be used for medical imaging. After the images captured by them are converted into OPENCV MAT images through SDK, image processing technology can be used to analyze and diagnose medical images to improve the accuracy and efficiency of medical diagnosis.

Logistics warehousing: Industrial cameras can be used in logistics warehousing. After the images captured by them are converted into MAT images of OPENCV through SDK, image processing technology can be used to monitor, manage, and intelligentize the logistics warehousing process, improving the efficiency of logistics warehousing and safety.

Video surveillance: Industrial cameras can be used for video surveillance. After converting the images captured by the SDK into MAT images of OPENCV, image processing technology can be used to analyze, identify, and track video images to realize intelligent video surveillance.

Guess you like

Origin blog.csdn.net/xianzuzhicai/article/details/130354502