I will assume that your image can be treated as a binary mask and that we are only interested in pixels with non-zero values.

To find the bounding box around the points in the left image above, iterate over all pixels in the image. For every non-zero pixel, check if its xy
-location lies outside the current bounding box. If it does, then update your bounding box. The resulting bounding box will enclose all points, as in the right image above.
Below is a minimally working example, which generates an image containing randomly sampling points for which a bounding box is determined.
// This example uses OpenCV 3.0.0-beta. To use with OpenCV 2.4.* a
// few changes have to be made.
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
cv::Mat randomPoints( cv::Size size = cv::Size( 320, 240 ), int pointCount = 25 )
{
cv::RNG rng( cv::getCPUTickCount() );
cv::Mat image = cv::Mat3b::zeros( size );
int radius = 3;
cv::Scalar color( 0, 153, 255 );
int thickness = -1;
int margin = std::min( size.height, size.width ) / 4;
for ( int i = 0; i < pointCount; ++i )
{
cv::Point p;
p.x = rng.uniform( margin, size.width - margin - 1 );
p.y = rng.uniform( margin, size.height - margin - 1 );
cv::circle( image, p, radius, color, thickness );
}
return image;
}
int main( int argc, char ** argv )
{
#if 0
cv::Mat image = imread( "C:/Users/Ravi Sharma/Desktop/img.bmp" );
#else
cv::Mat image = randomPoints();
#endif
cv::Mat imageGray;
cv::cvtColor( image, imageGray, cv::COLOR_BGR2GRAY );
cv::Size size = imageGray.size();
// The bounding box is defined by its top-left (TL) and bottom-right (BR)
// coordinates.
cv::Point tl( size.width, size.height );
cv::Point br( 0, 0 );
bool hasPoints = false;
for ( int y = 0; y < size.height; ++y )
{
for ( int x = 0; x < size.width; ++x )
{
if ( imageGray.at<unsigned char>( y, x ) > 0 )
{
hasPoints = true;
// Update the top-left corner.
if ( x < tl.x ) tl.x = x;
if ( y < tl.y ) tl.y = y;
// Update the bottom-right corner.
if ( x > br.x ) br.x = x;
if ( y > br.y ) br.y = y;
}
}
}
// If the image contains any non-zero pixels, then draw the bounding box.
if ( hasPoints )
cv::rectangle( image, tl, br, cv::Scalar( 255, 255, 255, 255 ) );
cv::namedWindow( "bounding-box" );
cv::imshow( "bounding-box", image );
cv::waitKey( 0 );
cv::imwrite( "bounding-box.png", image );
}
EDIT 1:
I also like the idea suggested by @Micka above, i.e. using cv::boundingRect(). So, inside the loop in the above code example you would push all xy
-locations of non-zeros pixels into a std::vector< cv::Point >
and then call cv::boundingRect
. In that context it is also interesting to look at the convex hull of a 2D point cloud.