JavaCV Basics: Splitting Objects

Here we put together functions from previous articles to describe a use case where images are broken into discovered chunks and rotated.

All code is available on GitHub under GoatImage. To fully understand this article, read the related articles and look at this code.

Related Articles:

Why Split Objects

At times, objects need to be tracked reliably, OCR needs to be broken down to more manageable tasks, or there is another task requiring splitting and rotation. Particularly, recognition and other forms of statistical computing benefit from such standardization.

Splitting allows object by object recognition which may or may not improve accuracy. A Bayesian Neural net may be better off with chained images or a convolutional network trained on highly detailed images may not. Ensembles and other methods help alleviate this problem automatically to an extent.

Rotating an object, whether a training object or an actual object, helps set a baseline for all images to be compared against. This increases accuracy in prediction and helps build larger ‘bins’ for certain algorithms.

splitting and Rotating

Used in conjunction with the methods listed above. The following function in GoatImage performs contouring to find objects, finds the minimum area rect, rotates based on the angle,and finally rotates each object. Here the anticipation is image object detection and OCR.

    * Split an image using an existing contouring function. Take each RIO, rotate, and return new Images with the original,
    * @param image              The image to split objects from
    * @param contourType        The contour type to use defaulting to CV_RETR_EXTERNAL
    * @param minBoxArea         Minumum box area to accept (-1 means everything and is default)
    * @param maxBoxArea         Maximum box area to accept (-1 means everything and is default)
    * @param show               Whether or not to show the image. Default is false.
    * @param xPosSort           Whether or not to sort the objects by their x position. Default is true. This is faster than a full sort
    * @return                   A tuple with the original Image and a List of split out Image objects named by the original_itemNumber
  def splitObjects(image : Image, contourType : Int=  CV_RETR_LIST,minBoxArea : Int = -1, maxBoxArea : Int = -1, show : Boolean= false,xPosSort : Boolean = true):(Image,List[(Image,BoundingBox)])={
    val imTup : (Image, List[BoundingBox]) = this.contour(image,contourType)

    var imObjs : List[(Image,BoundingBox)] = List[(Image,BoundingBox)]()

    var boxes : List[BoundingBox] = imTup._2

    //ensure that the boxes are sorted by x position
      boxes = boxes.sortBy(_.x1)

    if(minBoxArea > 0){
        boxes = boxes.filter({x => (x.width * x.height) > minBoxArea})

    if(maxBoxArea > 0){
      boxes = boxes.filter({x => (x.width * x.height) < maxBoxArea})

    //get and rotate objects
    var idx : Int = 0
    for(box <-  boxes){
      val im = this.rotateImage(box.image,box.skewAngle)
        im.showImage(s"My Box ${idx}")
      imObjs = imObjs :+ (im,box)
      idx += 1


Contours are filtered after a sort as specified by boolean switches. For each box, rotation is performed and the resulting image returned as a new Image.


Here the splitObjects function of GoatImage is reviewed, revealing how the library splits and rotates objects as part of standardization for object recognition and OCR.


6 thoughts on “JavaCV Basics: Splitting Objects

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s