Commits

iorodeo committed 6425665

Added simple blob finder demo.

Comments (0)

Files changed (6)

 else()
     message(STATUS "OpenCv not found")
 endif()
+message(STATUS "OpenCV_INCLUDE_DIRS = ${OpenCV_INCLUDE_DIRS}") 
 
 
 # FlyCapture2 library location - replace this this custom find_package

src/test/CMakeLists.txt

 cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
 
-project(bias_demo)
-add_executable(bias_demo bias_demo.cpp)
-target_link_libraries(bias_demo ${bias_ext_link_LIBS} bias_camera_facade)
+project(blob_finder)
+add_executable(blob_finder blob_finder_main.cpp blob_finder.cpp)
+target_link_libraries(blob_finder ${bias_ext_link_LIBS} bias_camera_facade)
 
 
 

src/test/bias_demo.cpp

-#if defined(WIN32) || defined(WIN64)
-#define _CRT_SECURE_NO_WARNINGS		
-#endif
-
-#include <iostream>
-#include "camera_facade.hpp"
-
-using namespace bias;
-
-int main(int argc, char** argv)
-{        
-
-    const float frameRate = 10.0;
-    const unsigned int numberOfFrames = 100;
-
-    std::cout << std::endl;
-    std::cout << "---------" << std::endl;
-    std::cout << "BIAS Demo" << std::endl;
-    std::cout << "---------" << std::endl;
-
-
-    // Find attached cameras
-    // -----------------------------------------------------------------------------
-    std::cout << "searching for cameras ... ";
-
-    CameraFinder camFinder;
-    CameraPtrList camPtrList = camFinder.createCameraPtrList();
-    if (camPtrList.empty())
-    {
-        std::cout << "no cameras found." << std::endl << std::endl;
-        return 0;
-    }
-
-    std::cout << camPtrList.size() << " cameras" << std::endl;
-    for (CameraPtrList::iterator it=camPtrList.begin(); it!=camPtrList.end(); it++)
-    {
-        CameraPtr camPtr = *it;
-        std::cout << "guid: " << camPtr -> getGuid().toString() << std::endl;
-    }
-
-
-    // Connect to first camera found
-    // -----------------------------------------------------------------------------
-    std::cout << "connecting to first camera ... ";
-
-    CameraPtr camPtr = camPtrList.front();
-    try
-    {
-        camPtr -> connect();
-    }
-    catch (RuntimeError &runtimeError)
-    {
-        std::cout << "error: " << runtimeError.what() << std::endl << std::endl;
-        return 0;
-    }
-    std::cout << "done" << std::endl;
-
-    // Print camera info
-    std::cout << "model:  " << camPtr -> getModelName() << std::endl;
-    std::cout << "vendor: " << camPtr -> getVendorName() << std::endl;
-
-    // Set camera videomode, trigger type, and framerate
-    // -----------------------------------------------------------------------------
-    std::cout << "setting videoMode to Format7 and trigger to internal ... ";
-    try
-    {
-        camPtr -> setVideoMode(VIDEOMODE_FORMAT7);
-        camPtr -> setTriggerInternal();
-    }
-    catch (RuntimeError &runtimeError)
-    {
-        std::cout << "error: " << runtimeError.what() << std::endl << std::endl;
-        return 0;
-    }
-    std::cout << "done" << std::endl;
-
-    std::cout << "setting framerate to " << frameRate << " fps ... ";
-    try
-    {
-        PropertyInfo  frameRateInfo = camPtr -> getPropertyInfo(PROPERTY_TYPE_FRAME_RATE);
-        Property frameRateProp = camPtr -> getProperty(PROPERTY_TYPE_FRAME_RATE);
-        frameRateProp.absoluteControl = true;
-        frameRateProp.absoluteValue = frameRate;
-        frameRateProp.autoActive = false;
-        camPtr -> setProperty(frameRateProp);
-        frameRateProp = camPtr -> getProperty(PROPERTY_TYPE_FRAME_RATE);
-    }
-    catch (RuntimeError &runtimeError)
-    {
-        std::cout << "error: " << runtimeError.what() << std::endl << std::endl;
-        return 0;
-    }
-    std::cout << "done" << std::endl;
-
-    // Start image capture
-    // -----------------------------------------------------------------------------
-    std::cout << "starting image capture ... ";
-    try
-    {
-        camPtr -> startCapture();
-    }
-    catch (RuntimeError &runtimeError)
-    {
-        std::cout << "error: " << runtimeError.what() << std::endl << std::endl;
-        return 0;
-    }
-    std::cout << "done" << std::endl;
-
-    // Grab images 
-    // -----------------------------------------------------------------------------
-    std::cout << "grabbing images" << std::endl;
-    for (unsigned int i=0; i< numberOfFrames; i++)
-    {
-        std::cout << " frame " << i << " ... ";
-        cv::Mat image = camPtr -> grabImage();
-        std::cout << "done" << std::endl;
-    }
-
-    // Stop image capture
-    // -----------------------------------------------------------------------------
-    std::cout << "stopping image capture ... ";
-    try
-    {
-        camPtr -> stopCapture();
-    }
-    catch (RuntimeError &runtimeError)
-    {
-        std::cout << "error: " << runtimeError.what() << std::endl << std::endl;
-        return 0;
-    }
-    std::cout << "done" << std::endl;
-
-
-    // Disconnect from camera
-    // -----------------------------------------------------------------------------
-    std::cout << "disconnecting camera ... ";
-    try
-    {
-        camPtr -> disconnect();
-    }
-    catch (RuntimeError &runtimeError)
-    {
-        std::cout << "error: " << runtimeError.what() << std::endl << std::endl;
-        return 0;
-    }
-    std::cout << "done" << std::endl;
-    std::cout << std::endl;
-
-	return 0;
-}
-

src/test/blob_finder.cpp

+#include "blob_finder.hpp"
+#include "camera_facade.hpp"
+#include <iostream>
+#include <vector>
+#include <opencv2/core/core.hpp>
+#include <opencv2/highgui/highgui.hpp>
+#include <opencv2/imgproc/imgproc.hpp>
+
+const float BlobFinder::DEFAULT_FRAMERATE = 10.0;
+const float BlobFinder::DEFAULT_DISPLAY_SCALE = 0.5;
+const double BlobFinder::DEFAULT_THRESHOLD = 100.0;
+const double BlobFinder::DEFAULT_MINIMUM_AREA = 50.0;
+const double BlobFinder::DEFAULT_MAXIMUM_AREA = 640.0*480.0;
+const double BlobFinder::THRESHOLD_MAXVAL = 255.0;
+
+
+BlobFinder::BlobFinder()
+{
+    frameRate_ = DEFAULT_FRAMERATE;
+    displayScale_ = DEFAULT_DISPLAY_SCALE;
+    threshold_ = DEFAULT_THRESHOLD;
+    minimumArea_ = DEFAULT_MINIMUM_AREA;
+    maximumArea_ = DEFAULT_MAXIMUM_AREA; 
+}
+
+
+BlobFinder::BlobFinder(float frameRate) : BlobFinder()
+{
+    frameRate_ = frameRate;
+}
+
+
+bool BlobFinder::run()
+{
+    bool success = true;
+
+    std::cout << std::endl;
+    std::cout << "* finding blobs - press 'x' to exit" << std::endl;
+
+    success = startCapture();
+    if (!success)
+    {
+        return false;
+    }
+    std::cout << std::endl;
+
+    cv::namedWindow("Raw Image", CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);
+    cv::namedWindow("Threshold Image", CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED); 
+    cv::namedWindow("Contour Image", CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED); 
+
+    unsigned long cnt = 0;
+
+    while (true)
+    {
+        cv::Mat image;
+        cv::Mat imageTemp;
+        cv::Mat imageThresh;
+        cv::Mat imageContour;
+        std::vector<std::vector<cv::Point>> contours;
+        std::vector<std::vector<cv::Point>> contoursFiltered;
+
+        // Grab frame from camera
+        std::cout << "  frame: " << cnt;
+        try
+        { 
+            image = cameraPtr_ -> grabImage();
+        }
+        catch (RuntimeError &runtimeError)
+        {
+            std::cout << " - error " << runtimeError.what() << std::endl;
+            continue;
+        }
+        std::cout << std::endl; 
+
+        // Threshold image, invert and find external contours 
+        cv::threshold(image,imageThresh, threshold_, THRESHOLD_MAXVAL, CV_THRESH_BINARY);
+        imageThresh = THRESHOLD_MAXVAL - imageThresh;
+        imageTemp = imageThresh.clone();
+        cv::findContours(imageTemp, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
+
+        // Filter contours by area
+        for (size_t index=0; index < contours.size(); index++)
+        {
+            cv::Moments contourMoments = cv::moments(contours[index]);
+            if ((contourMoments.m00 >= minimumArea_) && (contourMoments.m00 <= maximumArea_))
+            {
+                contoursFiltered.push_back(contours[index]);
+
+                double centroidX = contourMoments.m10/contourMoments.m00;
+                double centroidY = contourMoments.m01/contourMoments.m00;
+                int numBlob = contoursFiltered.size() - 1;
+                std::cout << "    blob: " << numBlob; 
+                std::cout << ", (" << centroidX << ", " << centroidY << ") " << std::endl;
+            }
+        }
+        std::cout << std::endl;
+
+        // Draw contours on image
+        imageContour = cv::Mat(image.size(), CV_8UC3, cv::Scalar(0,0,0));
+        for (size_t index=0; index < contoursFiltered.size(); index++)
+        {
+            cv::drawContours(imageContour, contoursFiltered, index, cv::Scalar(0,0,255));
+        }
+
+        // Display images
+        cv::resize(image,imageTemp, cv::Size(0,0), displayScale_, displayScale_);
+        cv::imshow("Raw Image", imageTemp);
+
+        cv::resize(imageThresh,imageTemp, cv::Size(0,0), displayScale_, displayScale_);
+        cv::imshow("Threshold Image", imageTemp);
+
+        cv::resize(imageContour, imageTemp, cv::Size(0,0), displayScale_, displayScale_); 
+        cv::imshow("Contour Image", imageTemp);
+
+        // Look for 'x' key press as signal to quit
+        int key = cv::waitKey(1);
+        if (key == 120)
+        {
+            break;
+        }
+
+        cnt++;
+    }
+
+    success = stopCapture();
+    if (!success)
+    {
+        return false;
+    }
+
+    return true;
+}
+
+bool BlobFinder::setupCamera()
+{
+    std::cout << std::endl;
+    std::cout << "* setup camera" << std::endl << std::endl;
+
+    std::cout << "  searching for cameras ... ";
+    CameraFinder cameraFinder;
+    CameraPtrList cameraPtrList = cameraFinder.createCameraPtrList();
+    if (cameraPtrList.empty())
+    {
+        std::cout << "no cameras found." << std::endl;
+        return false;
+    }
+
+    std::cout << cameraPtrList.size() << " cameras" << std::endl;
+    for (CameraPtrList::iterator it=cameraPtrList.begin(); it!=cameraPtrList.end(); it++)
+    {
+        CameraPtr cameraPtr_ = *it;
+        std::cout << "  guid: " << cameraPtr_ -> getGuid().toString() << std::endl;
+    }
+
+    std::cout << "  connecting to first camera ... ";
+    cameraPtr_ = cameraPtrList.front();
+
+    if (cameraPtr_ -> isConnected()) 
+    {
+        std::cout << "error: camera already connected" << std::endl;
+        return false;
+    }
+
+    try
+    {
+        cameraPtr_ -> connect();
+    }
+    catch (RuntimeError &runtimeError)
+    {
+        std::cout << "error: " << runtimeError.what() << std::endl;
+        return false;
+    }
+    std::cout << "done" << std::endl;
+
+    std::cout << "  model:  " << cameraPtr_ -> getModelName() << std::endl;
+    std::cout << "  vendor: " << cameraPtr_ -> getVendorName() << std::endl;
+
+    std::cout << "  setting videoMode=Format7, trigger=internal ... ";
+    try
+    {
+        cameraPtr_ -> setVideoMode(VIDEOMODE_FORMAT7);
+        cameraPtr_ -> setTriggerInternal();
+    }
+    catch (RuntimeError &runtimeError)
+    {
+        std::cout << "error: " << runtimeError.what() << std::endl;
+        return false;
+    }
+    std::cout << "done" << std::endl;
+
+    std::cout << "  setting framerate to " << frameRate_ << " fps ... ";
+    try
+    {
+        PropertyInfo  frameRateInfo = cameraPtr_ -> getPropertyInfo(PROPERTY_TYPE_FRAME_RATE);
+        Property frameRateProp = cameraPtr_ -> getProperty(PROPERTY_TYPE_FRAME_RATE);
+        frameRateProp.absoluteControl = true;
+        frameRateProp.absoluteValue = frameRate_;
+        frameRateProp.autoActive = false;
+        cameraPtr_ -> setProperty(frameRateProp);
+        frameRateProp = cameraPtr_ -> getProperty(PROPERTY_TYPE_FRAME_RATE);
+    }
+    catch (RuntimeError &runtimeError)
+    {
+        std::cout << "error: " << runtimeError.what() << std::endl;
+        return false;
+    }
+    std::cout << "done" << std::endl;
+    std::cout << std::endl;
+
+    return true;
+}
+
+
+bool BlobFinder::cleanUp()
+{
+
+    std::cout << std::endl;
+    std::cout << "* clean up" << std::endl << std::endl;
+    std::cout << "  disconnecting camera ... ";
+
+    if (!(cameraPtr_ -> isConnected()))
+    {
+        std::cout << "already disconnected" << std::endl;
+        return false;
+    }
+
+    try
+    {
+        cameraPtr_ -> disconnect();
+    }
+    catch (RuntimeError &runtimeError)
+    {
+        std::cout << "error: " << runtimeError.what() << std::endl;
+        return false;
+    }
+    std::cout << "done" << std::endl;
+    std::cout << std::endl;
+    return true;
+}
+
+
+
+bool BlobFinder::startCapture()
+{
+    std::cout << std::endl;
+    std::cout << "* start capture ... ";
+    try
+    {
+        cameraPtr_ -> startCapture();
+    }
+    catch (RuntimeError &runtimeError)
+    {
+        std::cout << "error: " << runtimeError.what() << std::endl;
+        return false;
+    }
+    std::cout << "done" << std::endl;
+    return true;
+}
+
+
+bool BlobFinder::stopCapture()
+{
+    std::cout << std::endl;
+    std::cout << "* stop capture ... ";
+    try
+    {
+        cameraPtr_ -> stopCapture();
+    }
+    catch (RuntimeError &runtimeError)
+    {
+        std::cout << "error: " << runtimeError.what() << std::endl << std::endl;
+        return false;
+    }
+    std::cout << "done" << std::endl;
+    return true;
+}

src/test/blob_finder.hpp

+#ifndef BLOB_FINDER_HPP
+#define BLOB_FINDER_HPP
+#include "camera_facade_fwd.hpp"
+
+using namespace bias;
+
+class BlobFinder
+{
+    public:
+
+        static const float DEFAULT_FRAMERATE;
+        static const float DEFAULT_DISPLAY_SCALE;
+        static const double DEFAULT_THRESHOLD;
+        static const double DEFAULT_MINIMUM_AREA;
+        static const double DEFAULT_MAXIMUM_AREA;
+        static const double THRESHOLD_MAXVAL;
+
+        BlobFinder();
+        BlobFinder(float frameRate);
+
+        bool setupCamera();
+        bool cleanUp();
+        bool run();
+
+    private:
+
+        float frameRate_;
+        float displayScale_;
+        double threshold_;
+        double minimumArea_; 
+        double maximumArea_;
+        CameraPtr cameraPtr_;
+
+        bool startCapture();
+        bool stopCapture();
+
+};
+
+#endif // #ifndef BLOB_FINDER_HPP
+
+

src/test/blob_finder_main.cpp

+#if defined(WIN32) || defined(WIN64)
+#define _CRT_SECURE_NO_WARNINGS		
+#endif
+
+#include <cstdlib>
+#include <iostream>
+#include "blob_finder.hpp"
+
+using namespace bias;
+
+int main(int argc, char** argv)
+{        
+    std::cout << std::endl;
+    std::cout << "--------------------------------------- " << std::endl;
+    std::cout << "  Blob Finder Demo                      " << std::endl;
+    std::cout << "--------------------------------------- " << std::endl;
+
+    bool success = true;
+    const float frameRate = 50.0;
+    BlobFinder blobFinder(frameRate); 
+
+    success = blobFinder.setupCamera();
+    if (!success)
+    {
+        std::cout << "* failed to setup camera" << std::endl;
+        std::exit(EXIT_FAILURE);
+    }
+
+    success = blobFinder.run();
+    if (!success)
+    {
+        std::cout << "* failure during blob detection" << std::endl;
+    }
+
+    success = blobFinder.cleanUp();
+    if (!success)
+    {
+        std::cout << "* failed to clean up camera" << std::endl;
+        std::exit(EXIT_FAILURE);
+    }
+
+	return 0;
+}
+