From c7fa115a6fd11c6a9a0cf0bd81314f6dda8220a0 Mon Sep 17 00:00:00 2001 From: gdd Date: Wed, 23 Nov 2011 14:51:23 +0000 Subject: [PATCH] rnc: added a method in FeatureDetector to cropp an image --- .../ShapeRec_FeatureDetector.cxx | 50 ++++++++++++++++--- .../ShapeRec_FeatureDetector.hxx | 12 +++-- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/ShapeRecognition/ShapeRec_FeatureDetector.cxx b/src/ShapeRecognition/ShapeRec_FeatureDetector.cxx index 532cba0c3..b9a341e60 100644 --- a/src/ShapeRecognition/ShapeRec_FeatureDetector.cxx +++ b/src/ShapeRecognition/ShapeRec_FeatureDetector.cxx @@ -38,16 +38,30 @@ using namespace cv; Constructor \param theFilename - image to process */ -ShapeRec_FeatureDetector::ShapeRec_FeatureDetector(const std::string& theFilename): +ShapeRec_FeatureDetector::ShapeRec_FeatureDetector(): corners() { cornerCount = 2000; rect=cvRect(0,0,0,0); - imagePath = theFilename; + imagePath = ""; //theFilename; // Store the dimensions of the picture - IplImage* bg_img = cvLoadImage (imagePath.c_str(), CV_LOAD_IMAGE_GRAYSCALE); - imgHeight = bg_img->height; - imgWidth = bg_img->width; + imgHeight = 0; + imgWidth = 0; +} + +/*! + Sets the path of the image file to be proccesed + \param thePath - Location of the image file +*/ +void ShapeRec_FeatureDetector::SetPath( const std::string& thePath ) +{ + imagePath = thePath; + if (imagePath != "") + { + IplImage* src = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR); + imgHeight = src->height; + imgWidth = src->width; + } } /*! @@ -167,6 +181,28 @@ void ShapeRec_FeatureDetector::SetROI( const QRect& theRect ) } } +/*! + Crops the image located at imagePath to the region of interest given by the user via SetROI + and stores the result in /tmp + \param theRect - Region Of Interest of the image located at imagePath +*/ +std::string ShapeRec_FeatureDetector::CroppImage() +{ + IplImage* src = cvLoadImage(imagePath.c_str(),CV_LOAD_IMAGE_COLOR); + + cvSetImageROI(src, rect); + IplImage* cropped_image = cvCreateImage(cvGetSize(src), + src->depth, + src->nChannels); + cvCopy(src, cropped_image, NULL); + cvResetImageROI(src); + + cvSaveImage ("/tmp/cropped_image.bmp", cropped_image); + + return "/tmp/cropped_image.bmp"; +} + + /*! Performs contours detection and store them in contours \param src - src image to find contours of @@ -198,8 +234,8 @@ Mat ShapeRec_FeatureDetector::_colorFiltering() // Crop the image to build an histogram from the selected part cvSetImageROI(find_image, rect); IplImage* test_image = cvCreateImage(cvGetSize(find_image), - find_image->depth, - find_image->nChannels); + find_image->depth, + find_image->nChannels); cvCopy(find_image, test_image, NULL); cvResetImageROI(find_image); diff --git a/src/ShapeRecognition/ShapeRec_FeatureDetector.hxx b/src/ShapeRecognition/ShapeRec_FeatureDetector.hxx index a56afcbe4..a3ebeebb2 100644 --- a/src/ShapeRecognition/ShapeRec_FeatureDetector.hxx +++ b/src/ShapeRecognition/ShapeRec_FeatureDetector.hxx @@ -45,12 +45,9 @@ public: typedef std::vector CvContour; typedef std::vector > CvContoursArray; - ShapeRec_FeatureDetector( const std::string& ); // Constructor - - void ComputeCorners(); // Detects the corners from the image located at imagePath - bool ComputeLines(); // Detects the lines from the image located at imagePath - bool ComputeContours( int method ); // Detects the contours from the image located at imagePath + ShapeRec_FeatureDetector(); // Constructor + void SetPath( const std::string& ); // Sets the image path void SetROI( const QRect& ); // Sets a Region Of Interest in the image CvPoint2D32f* GetCorners() { return corners; }; CvContoursArray GetContours() { return contours; }; @@ -60,6 +57,11 @@ public: int GetImgHeight() { return imgHeight; }; int GetImgWidth() { return imgWidth; }; + std::string CroppImage(); + void ComputeCorners(); // Detects the corners from the image located at imagePath + bool ComputeLines(); // Detects the lines from the image located at imagePath + bool ComputeContours( int method ); // Detects the contours from the image located at imagePath + private: std::string imagePath; -- 2.39.2