Ask Your Question

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

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 close merge delete

## 1 answer

Sort by ยป oldest newest most voted

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

Boundary:-

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

more

## Comments

1

@Haris Moonamkunnu Cool answer!

( 2013-11-20 00:30:09 -0500 )edit
1

@Daniil Osokin Thank you.....

( 2013-11-20 01:01:31 -0500 )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!

( 2013-11-20 03:07:37 -0500 )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?

( 2016-05-25 03:19:12 -0500 )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!

( 2016-05-25 09:02:54 -0500 )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?

( 2016-05-30 04:02:22 -0500 )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?

( 2016-05-30 04:13:03 -0500 )edit

Official site

GitHub

Wiki

Documentation

## Stats

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

Seen: 3,114 times

Last updated: Oct 01 '15