www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Regionprops implementation for D

reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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
Mar 11
next sibling parent reply Andrea Fontana <nospam example.org> writes:
On Monday, 11 March 2019 at 09:29:16 UTC, Ferhat Kurtulmuş wrote:
 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.
Nice work Ferhat! Some suggestions: - Add it to code.dlang.org (I can't find it) - Add a buildable example on your library code base. - Publish some pictures on github page to make the result clear :)
Mar 11
next sibling parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Monday, 11 March 2019 at 09:41:59 UTC, Andrea Fontana wrote:
 On Monday, 11 March 2019 at 09:29:16 UTC, Ferhat Kurtulmuş 
 wrote:
 [...]
Nice work Ferhat! Some suggestions: - Add it to code.dlang.org (I can't find it) - Add a buildable example on your library code base. - Publish some pictures on github page to make the result clear :)
I will do it, thank you for appreciation!
Mar 11
prev sibling parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Monday, 11 March 2019 at 09:41:59 UTC, Andrea Fontana wrote:
 On Monday, 11 March 2019 at 09:29:16 UTC, Ferhat Kurtulmuş 
 wrote:
 [...]
Nice work Ferhat! Some suggestions: - Add it to code.dlang.org (I can't find it) - Add a buildable example on your library code base. - Publish some pictures on github page to make the result clear :)
it is now on: https://code.dlang.org/packages/regionpropsford
Mar 11
prev sibling parent reply Dennis <dkorpel gmail.com> writes:
On Monday, 11 March 2019 at 09:29:16 UTC, Ferhat Kurtulmuş wrote:
 I am trying to learn dlang and decided to develop a regionprops 
 library in pure d (https://github.com/aferust/regionpropsford).
What is regionprops? What are the use cases for this library?
Mar 11
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Monday, 11 March 2019 at 10:18:39 UTC, Dennis wrote:
 On Monday, 11 March 2019 at 09:29:16 UTC, Ferhat Kurtulmuş 
 wrote:
 I am trying to learn dlang and decided to develop a 
 regionprops library in pure d 
 (https://github.com/aferust/regionpropsford).
What is regionprops? What are the use cases for this library?
Region Properties, AKA regionprops is a famous routine for calculating region (blob) properties/statistics in image processing/computer vision. It firstly appeared in image processing toolbox of Matlab, then some image processing libraries such as scikit-image (Python) and GNU/Octave made its own implementations using the same name. The process starts with labeling discrete binary regions for 8-connected objects, and calculates properties of these regions like area, centroid, perimeter, bounding box etc.
Mar 11