Skip to content

Vison Processing Blob

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Convert Images into the necessary color space
hr := F_VN_GetPixelFormat(ipImageIn, PixelFormat, hr);
IF PixelFormat.nChannels = 3 THEN
    hr := F_VN_CopyImage(ipImageIn, ipImageRes, hr);
    hr := F_VN_ConvertColorSpace(ipImageRes,
                                 ipImageIn,
                                 TCVN_CST_RGB_TO_GRAY,
                                 hr);
ELSE
    hr := F_VN_ConvertColorSpace(ipImageIn,
                                 ipImageRes,
                                 TCVN_CST_Gray_TO_RGB,
                                 hr);
END_IF

// Image Segementation
hr := F_VN_Threshold(ipImageIn,
                     ipImageWork,
                     _BlobParams.fMinThreshold,
                     255,
                     TCVN_TT_Binary,
                     hr);
// Start Watchdog - DetectBlobs - Stop Watchdog
hrWD   := F_VN_StartRelWatchdog(tStop, S_OK);
hrFunc := F_VN_DetectBlobs(ipSrcImage     := ipImageWork,
                           ipBlobContours := ipContourList,
                           stParams       := _BlobParams,
                           hrPrev         := hr);

hrWD := F_VN_StopWatchdog(hrWD, tRest => tRest, nFractionProcessed => nFraction);


//Clear Results and set new results-Number of Blobs
MEMSET(ADR(Results), 0, SIZEOF(Results));
ulint_ref REF= (NumberOfBlobs);
hr        := F_VN_GetNumberOfElements(ipContainer := ipContourList, nNumberOfElements := ulint_ref, hrPrev := hr);
IF NumberOfBlobs > 0 THEN
    FOR i := 0 TO (NumberOfBlobs - 1) BY 1 DO
        hr := F_VN_GetAt_ITcVnContainer(ipSrcContainer  := ipContourList,
                                        ipDestContainer := ipResultContainer,
                                        nIndex          := i,
                                        hrPrev          := hr);

        hr := F_VN_ContourArea(ipContour := ipResultContainer, fArea := Results[i + 1].Area, hrPrev := hr);
        hr := F_VN_ContourCenterOfMass(ipContour := ipResultContainer, aCenterOfMass := Results[i+1].CenterOfMass, hrPrev := hr);

        //More items
        hr := F_VN_ContourCircularity(ipContour := ipResultContainer, fCircularity := Results[i + 1].Circularity, hrPrev := hr);
        hr := F_VN_ContourConvexity(ipContour := ipResultContainer, fConvexity := Results[i + 1].Convexity, hrPrev := hr);
        hr := F_VN_ContourEccentricity(ipContour := ipResultContainer, fEccentricity := Results[i + 1].Eccentricity, hrPrev := hr);
        hr := F_VN_ContourInertiaRatio(ipContour := ipResultContainer, fInertiaRatio := Results[i + 1].InertiaRatio, hrPrev := hr);
        hr := F_VN_ContourOrientation(ipContour := ipResultContainer, stOrientation := Results[i+1].Orientation, hrPrev := hr);
        hr := F_VN_ContourPerimeter(ipContour  := ipResultContainer,
                                    fPerimeter := Results[i + 1].Perimeter,
                                    bClosed    := TRUE,
                                    hrPrev     := hr);

        hr := F_VN_ContourRoundness(ipContour := ipResultContainer, fRoundness := Results[i + 1].Roundness, hrPrev := hr);

    END_FOR
END_IF

(* Draw Result Image
// --------------------------------------------------------------*)
hr := F_VN_DrawContours(ipContourList,
                        -1,
                        ipImageRes,
                        GVL_VisionColor.ColorGreen,
                        3,
                        hr);

IF NumberOfBlobs > 0 THEN
    FOR i := 0 TO (NumberOfBlobs - 1) DO
        hr := F_VN_GetAt_ITcVnContainer(ipSrcContainer  := ipContourList,
                                        ipDestContainer := ipResultContainer,
                                        nIndex          := i,
                                        hrPrev          := hr);

        //hr     := F_VN_UprightBoundingRectangle(ipResultContainer, Rect, hr);
        Text := TO_STRING(i + 1);
        hr   := F_VN_PutText(Text,
                             ipImageRes,
                             TO_UDINT(Results[i + 1].CenterOfMass[0]) - 20,
                             TO_UDINT(Results[i + 1].CenterOfMass[1]) + 20,
                             TCVN_FT_HERSHEY_DUPLEX,
                             2,
                             GVL_VisionColor.ColorMagenta,
                             hr);
    END_FOR
END_IF

// Display Input Image
hr := F_VN_TransformIntoDisplayableImage(ipImageIn, ipImageInDisp, hr);

// Display Result Image
hr := F_VN_TransformIntoDisplayableImage(ipImageRes, ipImageResDisp, hr);

// Display Threshold Image
hr := F_VN_TransformIntoDisplayableImage(ipImageWork, ipImageThresholdDisp, hr);

Blob Parameters

Name Type Default Description
FilterByArea BOOL TRUE Enable filtering by area (fMinArea <= area <= fMaxArea); strongly recommended to activate for filtering noise with fMinArea.
FilterByCircularity BOOL FALSE Enable filtering by circularity (fMinCircularity <= circularity(4pi * area / perimeter^2) <= fMaxCircularity).
FilterByConvexity BOOL FALSE Enable filtering by convexity (fMinConvexity <= convexity(area / hullArea) <= fMaxConvexity).
FilterByEccentricity BOOL FALSE Enable filtering by eccentricity (fMinEccentricity <= eccentricity <= fMaxEccentricity).
FilterByInertiaRatio BOOL FALSE Enable filtering by inertia ratio (fMinInertiaRatio <= inertia ratio <= fMaxInertiaRatio).
MinArea REAL 10 Min estimated blob area in pixel
MaxArea REAL 100000000 Max estimated blob area in pixel
MinCircularity REAL 0 Min circularity (1.0: ideal circle, < 1: less circular, 0: not circular at all)
MaxCircularity REAL 1 Max circularity (1.0: ideal circle, < 1: less circular, 0: not circular at all)
MinConvexity REAL 0 Min convexity (1.0: blob fully convex, < 1: less convex)
MaxConvexity REAL 1 Max convexity (1.0: blob fully convex, < 1: less convex)
MinEccentricity REAL 0 Min eccentricity (0.0: circular, 1.0: linear)
MaxEccentricity REAL 1 Max eccentricity (0.0: circular, 1.0: linear)
MinInertiaRatio REAL 0 Min inertia ratio (1.0: equal width and height, 0.0: linear)
MinInertiaRatio REAL 0 Max inertia ratio (1.0: equal width and height, 0.0: linear)
ThresholdType ETcVnThresholdType TCVN_TT_BINARY Threshold type for internally applied threshold method (OTSU_XXX only supported for USINT images).
MinThreshold REAL 30 Threshold to start with (if fThresholdStep > 0, otherwise this is the only threshold used).
MaxThreshold REAL 225 MinThreshold : REAL := 30
ThresholdStep REAL 0 Sets to 0 if only 1 threshold should be used (much faster than multiple thresholds and combining the results).
MinBlobDistance REAL 5 Minimum distance between the center points of two different blobs (only used if fThresholdStep > 0; if distance < fMinBlobDistance, the blobs are treated as the same).
MinRepeatability UDINT 2 Minimum number of threshold steps, for which the same contour has to be detected (only used if fThresholdStep > 0; same means center point distance < fMinBlobDistance).
BlobCombination ETcVnBlobCombination TCVN_BC_MEDIAN_THRESHOLD Selects, which of the multi-threshold blob contours should be returned.

Results

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
TYPE ST_BlobResults :
    STRUCT
        Area           : LREAL;
        CenterOfMass   : TcVnPoint2_LREAL;
        Circularity    : LREAL;
        Convexity      : LREAL;
        Eccentricity   : LREAL;
        ExtremePoint   : TcVnPoint2_LREAL;
        InertiaRatio   : LREAL;
        Moments        : TcVnMoments;
        Orientation    : TcVnRotatedRectangle;
        OrientationExp : TcVnRotatedRectangle;
        Perimeter      : LREAL;
        Roundness      : LREAL;
    END_STRUCT
END_TYPE