Ask Your Question
1

Problem in thresholding of a part of image

asked 2016-01-22 21:29:45 -0600

Dr Dre gravatar image

updated 2016-01-22 21:30:56 -0600

Hi guys

I have the following image

image description

I want to extract the following part circled red in the image as shown

image description

The problem is both the background and the box to extract are white..

How can i perform thresholding such that i get only the box as output.

I am using python 2.7 with cv2

edit retag flag offensive close merge delete

Comments

@matman could you post your comment as an answer. i will add a sample code in your answer. i was just trying exactly the same method you explained.

sturkmen gravatar imagesturkmen ( 2016-01-23 05:42:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-01-23 05:47:45 -0600

matman gravatar image

updated 2016-01-23 05:57:54 -0600

I would do a simple threshold on the image. Then use cv::findContours() an choose the correct contours with cv::contourArea(). After that you can cut out the region with creating a rectangle around it (cv::boundingRect) and copy the specific part to a new cv::Mat.

Perhabs you have to dilate the image before, if the small bright regions are not seperated from the large ones by cv::findContours(). Also you can make the size of the bounding rectangle a bit larger if you want.

sample c++ code below can easily be converted to Python

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

using namespace cv;
using namespace std;

int main( int, char** argv )
{
  /// Load source image
  Mat src = imread(argv[1]);
  if (src.empty())
  {
    cerr << "No image supplied ..." << endl;
    return -1;
  }

  /// Convert image to gray
  Mat src_gray;
  cvtColor( src, src_gray, COLOR_BGR2GRAY );
  threshold( src_gray, src_gray, 250, 255, THRESH_BINARY );
  //erode( src_gray, src_gray, Mat());

  /// Find contours
  vector<vector<Point> > contours;
  findContours( src_gray, contours, RETR_TREE, CHAIN_APPROX_SIMPLE );

  /// Draw contours
  for( size_t i = 0; i< contours.size(); i++ )
     {
       Scalar color = Scalar( 0, 0, 255 );
       Rect _boundingRect = boundingRect( contours[i] );
       if( _boundingRect.height < 50 )
       drawContours( src, contours, (int)i, color, 2 );
     }

  /// Show in a window
  imshow( "Contours", src );
  waitKey(0);
  return(0);
}

after threshold you will get this binarized image

image description

finally you will get your desired parts by contour analyzing

image description

edit flag offensive delete link more

Comments

I have written a python equivalent code for the above

import numpy as np
import cv2


img = cv2.imread('New Bitmap Image.bmp')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
lt = len(contours)
for i in range (0,lt):
    x,y,w,h = cv2.boundingRect(i)
    if (h < 50):
        cv2.drawContours(img,contours,i,(0,255,0),3)
cv2.imshow('output',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

I get the following error

Dr Dre gravatar imageDr Dre ( 2016-01-23 22:28:27 -0600 )edit

ERROR is

Traceback (most recent call last):
  File "C:/Desktop/test.py", line 12, in <module>
    x,y,w,h = cv2.boundingRect(i)
error: ..\..\..\..\opencv\modules\imgproc\src\contours.cpp:1895: error: (-215) points.checkVector(2) >= 0 && (points.depth() == CV_32F || points.depth() == CV_32S) in function cv::boundingRect

>>>
Dr Dre gravatar imageDr Dre ( 2016-01-23 22:30:06 -0600 )edit
1

i don't know Python but maybe x,y,w,h = cv2.boundingRect(contours[i])

sturkmen gravatar imagesturkmen ( 2016-01-24 03:56:54 -0600 )edit

Damn me ,, UR right sir that solved the problem Thank U

Dr Dre gravatar imageDr Dre ( 2016-01-24 04:20:37 -0600 )edit

Thank U matman and sturk

Dr Dre gravatar imageDr Dre ( 2016-01-24 04:21:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-22 21:29:45 -0600

Seen: 2,308 times

Last updated: Jan 23 '16