Regionprops implementation for D

Ferhat Kurtulmuş aferust at gmail.com
Mon Mar 11 09:29:16 UTC 2019


I am trying to learn dlang and decided to develop a regionprops 
library in pure d (https://github.com/aferust/regionpropsford). I 
borrowed some code and translated into d for performing routines 
like connected component labeling and convexhull. The library 
does not have any dependency except dlang standard library 
phobos. In theory, the library can be used with any image 
processing library allowing access to raw image data pointer. I 
have recently found out that dcv is dead. So, the library uses 
its own data type (Mat2D!T). Although it has been tested with 
dlib, you can test it with other libraries such as dcv. I admit 
that many code were written clumsily, however my test showed that 
it is fast and safe enough so far.

Example usage with dlib:

   import std.stdio;
   import std.format;
   import dlib.image;

   import measure.regionprops;
   import measure.types;


   void main(){
       auto img = loadImage("test.png");
       auto _imgbin = otsuBinarization(img);

       auto imgbin = Mat2D!ubyte(_imgbin.data, _imgbin.height, 
_imgbin.width);

       // input binary pixels must be 0 for background and 255 for 
regions
       /*
       try RegionProps(imgbin, false) if your OS complains about 
max
       stack size limit. In this way labeling will be done using
       a non-recursive method.
       */
       auto rp = new RegionProps(imgbin);
       rp.calculateProps();
       /+
       now you can access blob properties like:
       rp.regions[0].orientation
       rp.regions[0].majorAxisLength
       rp.regions[3].area
       +/
       auto res = new Image!(PixelFormat.L8)(col_count, row_count);
       res.data[] = imgbin.data[];

       foreach(i, region; rp.regions){ // mark the centroids
           res[region.centroid.x, region.centroid.y] = Color4f(0, 
0, 0, 255);
       }
       saveImage(res, "result.png");
   }

I also provide a python binnding based on cython:

usage with opencv:

import numpy as np
import cv2
import rprops

imrgb = cv2.imread('test.png');
img_gray = cv2.cvtColor(imrgb, cv2.COLOR_BGR2GRAY)

binary = img_gray > 200 # do your thresholding somehow

# returns a list containing dicts:
regions = rprops.regionpropsford(binary.astype(np.uint8))

print(regions[0]["Perimeter"])

avalable props (keys of dictionary obj representing a region):

Perimeter
AreaFromContour
Orientation
ConvexHull
Area
Moments
MinorAxis
Solidity
ConvexArea
Eccentricity
ContourPixelList
Centroid
MajorAxis
PixelList
BoundingBox
AspectRatio
EquivalentDiameter
Ellipse


More information about the Digitalmars-d-announce mailing list