[PYTHON] Convert images from FlyCapture SDK to a form that can be used with openCV

Introduction

FlyCapture SDK

This is a dedicated SDK for handling Point Gray CCD cameras that are often used in research. Since the camera cannot be opened from ʻopenCV, it is necessary to convert it to Mat type` if you want to process it. The platform is

The language is

Is supported.

Download the SDK from here [https://www.ptgrey.com/flycapture-sdk) (registration required) There is also an official reference in PDF, but unfortunately it is in English.

Language to use

This time, we will deal with conversion in C ++ and Python. I can't explain about other languages because I haven't touched them in the first place, but I think that the things to do are almost the same.

Also, this time we are using ʻopencv 3.10`.

C++

VisualStudio2015 will be used, and environment construction will be omitted.


	Error error;
	BusManager busMgr;
	unsigned int numCameras;
	PGRGuid guid;
	Camera cam;
	Image rawImage, convertedImage;
	IplImage *cv_image;
	cv::Mat imgSub;
	cv::Rect rect;


    //=====================
    //Start the camera
    //=====================

	error = busMgr.GetNumOfCameras(&numCameras);
	if (error != PGRERROR_OK) {
		error.PrintErrorTrace();
		return -1;
	}

	printf("Number of cameras detected: %u\n", numCameras);
	if (numCameras != 1)
		return -1;

	error = busMgr.GetCameraFromIndex(0, &guid);
	if (error != PGRERROR_OK) {
		error.PrintErrorTrace();
		return -1;
	}

	error = cam.Connect(&guid);
	if (error != PGRERROR_OK) {
		error.PrintErrorTrace();
		return -1;
	}

	error = cam.StartCapture();
	if (error != PGRERROR_OK) {
		error.PrintErrorTrace();
		return -1;
	}

	error = cam.RetrieveBuffer(&rawImage);
	if (error != PGRERROR_OK) {
		error.PrintErrorTrace();
		return -1;
	}

	error = cam.RetrieveBuffer(&rawImage);
	if (error != PGRERROR_OK) {
		error.PrintErrorTrace();
		return -1;
	}


    //=====================
    //  Image => cv::Mat
    //=====================


    cv::Mat writer_img(h, w, CV_8UC3, convertedImage.GetData());

    ////In my environment, it is upside down, so add the following line
    //cv:Flip(writer_img, writer_img);

In the comments, yumetodo pointed out and changed the code. The operation has also been confirmed.

It worked if I rebuilt the environment to check the operation, but it didn't work in my environment for some reason. If you have similar symptoms, please refer to the code before the change.


    //=====================
    //  Image => cv::Mat
    //=====================

    //Convert camera image to IplImage
    error = rawImage.Convert(PIXEL_FORMAT_BGR, &convertedImage);
    if (error != PGRERROR_OK) {
        error.PrintErrorTrace();
        return -1;
    }

   //Copy destination cv_image initialization
    int w = rawImage.GetCols(), h = rawImage.GetRows();
    cv_image = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);

    // cv_Copy to image memory
    memcpy(cv_image->imageData, convertedImage.GetData(), w * h * 3);

    //In my environment, it is upside down, so add the following line
    //cvFlip(cv_image, cv_image); 

    //Convert from IplImage to Mat
    cv::Mat writer_img = cv::cvarrToMat(cv_image);

Python

The version of python used is 3.5.2. We have not confirmed the operation on the 2nd system, but since both Wrapper libraries are supported, I think that it will be a reference level.


import PyCapture2
import cv2

#============================
#Start the camera
#============================

bus = PyCapture2.BusManager()
cam = PyCapture2.Camera()
uid = bus.getCameraFromIndex(0)
cam.connect(uid)

numCams = bus.getNumOfCameras()
print("Number of cameras detected: ", numCams)
if not numCams:
	print("Insufficient number of cameras. Exiting...")
	exit()

cam.startCapture()


#============================
#Convert to a form that can be used with opencv
#============================

#Image acquisition
tmp_image = cam.retrieveBuffer()

#numpy array(python opencv uses numpy array)
row_bytes = float(len(tmp_image.getData()))/float(tmp_image.getRows())
cv_image = np.array(tmp_image.getData(), dtype="uint8").reshape((tmp_image.getRows(), tmp_image.getCols()));

#Specify color space(If you do not do this, you will get a gray image)
raw_image = cv2.cvtColor(cv_image, cv2.COLOR_BAYER_BG2BGR)

Recommended Posts

Convert images from FlyCapture SDK to a form that can be used with openCV
Convert mesh data exported from SpriteUV2 to a format that can be imported by Spine
Convert PIL format images read from form with Django to base64 format
File types that can be used with Go
Format DataFrame data with Pytorch into a form that can be trained with NN
I made a familiar function that can be used in statistics with Python
How to install a Python library that can be used by pharmaceutical companies
I wanted to quickly create a mail server that can be used freely with postfix + dovecot on EC2
Python knowledge notes that can be used with AtCoder
I made a program to convert images into ASCII art with Python and OpenCV
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
A mechanism to call a Ruby method from Python that can be done in 200 lines
Create a web API that can deliver images with Django
Let's make a diagram that can be clicked with IPython
[Python3] Code that can be used when you want to resize images in folder units
Create a program that can generate your favorite images with Selenium
[Python] Make a graph that can be moved around with Plotly
I tried to expand the database so that it can be used with PES analysis software
Make a Spinbox that can be displayed in Binary with Tkinter
A timer (ticker) that can be used in the field (can be used anywhere)
Understand the probabilities and statistics that can be used for progress management with a python program
I made a shuffle that can be reset (reverted) with Python
Make a currency chart that can be moved around with Plotly (2)
About the matter that torch summary can be really used when building a model with Pytorch
Comparison of 4 styles that can be passed to seaborn with set_context
Make a Spinbox that can be displayed in HEX with Tkinter
Make a currency chart that can be moved around with Plotly (1)
[Python] Try to recognize characters from images with OpenCV and pyocr
Convert facial images with PULSE to high image quality so that you can see pores and texture
Create a BOT that can call images registered with Discord like pictograms
I created a template for a Python project that can be used universally
Acoustic signal processing module that can be used with Python-Sounddevice ASIO [Application]
How to make a rock-paper-scissors bot that can be easily moved (commentary)
Create a web app that can be easily visualized with Plotly Dash
Mathematical optimization that can be used for free work with Python + PuLP
Draw a graph that can be moved around with HoloViews and Bokeh
I made a simple timer that can be started from the terminal
Acoustic signal processing module that can be used with Python-Sounddevice ASIO [Basic]
Convert from PDF to CSV with pdfplumber
[Python3] Code that can be used when you want to cut out an image in a specific size
It seems that cancelall childorders can be used to cancel all parent orders (special orders) with the bitflyer API
If you want to make a Windows application (exe) that can be actually used now using only Python
I want to create a priority queue that can be updated in Python (2.7)
A personal memo of Pandas related operations that can be used in practice
I made a Docker image that can call FBX SDK Python from Node.js
I tried to summarize the operations that are likely to be used with numpy-stl
Linux command that can be used from today if you know it (Basic)
"Gazpacho", a scraping module that can be used more easily than Beautiful Soup
A new form of app that works with GitHub: How to make GitHub Apps
Until youtube-dl can be used with Synology (DS120j)
Functions that can be used in for statements
Cannot upload multiple images from form with FastAPI
Convert PDFs to images in bulk with Python
Pretend to be a server with two PCs
List packages that can be updated with pip
A story that failed when trying to remove the suffix from the string with rstrip
A memo that detects and returns an image acquired from a webcam with Django's OpenCV
I tried to make a memo app that can be pomodoro, but a reflection record
From a book that programmers can learn: Converting characters that represent numbers to integer types
How to create a property of relations that can be prefetch_related by specific conditions
File sharing server made with Raspberry Pi that can be used for remote work