System designHow-to

How to display region width and height in one string

Build one labelled display string from a region's width and height: concatenation, tuple conversion, rounding, and routing it to overlay, log, PLC or HMI.

LumaSpec Vision8 min read Share on LinkedIn

On this page 10 sections
  1. 01Why the display call takes one string
  2. 02What you need
  3. 03Step by step: build the string once, show it once
  4. 04Three ways to join the values, and when each fits
  5. 05Worked example: from pixels to a labelled millimetre string
  6. 06Where the same string can go
  7. 07How to verify it worked
  8. 08Common mistakes and the fixes
  9. 09The hardware that runs the script and ships the string
  10. 10Questions

You have measured a region and the numbers are good — Width1 is 3600, Height1 is 1200 — but the display call only shows one of them at a time. What the image overlay, the log file and the PLC message all need is a single labelled line: width=3600 height=1200. This guide shows how to build that string in the script editor of your vision software, the two ways to join the values, how to round and carry units, and how to send one string to three destinations without writing the format three times.

Why the display call takes one string

A call such as write_string draws one string at a time: window handle in, text out, done. Width1 and Height1 are numbers, and each call prints its own line. Two calls give two lines that flicker and scroll; one call with one prepared string gives a stable line an operator can read at a glance. So the fix is not a different display call. It is building the string before the call — and knowing, for every value, whether it is an integer, a real or a tuple, because that decides which join survives.

What you need

  • The interactive script editor of your vision software, with an output window open for the overlay
  • A measured region, so Width1 and Height1 hold real values (3600 px and 1200 px in the examples below)
  • A calibration value in millimetres per pixel if the string should carry physical units (0.01 mm/px in the worked example)
  • An Ethernet or serial link to the PLC, if the same string doubles as the message text

Step by step: build the string once, show it once

  1. Fix the variables and their types. Width1 := 3600 and Height1 := 1200, both integers in pixels; in the running inspection they come from the region tool. If a tool hands you a real (1800.0 instead of 1800), decide now — the string, the log and the PLC register should carry the same form. Checking what an object actually is before you use it is the same discipline that catches pixel-format surprises in converted image objects.
  2. Decide the unit. Pixels for alignment and presence checks; millimetres for measurement records. The unit lives in the string as text — it is part of the message, not a property of the number.
  3. Join labels and values in one expression.
Width1       := 3600          // integer, pixels
Height1      := 1200          // integer, pixels
ResultString := 'width=' + Width1 + ' height=' + Height1

The quoted parts are text. Adding a number to a string converts the number to text in the scripting languages vision software uses, so ResultString now reads width=3600 height=1200.

  1. Show it in one call.
write_string (WindowID, ResultString)

WindowID is the output window handle the script already opens. One call, one line, both values.

  1. Round real values before the join. A real such as 36.00625 prints every digit the processor keeps. Round it first — the arithmetic is in the worked example below.
  2. Give the empty case its own string. If the region tool finds nothing, Width1 has nothing to convert and the join fails. Set the fallback string before you attempt the join, as the empty-case answer below shows.

Three ways to join the values, and when each fits

Method One-line form Output Best for
Concatenation with labels 'width=' + Width1 + ' height=' + Height1 width=3600 height=1200 overlays, PLC messages, quality records
Tuple conversion ResultString := [Width1, Height1] [3600,1200] quick debug prints, machine-readable logs
Calibrated join with units labels + scaled values + 'mm' width=36.00mm height=12.00mm measurement reports, operator screens

Concatenation is the operator-facing form: labelled, readable, unit-ready. The tuple form is the fastest to type and fine for your own debug output: ResultString := [Width1, Height1] converts to the text [3600,1200], brackets included, labels absent. Pick one method per output line — adding a string literal to a tuple, as in 'width=' + [Width1, Height1], either prints the brackets or stops with a type error.

Worked example: from pixels to a labelled millimetre string

The pixel string answers where the part sits. A measurement record wants millimetres, and that takes the calibration scale:

width_mm = Width1_px × mm_per_px height_mm = Height1_px × mm_per_px

Take mm_per_px = 0.01 mm/px (the example input; your calibration plate run sets your own value) with Width1_px = 3600 px and Height1_px = 1200 px:

  • width_mm = 3600 px × 0.01 mm/px = 36.00 mm
  • height_mm = 1200 px × 0.01 mm/px = 12.00 mm

If the scale leaves an awkward real in the variable — 36.00625 mm, say — round before the join: 36.00625 × 100 = 3600.625, round gives 3601, 3601 / 100.0 = 36.01, and the two-decimal text conversion prints 36.01.

Then join with the units inside the text:

Width_mm     := 36.00         // real, millimetres
Height_mm    := 12.00         // real, millimetres
ResultString := 'width=' + Width_mm + 'mm height=' + Height_mm + 'mm'

The result, width=36.00mm height=12.00mm, is 28 characters: 6 + 5 + 2 + 1 + 7 + 5 + 2. The pixel version, width=3600 height=1200, is 22: 6 + 4 + 1 + 7 + 4. If your PLC message register or log column is fixed-length, those are the numbers to size it against.

Where the same string can go

Build ResultString once and pass the same variable to every consumer. On the image, the write_string call above draws it. To the PLC, send it as message text over Ethernet or serial; the PLC displays it in its message view and files it with the part record. On an HMI, the numbers can sit inside the HMI's own picture window — that window's size and content are set in the HMI project, not in the vision script, and sizing a dynamic picture window on an HMI walks through that side. If the operator should also watch the live camera picture on the HMI, that is its own display path, covered in what actually displays live in a PLC HMI. And where no PLC is involved, a vision sensor can serve the image and results to a local browser.

How to verify it worked

Three checks, two minutes:

  • Known part. Measure something you have measured by hand — a 36 by 12 mm blank — and the string must read width=36.00mm height=12.00mm exactly, characters included.
  • Character count. The pixel string is 22 characters, the millimetre string 28. A mismatch means a value joined as a tuple or a label with a stray space.
  • Empty run. Block the camera, trigger once, and confirm the fallback line no part appears instead of an error.

More faults that hide between capture and display are collected on the vision systems topic page.

Common mistakes and the fixes

  • Mixing the two joins. 'width=' + [Width1, Height1] prints brackets or throws a type error. One method per line.
  • Raw real values. 36.006250000000001 on the overlay is the processor's stored digits, not extra measurement precision. Round before the join.
  • Integer division in the chain. Halving a centre coordinate with integer arithmetic turns 3601 / 2 into 1800, losing the 0.5 px. The loss grows when you scale values later, so settle integer or real before formatting.
  • Right string, wrong numbers. Formatting can be perfect while the measurement reads the wrong image or ROI. If values are stable but wrong, check which image and region the measurement is actually bound to before you touch the string.

The hardware that runs the script and ships the string

A script like this runs on a vision controller: a fanless industrial computer with camera ports, built-in light-output channels and the LAN ports that carry the result string to the PLC. If the whole inspection fits on the device instead, smart cameras run it without a separate computer and send the same string over Ethernet.

An example of the class:

Vision controllersHikrobot MV-VC3301H-128G66 I3-8100, 8GB+128G SSD, No PoE, PCIe expansion supported, Incl. VM6200Pro licenseMV-VC3301H-128G66
LAN ports
5+ LAN ports
USB 3 ports
3-4 USB 3 ports
Light outputs
4 built-in channels
Memory
8 GB
$2,419.00View partBuild a system with it

When the station grows and the PLC link wants its own ports:

Vision controllersHikrobot MV-VC2140-128G40-1T VC2100 Vision Controller, N97, 8GB+128G SSD+ 1THDDMV-VC2140-128G40-1T
LAN ports
3-4 LAN ports
USB 3 ports
2 USB 3 ports
$1,679.00View part
Vision controllersHikrobot MV-VC5525-256G70-NN VC5000 Vision Controller, 12th-I5, 32GB+256G SSD, Supported PCIEMV-VC5525-256G70-NN
LAN ports
5+ LAN ports
USB 3 ports
3-4 USB 3 ports
$4,479.00 · quotedView and quote

Each carries built-in light-output channels, so the same box can drive the light while the script ships the string. To match a controller to your camera count, cycle time and protocol, walk the station through the system configurator or send the set-up to the contact page. In-stock items ship within one business day.

Questions

Why does my joined string print brackets around the numbers?

Because the whole tuple was converted instead of each value being joined separately. [Width1, Height1] converts to the text [3600,1200], brackets included. When you concatenate, add each number to the quoted labels on its own: 'width=' + Width1 + ' height=' + Height1. Use one method per line — adding a string literal to a tuple either prints the brackets or stops the script with a type error.

How do I keep a width value to two decimal places in the string?

Scale, round, scale back: multiply the millimetre value by 100, round to a whole number, divide by 100.0, then let the two-decimal text conversion print it. For 36.00625 mm that is 3600.625 → 3601 → 36.01 mm. If your environment's number-to-text conversion accepts a digit count, use that instead. The rule is to round before the join, never during it.

How many characters should the PLC receive buffer reserve for the string?

Size it from the worst case your field of view can produce, not from the example. The pixel string width=3600 height=1200 is 22 characters (6 + 4 + 1 + 7 + 4); the millimetre string width=36.00mm height=12.00mm is 28 (6 + 5 + 2 + 1 + 7 + 5 + 2). A five-digit pixel width adds one more. Add a few characters for terminator or protocol overhead and use that figure.

What should the string contain when the region tool finds nothing?

A defined message, not an empty value. When the region is missing, Width1 has nothing to convert and the join either fails or prints placeholder text. Assign the fallback before joining: ResultString := 'no part'. The operator sees a readable line, the PLC message stays valid, and the log separates 'measured, out of tolerance' from 'nothing measured' — two faults with different fixes.

Can the same string go to the overlay, the log and the PLC?

Yes, and it should. Build ResultString once and pass that one variable to the display call, the log writer and the Ethernet or serial send. Three separately formatted copies drift apart the first time someone edits one of them. If an output needs different precision — more decimals in the log than on screen — build the second string in the same place, right beside the first.