Ask Your Question
1

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

asked 2013-11-13 18:49:54 -0600

Peter_OpenCV gravatar image

updated 2015-10-01 00:55:35 -0600

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
10

answered 2013-11-14 01:44:35 -0600

Haris gravatar image

updated 2013-11-19 23:35:43 -0600

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

edit flag offensive delete link more

Comments

1

@Haris Moonamkunnu Cool answer!

Daniil Osokin gravatar imageDaniil Osokin ( 2013-11-20 00:30:09 -0600 )edit
1

@Daniil Osokin Thank you.....

Haris gravatar imageHaris ( 2013-11-20 01:01:31 -0600 )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 ( 2013-11-20 03:07:37 -0600 )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?

VeeranjaneyuluToka gravatar imageVeeranjaneyuluToka ( 2016-05-25 03:19:12 -0600 )edit
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 ( 2016-05-25 09:02:54 -0600 )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?

VeeranjaneyuluToka gravatar imageVeeranjaneyuluToka ( 2016-05-30 04:02:22 -0600 )edit

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?

VeeranjaneyuluToka gravatar imageVeeranjaneyuluToka ( 2016-05-30 04:13:03 -0600 )edit

Question Tools

Stats

Asked: 2013-11-13 18:49:54 -0600

Seen: 5,021 times

Last updated: Oct 01 '15