Skip to content

find_ncc_modelFindNccModelFindNccModelfind_ncc_modelT_find_ncc_modelπŸ”—

Short descriptionπŸ”—

find_ncc_modelFindNccModelFindNccModelfind_ncc_modelT_find_ncc_model β€” Find the best matches of an NCC model in an image.

SignatureπŸ”—

find_ncc_model( image Image, ncc_model ModelID, angle.rad AngleStart, angle.rad AngleExtent, real MinScore, integer NumMatches, real MaxOverlap, string SubPixel, integer NumLevels, out point.y Row, out point.x Column, out angle.rad Angle, out real Score )void FindNccModel( const HObject& Image, const HTuple& ModelID, const HTuple& AngleStart, const HTuple& AngleExtent, const HTuple& MinScore, const HTuple& NumMatches, const HTuple& MaxOverlap, const HTuple& SubPixel, const HTuple& NumLevels, HTuple* Row, HTuple* Column, HTuple* Angle, HTuple* Score )static void HOperatorSet.FindNccModel( HObject image, HTuple modelID, HTuple angleStart, HTuple angleExtent, HTuple minScore, HTuple numMatches, HTuple maxOverlap, HTuple subPixel, HTuple numLevels, out HTuple row, out HTuple column, out HTuple angle, out HTuple score )def find_ncc_model( image: HObject, model_id: HHandle, angle_start: float, angle_extent: float, min_score: float, num_matches: int, max_overlap: float, sub_pixel: str, num_levels: MaybeSequence[int] ) -> Tuple[Sequence[float], Sequence[float], Sequence[float], Sequence[float]]

Herror T_find_ncc_model( const Hobject Image, const Htuple ModelID, const Htuple AngleStart, const Htuple AngleExtent, const Htuple MinScore, const Htuple NumMatches, const Htuple MaxOverlap, const Htuple SubPixel, const Htuple NumLevels, Htuple* Row, Htuple* Column, Htuple* Angle, Htuple* Score )

void HNCCModel::FindNccModel( const HImage& Image, double AngleStart, double AngleExtent, double MinScore, Hlong NumMatches, double MaxOverlap, const HString& SubPixel, const HTuple& NumLevels, HTuple* Row, HTuple* Column, HTuple* Angle, HTuple* Score ) const

void HNCCModel::FindNccModel( const HImage& Image, double AngleStart, double AngleExtent, double MinScore, Hlong NumMatches, double MaxOverlap, const HString& SubPixel, Hlong NumLevels, HTuple* Row, HTuple* Column, HTuple* Angle, HTuple* Score ) const

void HNCCModel::FindNccModel( const HImage& Image, double AngleStart, double AngleExtent, double MinScore, Hlong NumMatches, double MaxOverlap, const char* SubPixel, Hlong NumLevels, HTuple* Row, HTuple* Column, HTuple* Angle, HTuple* Score ) const

void HNCCModel::FindNccModel( const HImage& Image, double AngleStart, double AngleExtent, double MinScore, Hlong NumMatches, double MaxOverlap, const wchar_t* SubPixel, Hlong NumLevels, HTuple* Row, HTuple* Column, HTuple* Angle, HTuple* Score ) const (Windows only)

void HImage::FindNccModel( const HNCCModel& ModelID, double AngleStart, double AngleExtent, double MinScore, Hlong NumMatches, double MaxOverlap, const HString& SubPixel, const HTuple& NumLevels, HTuple* Row, HTuple* Column, HTuple* Angle, HTuple* Score ) const

void HImage::FindNccModel( const HNCCModel& ModelID, double AngleStart, double AngleExtent, double MinScore, Hlong NumMatches, double MaxOverlap, const HString& SubPixel, Hlong NumLevels, HTuple* Row, HTuple* Column, HTuple* Angle, HTuple* Score ) const

void HImage::FindNccModel( const HNCCModel& ModelID, double AngleStart, double AngleExtent, double MinScore, Hlong NumMatches, double MaxOverlap, const char* SubPixel, Hlong NumLevels, HTuple* Row, HTuple* Column, HTuple* Angle, HTuple* Score ) const

void HImage::FindNccModel( const HNCCModel& ModelID, double AngleStart, double AngleExtent, double MinScore, Hlong NumMatches, double MaxOverlap, const wchar_t* SubPixel, Hlong NumLevels, HTuple* Row, HTuple* Column, HTuple* Angle, HTuple* Score ) const (Windows only)

void HNCCModel.FindNccModel( HImage image, double angleStart, double angleExtent, double minScore, int numMatches, double maxOverlap, string subPixel, HTuple numLevels, out HTuple row, out HTuple column, out HTuple angle, out HTuple score )

void HNCCModel.FindNccModel( HImage image, double angleStart, double angleExtent, double minScore, int numMatches, double maxOverlap, string subPixel, int numLevels, out HTuple row, out HTuple column, out HTuple angle, out HTuple score )

void HImage.FindNccModel( HNCCModel modelID, double angleStart, double angleExtent, double minScore, int numMatches, double maxOverlap, string subPixel, HTuple numLevels, out HTuple row, out HTuple column, out HTuple angle, out HTuple score )

void HImage.FindNccModel( HNCCModel modelID, double angleStart, double angleExtent, double minScore, int numMatches, double maxOverlap, string subPixel, int numLevels, out HTuple row, out HTuple column, out HTuple angle, out HTuple score )

DescriptionπŸ”—

The operator find_ncc_modelFindNccModel finds the best NumMatchesnumMatchesnum_matches instances of the NCC model ModelIDmodelIDmodel_id in the input image Imageimageimage. The model must have been created previously by calling create_ncc_modelCreateNccModel or read_ncc_modelReadNccModel.

The position and rotation of the found instances of the model is returned in Rowrowrow, Columncolumncolumn, and Angleangleangle. Additionally, the score of each found instance is returned in Scorescorescore.

If NCC finds no suitable match or the matching scores are to low, the search should be performed using a different matching method (see, e.g., β€œSolution Guide II-B - Matching”).

Input parameters in detailπŸ”—

Output parameters in detailπŸ”—

  • Rowrowrow, Columncolumncolumn and Angleangleangle: The position and rotation of the found instances of the model is returned in Rowrowrow, Columncolumncolumn, and Angleangleangle. The coordinates Rowrowrow and Columncolumncolumn are related to the position of the origin of the model in the search image. However, Rowrowrow and Columncolumncolumn do not exactly correspond to this position. Instead, find_ncc_modelFindNccModel returns slightly modified values that are optimized for creating a transformation matrix, that can be used for alignment or visualization of the model. (This has to do with the way HALCON transforms iconic objects, see affine_trans_pixelAffineTransPixel). The example below shows how to create the transformation matrix for alignment and how to calculate the exact coordinates of the found matches.

    By default, the origin is the center of gravity of the domain (region) of the image that was used to create the NCC model with create_ncc_modelCreateNccModel. A different origin can be set with set_ncc_model_originSetNccModelOrigin.

  • Scorescorescore: Additionally, the score of each found instance is returned in Scorescorescore. The score is the normalized cross correlation of the template \(t(r,c)\) and the image \(i(r,c)\):

    \[\begin{eqnarray*} \mathrm{ncc}(r,c) = \frac{1}{n} \sum_{(u,v) \in R} \frac{t(u,v)-m_{t}}{\sqrt{s_{t}^2}} \cdot \frac{i(r+u,c+v)-m_{i}(r,c)}{\sqrt{s_{i}^2(r,c)}} \end{eqnarray*}\]

    Here, \(n\) denotes the number of points in the template, \(R\) denotes the domain (ROI) of the template, \(m_{t}\) is the mean gray value of the template

    \[\begin{eqnarray*} m_{t} = \frac{1}{n} \sum_{(u,v) \in R} t(u,v) \end{eqnarray*}\]

    \(s_{t}^2\) is the variance of the gray values of the template

    \[\begin{eqnarray*} s_{t}^2 = \frac{1}{n} \sum_{(u,v) \in R} \left( t(u,v) - m_{t} \right)^2 \end{eqnarray*}\]

    \(m_{i}(r,c)\) is the mean gray value of the image at position \((r,c)\) over all points of the template (i.e., the template points are shifted by \((r,c)\))

    \[\begin{eqnarray*} m_{i}(r,c) = \frac{1}{n} \sum_{(u,v) \in R} i(r+u,c+v) \end{eqnarray*}\]

    and \(s_{i}^2(r,c)\) is the variance of the gray values of the image at position \((r,c)\) over all points of the template

    \[\begin{eqnarray*} s_{i}^2(r,c) = \frac{1}{n} \sum_{(u,v) \in R} \left( i(r+u,c+v) - m_{i}(r,c) \right)^2 \end{eqnarray*}\]

    The NCC measures how well the template and image correspond at a particular point \((r,c)\). It assumes values between \(-1\) and \(1\). The larger the absolute value of the correlation, the larger the degree of correspondence between the template and image. A value of \(1\) means that the gray values in the image are a linear transformation of the gray values in the template: i(r+u,c+v) = a * t(u,v) + b where \(a > 0\). Similarly, a value of \(-1\) means that the gray values in the image are a linear transformation of the gray values in the template with \(a < 0\). Hence, in this case the template occurs with a reversed polarity in the image. Because of the above property, the NCC is invariant to linear illumination changes.

    The NCC as defined above is used if the NCC model has been created with Metricmetricmetric \(=\) 'use_polarity'"use_polarity". If the model has been created with Metricmetricmetric \(=\) 'ignore_global_polarity'"ignore_global_polarity", the absolute value of ncc(r,c) is used as the score.

Specifying a timeoutπŸ”—

Using the operator set_ncc_model_paramSetNccModelParam you can specify a 'timeout'"timeout" for find_ncc_modelFindNccModel. If find_ncc_modelFindNccModel reaches this 'timeout'"timeout", it terminates without results and returns the error code 9400 (H_ERR_TIMEOUT).

Visualization of the resultsπŸ”—

To display the results found by correlation-based matching, we highly recommend the usage of the procedure dev_display_ncc_matching_results.

Further InformationπŸ”—

For an explanation of the different 2D coordinate systems used in HALCON, see the introduction of chapter Transformations / 2D Transformations.

AttentionπŸ”—

find_ncc_modelFindNccModel can be partially executed on OpenCL devices that support the cl_khr_global_int32_base_atomics OpenCL extension. Only the initial search for the model in the topmost pyramid level is done on the OpenCL device, while the tracking of matches is done on the host CPU. If the domain of the image to search in is substantially smaller than the size of the image, use crop_domainCropDomain to reduce the amount of data that needs to be copied from the OpenCL device to the host CPU. Note that find_ncc_modelFindNccModel using OpenCL may run either substantially faster or slower depending on a wide number of factors, so the only way to tell if using OpenCL is beneficial is by testing with images from the actual application.

Furthermore, note that the internally used memory increases with the number of used threads.

Execution informationπŸ”—

Execution information
  • Supports OpenCL compute devices.

  • Multithreading type: reentrant (runs in parallel with non-exclusive operators).

  • Multithreading scope: global (may be called from any thread).

  • Automatically parallelized on internal data level.

ParametersπŸ”—

Imageimageimage (input_object) singlechannelimage β†’ object (byte* / uint2*)HObject (byte* / uint2*)HImage (byte* / uint2*)HObject (byte* / uint2*)Hobject (byte* / uint2*) *allowed for compute devices

Input image in which the model should be found.

ModelIDmodelIDmodel_id (input_control) ncc_model β†’ (handle)HTuple (HHandle)HNCCModel, HTuple (IntPtr)HHandleHtuple (handle)

Handle of the model.

AngleStartangleStartangle_start (input_control) angle.rad β†’ (real)HTuple (double)HTuple (double)floatHtuple (double)

Smallest rotation of the model.

Default: -0.39-0.39
Suggested values: -3.14, -1.57, -0.79, -0.39, -0.20, 0.0-3.14, -1.57, -0.79, -0.39, -0.20, 0.0

AngleExtentangleExtentangle_extent (input_control) angle.rad β†’ (real)HTuple (double)HTuple (double)floatHtuple (double)

Extent of the rotation angles.

Default: 0.790.79
Suggested values: 6.29, 3.14, 1.57, 0.79, 0.39, 0.06.29, 3.14, 1.57, 0.79, 0.39, 0.0
Restriction: AngleExtent >= 0

MinScoreminScoremin_score (input_control) real β†’ (real)HTuple (double)HTuple (double)floatHtuple (double)

Minimum score of the instances of the model to be found.

Default: 0.80.8
Suggested values: 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.00.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
Value range: 0 ≀ MinScore ≀ 1
Minimum increment: 0.01
Recommended increment: 0.05

NumMatchesnumMatchesnum_matches (input_control) integer β†’ (integer)HTuple (Hlong)HTuple (int / long)intHtuple (Hlong)

Number of instances of the model to be found (or 0 for all matches).

Default: 11
Suggested values: 0, 1, 2, 3, 4, 5, 10, 200, 1, 2, 3, 4, 5, 10, 20

MaxOverlapmaxOverlapmax_overlap (input_control) real β†’ (real)HTuple (double)HTuple (double)floatHtuple (double)

Maximum overlap of the instances of the model to be found.

Default: 0.50.5
Suggested values: 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.00.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
Value range: 0 ≀ MaxOverlap ≀ 1
Minimum increment: 0.01
Recommended increment: 0.05

SubPixelsubPixelsub_pixel (input_control) string β†’ (string)HTuple (HString)HTuple (string)strHtuple (char*)

Subpixel accuracy.

Default: 'true'"true"
List of values: 'false', 'true'"false", "true"

NumLevelsnumLevelsnum_levels (input_control) integer(-array) β†’ (integer)HTuple (Hlong)HTuple (int / long)MaybeSequence[int]Htuple (Hlong)

Number of pyramid levels used in the matching (and lowest pyramid level to use if \(|\textrm{NumLevels}| = 2\)).

Default: 00
List of values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Rowrowrow (output_control) point.y-array β†’ (real)HTuple (double)HTuple (double)Sequence[float]Htuple (double)

Row coordinate of the found instances of the model.

Columncolumncolumn (output_control) point.x-array β†’ (real)HTuple (double)HTuple (double)Sequence[float]Htuple (double)

Column coordinate of the found instances of the model.

Angleangleangle (output_control) angle.rad-array β†’ (real)HTuple (double)HTuple (double)Sequence[float]Htuple (double)

Rotation angle of the found instances of the model.

Scorescorescore (output_control) real-array β†’ (real)HTuple (double)HTuple (double)Sequence[float]Htuple (double)

Score of the found instances of the model.

ExampleπŸ”—

(HDevelop)

create_ncc_model (TemplateImage, 'auto', rad(-45), rad(90), 'auto', \
                  'use_polarity', ModelID)
find_ncc_model (SearchImage, ModelID, rad(-45), rad(90), 0.7, 1, \
                0.5, 'true', 0, Row, Column, Angle, Score)
* Create transformation matrix
vector_angle_to_rigid (0, 0, 0, Row, Column, Angle, HomMat2D)
* Calculate true position of the model origin in the search image
affine_trans_pixel (HomMat2D, 0, 0, RowObject, ColumnObject)
* display the results
dev_display_ncc_matching_results (ModelID, 'red', Row, Column, \
                                  Angle, 0)

ResultπŸ”—

If the parameter values are correct, the operator find_ncc_modelFindNccModel returns the value 2 (H_MSG_TRUE). If the input is empty (no input images are available) the behavior can be set via set_system('no_object_result',<Result>). If necessary, an exception is raised.

Combinations with other operatorsπŸ”—

Combinations

Possible predecessors

create_ncc_modelCreateNccModel, read_ncc_modelReadNccModel, set_ncc_model_originSetNccModelOrigin

Possible successors

clear_ncc_modelClearNccModel

Alternatives

find_generic_shape_modelFindGenericShapeModel

ModuleπŸ”—

Matching