First time here? Check out the FAQ!

Ask Your Question
1

Boundary detection-What function will be the best for this?

asked Nov 14 '13

Peter_OpenCV gravatar image

updated Oct 1 '15

Haris gravatar image

image descriptionC:\fakepath\goal.jpg

Hi guys, My goal is to draw a line on the boundary between the grass and concrete as in the image. However, I am having difficulty cuz the grass is too jagged. What function should I use in this case?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
10

answered Nov 14 '13

Haris gravatar image

updated Nov 20 '13

You can follow the below step to find the minimum and maximum boundary.

  1. Load image.
  2. Blur to remove noise.
  3. Convert to HSV colour space for segment the grass.
  4. Use OpenCV inRange() to filter green colour.
  5. Morphological operation to fill-out the black region. Here you will get binary image with boundary of 0 value.

  6. Scan each column until black pixel to find minimum and maximum boundary.

Here is the code

#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
  Mat edge,dst,threshold;
  Mat HSV;
  int dilation_size =4;

  Mat src = imread( "src.jpg",1);
  blur( src, src, Size(2,2) );
  cvtColor(src,HSV,CV_BGR2HSV);
  inRange(HSV,Scalar(44,90,30),Scalar(76,255,255),threshold);
  Mat element = getStructuringElement( MORPH_RECT,Size( 2*dilation_size + 1, 2*dilation_size+1 ), Point( dilation_size, dilation_size ) );
  morphologyEx( threshold, dst, MORPH_CLOSE, element );


  int minBound=dst.rows;
  int maxBound=0;
  int tmp=0;

  for(int i = 0; i < dst.cols; i++){
     for(int j = 0; j < dst.rows; j++){
        if(dst.at<uchar>(j, i)==0){
        tmp=j;
        break;
        }
     }
    if(tmp<minBound) minBound=tmp;
    if(tmp>maxBound) maxBound=tmp;
   }

  line(dst, Point(0,minBound), Point(dst.cols,minBound),  Scalar(0), 1,8, 0); //minmum boudary
  line(dst, Point(0,maxBound), Point(dst.cols,maxBound),  Scalar(255), 1,8, 0); // maximum boundary
  line(src, Point(0,minBound), Point(dst.cols,minBound),  Scalar(0,0,255), 1,8, 0); //minmum boudary
  line(src, Point(0,maxBound), Point(dst.cols,maxBound),  Scalar(0,0,255), 1,8, 0); // maximum boundary

  imshow( "source", src );
  imshow( "threshold", threshold );
  imshow( "dst", dst );
  waitKey(0);
  return 0;
}

Result :-

Threshold Image:-

image description

Boundary:-

image description

You can chose any value between minimum and maximum boundary, like the row which have maximum transition from 255 to 0, and that may be depend on your source image.

Hope these helpful....

Preview: (hide)

Comments

1

@Haris Moonamkunnu Cool answer!

Daniil Osokin gravatar imageDaniil Osokin (Nov 20 '13)edit
1

@Daniil Osokin Thank you.....

Haris gravatar imageHaris (Nov 20 '13)edit
2

I would like to see more people make such a clear answers on this forum, it would definately improve the quality! +1 from my side!

StevenPuttemans gravatar imageStevenPuttemans (Nov 20 '13)edit

looks like this piece of code works only for detecting boundaries of green objects... how about detecting boundaries of objects which has red, blue and all together?

1

@VeeranjaneyuluToka I guess you will need to read the code, look for a line like this inRange(HSV,Scalar(44,90,30),Scalar(76,255,255),threshold); and define new ranges for your color range that you want!

StevenPuttemans gravatar imageStevenPuttemans (May 25 '16)edit

@StevenPuttemans Agree with you, had a look into the code and experimented with a few set of values, could not get quite good result. Let me tell you my requirement, basically trying to find out a boundary of an item (for example any food item) which could be mix up of multiple colours, in that case will this method helpful to me or any different approach?

one more thing, am just wondering if there is a way to find out the range automatically as it vary from image to image and feed into this api, any idea how to get that range if it is possible?

Question Tools

Stats

Asked: Nov 14 '13

Seen: 5,206 times

Last updated: Oct 01 '15