Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Fill Color on ConvexHull

Everyone, please help me. I have a problem. I make a Convex Hull detection from this image: Image Source: image description

This is a result of Convex Hull detection:

image description

Then, i have a problem to fill a color on area within the boundary of ConvexHull, as shown in the image below:

image description

This is a source code:

include "stdafx.h" #include "opencv2/highgui/highgui.hpp"

include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> #include <stdlib.h>

using namespace cv; using namespace std;

Mat src; Mat src_gray; int thresh = 240; int max_thresh = 255; RNG rng(12345);

/// Function header void thresh_callback(int, void* );

/* @function main */ int main( int argc, char* argv ) { /// Load source image and convert it to gray
src = imread( "kubis1.jpg", 1 );

/// Convert image to gray and blur it cvtColor( src, src_gray, CV_BGR2GRAY ); blur( src_gray, src_gray, Size(3,3) );

/// Create Window char* source_window = "Source";
namedWindow( source_window, CV_WINDOW_AUTOSIZE ); imshow( source_window, src );

createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback ); thresh_callback( 0, 0 );

waitKey(0); return(0); }

/* @function thresh_callback */ void thresh_callback(int, void ) {
Mat src_copy = src.clone(); Mat threshold_output;
vector<vector<point> > contours;
vector<vec4i> hierarchy;

/// Detect edges using Threshold
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );

/// Find contours findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

/// Find the convex hull object for each contour vector<vector<point>

hull( contours.size() ); for( int i = 0; i < contours.size(); i++ ) { convexHull( Mat(contours[i]), hull[i], false ); }

/// Draw contours + hull results
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ ) { Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); drawContours( drawing, contours, i, color, 1, 8, vector<vec4i>(), 0, Point() ); drawContours( drawing, hull, i, color, 1, 8, vector<vec4i>(), 0, Point() ); }

/// Show in a window
namedWindow( "Hull demo", CV_WINDOW_AUTOSIZE ); imshow( "Hull demo", drawing ); }

I use OpenCV 2.3. Is there someone who can help me?

Fill Color on ConvexHull

Everyone, please help me. I have a problem. I make a Convex Hull detection from this image: Image Source:

image description

This is a result of Convex Hull detection:

image description

Then, i have a problem to fill a color on area within the boundary of ConvexHull, as shown in the image below:

image description

This is a source code:

include
 #include "stdafx.h"    #include "opencv2/highgui/highgui.hpp"

include "opencv2/imgproc/imgproc.hpp" "opencv2/highgui/highgui.hpp" #include <iostream> "opencv2/imgproc/imgproc.hpp" #include <stdio.h> <iostream> #include <stdlib.h>

<stdio.h> #include <stdlib.h> using namespace cv; cv; using namespace std;

namespace std; Mat src; Mat src_gray; src_gray; int thresh = 240; = 240; int max_thresh = 255; RNG rng(12345);

255; RNG rng(12345); /// Function header void header void thresh_callback(int, void* );

/* ); /** @function main */ */ int main( int int argc, char* char** argv ) { /// Load ) { /// Load source image and convert it to gray
gray src = imread( "kubis1.jpg", 1 );

); /// Convert image to gray and blur it blur it cvtColor( src, src_gray, src_gray, CV_BGR2GRAY ); ); blur( src_gray, src_gray, src_gray, Size(3,3) );

); /// Create Window char* Window char* source_window = "Source";
"Source"; namedWindow( source_window, source_window, CV_WINDOW_AUTOSIZE ); imshow( ); imshow( source_window, src );

); createTrackbar( " Threshold:", Threshold:", "Source", &thresh, max_thresh, max_thresh, thresh_callback ); thresh_callback( ); thresh_callback( 0, 0 );

waitKey(0); return(0); }

/* ); waitKey(0); return(0); } /** @function thresh_callback */ */ void thresh_callback(int, void ) {
void* ) { Mat src_copy = src.clone(); Mat threshold_output;
vector<vector<point>
src.clone(); Mat threshold_output; vector<vector<Point> > contours;
vector<vec4i> hierarchy;

contours; vector<Vec4i> hierarchy; /// Detect edges using Threshold
Threshold threshold( src_gray, threshold_output, threshold_output, thresh, 255, THRESH_BINARY );

); /// Find contours findContours( contours findContours( threshold_output, contours, hierarchy, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

); /// Find the convex hull object for for each contour vector<vector<point>

hull( contour vector<vector<Point> >hull( contours.size() ); ); for( int i = 0; i < contours.size(); i++ ) { convexHull( Mat(contours[i]), hull[i], false ); }

); } /// Draw contours + hull results
results Mat drawing = Mat::zeros( Mat::zeros( threshold_output.size(), CV_8UC3 );
); for( int i = 0; i< contours.size(); contours.size(); i++ ) { Scalar color = Scalar( rng.uniform(0, 255), 255), rng.uniform(0,255), rng.uniform(0,255) ); rng.uniform(0,255) ); drawContours( drawing, contours, i, color, 1, 8, vector<vec4i>(), 8, vector<Vec4i>(), 0, Point() ); drawContours( drawing, hull, i, color, 1, 8, vector<vec4i>(), 0, vector<Vec4i>(), 0, Point() ); }

} /// Show in a window
window namedWindow( "Hull demo", demo", CV_WINDOW_AUTOSIZE ); ); imshow( "Hull "Hull demo", drawing ); }

); //Fill Color drawing = cv::Scalar(255,255,0); namedWindow( "Warna", CV_WINDOW_AUTOSIZE ); imshow( "Warna", drawing ); }

I use OpenCV 2.3. 2.3.

Is there someone who can help me?

Fill Color on ConvexHull

Everyone, please help me. I have a problem. I make a Convex Hull detection from this image:

image description

This is a result of Convex Hull detection:

image description

Then, i have a problem I want to fill a color on area within the boundary of ConvexHull, as shown in the image below:

image description

Is there someone who can help me to make image like above?

I use OpenCV 2.3.

This is a source code:

 #include "stdafx.h" 
 #include "opencv2/highgui/highgui.hpp"
 #include "opencv2/imgproc/imgproc.hpp"
 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>

 using namespace cv;
 using namespace std;

 Mat src; Mat src_gray;
 int thresh = 240;
 int max_thresh = 255;
 RNG rng(12345);

 /// Function header
 void thresh_callback(int, void* );

/** @function main */
int main( int argc, char** argv )
 {
   /// Load source image and convert it to gray
   src = imread( "kubis1.jpg", 1 );

   /// Convert image to gray and blur it
   cvtColor( src, src_gray, CV_BGR2GRAY );
   blur( src_gray, src_gray, Size(3,3) );

   /// Create Window
   char* source_window = "Source";
   namedWindow( source_window, CV_WINDOW_AUTOSIZE );
   imshow( source_window, src );

   createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
   thresh_callback( 0, 0 );

   waitKey(0);
   return(0);
 }

 /** @function thresh_callback */
 void thresh_callback(int, void* )
 {
   Mat src_copy = src.clone();
   Mat threshold_output;
   vector<vector<Point> > contours;
   vector<Vec4i> hierarchy;

   /// Detect edges using Threshold
   threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );

   /// Find contours
   findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

   /// Find the convex hull object for each contour
   vector<vector<Point> >hull( contours.size() );
   for( int i = 0; i < contours.size(); i++ )
      {  convexHull( Mat(contours[i]), hull[i], false );
   }

   /// Draw contours + hull results
   Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
   for( int i = 0; i< contours.size(); i++ )
      {
        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
        drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
        drawContours( drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
      }

   /// Show in a window
   namedWindow( "Hull demo", CV_WINDOW_AUTOSIZE );
   imshow( "Hull demo", drawing );

   //Fill Color
   drawing = cv::Scalar(255,255,0);
   namedWindow( "Warna", CV_WINDOW_AUTOSIZE );
   imshow( "Warna", drawing );
 }

I use OpenCV 2.3.

Is there someone who can help me?