mrDarker
2025-08-14 aa2c9d14b1b4058639d938ef7f943c7c7fc7c210
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
100
101
102
103
104
105
106
#include "stdafx.h"
#include "BaseProcApi.h"
 
//¶ÔÏóÊÇ·ñ´æÔÚ
bool ObjectIsExist(HalconCpp::HObject& hObject) {
    /* code */
    // ÅжÏhObjectÊÇ·ñδ³õʼ»¯
    if (!hObject.IsInitialized()) return false;
 
    // ´´½¨Ò»¸ö¿ÕµÄHObject
    HalconCpp::HObject hEmptyObj;
    HalconCpp::GenEmptyObj(&hEmptyObj);
    // ÅжÏhObjectÊÇ·ñµÈÓÚhEmptyObj
    if (hObject == hEmptyObj) return false;
 
    // ·ñÔò·µ»Øtrue
    return true;
}
 
bool GetImageSizeApi(HalconCpp::HObject hImage, int& width, int& height) {
    /*c ode */
    if (!ObjectIsExist(hImage)) return false;
 
    HalconCpp::HTuple hWidth, hHeight;
    HalconCpp::GetImageSize(hImage, &hWidth, &hHeight);
    width = hWidth[0].I();
    height = hHeight[0].I();
    if (width < 1 || height < 1) return false;
    return true;
}
 
int doMeasurePosApi(HalconCpp::HObject& hImage, double x1, double y1, double x2, double y2, double len, int threshold, double sigma, double* xOutResult, double* yOutResult, double* cyOutResult, double* distOutResult, int& nOutResult) {
    /* code */
    int width = 0; 
    int height = 0;
    nOutResult = 0;
    if (!GetImageSizeApi(hImage, width, height)) return -1;
    if (width < 1 || height < 1) return -1;
    
    // Local control variables
    HalconCpp::HTuple  hv_AmplitudeThreshold, hv_RoiWidthLen2;
    HalconCpp::HTuple  hv_LineRowStart_Measure_01_0, hv_LineColumnStart_Measure_01_0;
    HalconCpp::HTuple  hv_LineRowEnd_Measure_01_0, hv_LineColumnEnd_Measure_01_0;
    HalconCpp::HTuple  hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Dr;
    HalconCpp::HTuple  hv_TmpCtrl_Dc, hv_TmpCtrl_Phi, hv_TmpCtrl_Len1;
    HalconCpp::HTuple  hv_TmpCtrl_Len2, hv_MsrHandle_Measure_01_0, hv_Row_Measure_01_0;
    HalconCpp::HTuple  hv_Column_Measure_01_0, hv_Amplitude_Measure_01_0;
    HalconCpp::HTuple  hv_Distance_Measure_01_0;
 
    hv_AmplitudeThreshold = threshold;
    hv_RoiWidthLen2 = len;
 
    //Measure 01: Coordinates for line Measure 01 [0]
    hv_LineRowStart_Measure_01_0 = y1;
    hv_LineColumnStart_Measure_01_0 = x1;
    hv_LineRowEnd_Measure_01_0 = y2;
    hv_LineColumnEnd_Measure_01_0 = x2;
 
    //Measure 01: Convert coordinates to rectangle2 type
    hv_TmpCtrl_Row = 0.5 * (hv_LineRowStart_Measure_01_0 + hv_LineRowEnd_Measure_01_0);
    hv_TmpCtrl_Column = 0.5 * (hv_LineColumnStart_Measure_01_0 + hv_LineColumnEnd_Measure_01_0);
    hv_TmpCtrl_Dr = hv_LineRowStart_Measure_01_0 - hv_LineRowEnd_Measure_01_0;
    hv_TmpCtrl_Dc = hv_LineColumnEnd_Measure_01_0 - hv_LineColumnStart_Measure_01_0;
    hv_TmpCtrl_Phi = hv_TmpCtrl_Dr.TupleAtan2(hv_TmpCtrl_Dc);
    hv_TmpCtrl_Len1 = 0.5 * (((hv_TmpCtrl_Dr * hv_TmpCtrl_Dr) + (hv_TmpCtrl_Dc * hv_TmpCtrl_Dc)).TupleSqrt());
    hv_TmpCtrl_Len2 = hv_RoiWidthLen2;
    double len1 = hv_TmpCtrl_Len1[0].D();
    double len2 = hv_TmpCtrl_Len2[0].D();
    printf("len1 = %f, len2 = %f\n", len1, len2);
 
    //Measure 01: Create measure for line Measure 01 [0]
    //Measure 01: Attention: This assumes all images have the same size!
    HalconCpp::GenMeasureRectangle2(hv_TmpCtrl_Row, hv_TmpCtrl_Column, hv_TmpCtrl_Phi, hv_TmpCtrl_Len1,
        hv_TmpCtrl_Len2, width, height, "nearest_neighbor", &hv_MsrHandle_Measure_01_0);
    //Measure 01: ***************************************************************
    //Measure 01: * The code which follows is to be executed once / measurement *
    //Measure 01: ***************************************************************
    //Measure 01: The image is assumed to be made available in the
    //Measure 01: variable last displayed in the graphics window
    //Measure 01: Execute measurements
    HalconCpp::MeasurePos(hImage, hv_MsrHandle_Measure_01_0, sigma, hv_AmplitudeThreshold, "all",
        "all", &hv_Row_Measure_01_0, &hv_Column_Measure_01_0, &hv_Amplitude_Measure_01_0,
        &hv_Distance_Measure_01_0);
    //Measure 01: Do something with the results
    HalconCpp::CloseMeasure(hv_MsrHandle_Measure_01_0);
 
    HalconCpp::HTuple hv_length = hv_Row_Measure_01_0.TupleLength();
    int count = hv_length[0].I();
    if (count > MEASURE_MAX_NUM) {
        count = MEASURE_MAX_NUM;
    }
 
    for (int i = 0; i < count; i++) {
        xOutResult[i] = hv_Column_Measure_01_0[i].D();
        yOutResult[i] = hv_Row_Measure_01_0[i].D();
        cyOutResult[i] = hv_Amplitude_Measure_01_0[i].D();
    }
    int num = count - 1;
    for (int i = 0; i < num; i++) {
        distOutResult[i] = hv_Distance_Measure_01_0[i].D();
    }
 
    nOutResult = count;
 
    return 1;
}