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.
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