user2380654
user2380654

Reputation:

Generate HALCON region from contour

I have a polygon, consisting of 2D points representing pixel coordinates, in an internal data structure. I need this polygon as an HALCON region (HRegion). The conversion is supposed to happen like that:

HTuple hCols, hRows;
for (auto n = 0; n < nNodes; ++n)
{
    auto v2dNode = GetNode(n);
    hCols.Append(v2dNode.GetX());
    hRows.Append(v2dNode.GetY());
}

HalconCpp::HObject hContour;
HalconCpp::GenContourPolygonXld(&hContour, hRows, hCols);

HalconCpp::HObject hRegion;
HalconCpp::GenRegionContourXld(hContour, &hRegion, "filled");

Whereas the contour (HContour) is valid, according to Halcon Variable Inspect, the created region (hRegion) seems to be empty. HRegion::IsInitialized returns true, but HRegion::AreaCenter would return zero for both area and position, which is clearly wrong.

There's constructor versions of these function calls (e.g. GenContourPolygonXld), too, using the "iconic" types HXLDCont and HRegion, which result in an incorrect region as well.

What I also tried is to serialize the contour, save it in an file and load it in HDevelop. There, the corresponding code does create a valid region:

open_file('D:/HContour.mvt', 'input_binary', hFile)
fread_serialized_item(hFile, hSer)
deserialize_xld(hContour, hSer)
close_file(hFile)
gen_region_contour_xld(hContour, hRegion, 'filled')
area_center(hRegion, Area, Row, Column)

Contour Corresponding region

In C# I also loaded that contour file and tried to create the corresponding region. That approach resulted in an incorrectly empy region however, too:

HObject hObj;
using (var hFile = new HFile(@"D:\\HContour.mvt", "input_binary"))
{
    FreadSerializedItem(hFile, out var hSerialized);
    DeserializeXld(out hObj, hSerialized);
}
var hContour = new HXLDCont(hObj);
var hRegion = hContour.GenRegionContourXld("filled");
var area = hRegion.AreaCenter(out double row, out var col);
Console.WriteLine($"Area: {area}, Center: {col}|{row}");

The Halcon version is 12.0.3.

Is there a bug in the library, or am I doing it wrong in the C++ and C# code?


Edit:

Before any Halcon code is executed, the following settings are made:

HalconCpp::ResetObjDb(5000, 5000, 1);
HalconCpp::SetSystem("clip_region", "false");
HalconCpp::SetSystem("store_empty_region", "true");

All coordinates are in a valid range, and regions are not clipped.

The contour that has been used for testing is this.

Upvotes: 1

Views: 1265

Answers (1)

Vladimir Perković
Vladimir Perković

Reputation: 1433

Could it be that your region is partially outside of predefined region work space. Meaning that some of the pixels have coordinates less than zero?

If that's the case, all you need to do before loading is run this command:

set_system ('clip_region', 'false')

Upvotes: 1

Related Questions