System designHow-to

insertShape output as grayscale image: convert RGB after drawing

insertShape always returns truecolor, even on mono frames. Convert the RGB output to a grayscale image after drawing, with luma weights and buffer math.

LumaSpec Vision8 min read Share on LinkedIn

On this page 10 sections
  1. 01Why the output is always truecolor
  2. 02What you need
  3. 03Convert the output right after drawing
  4. 04Which overlay colors survive rgb2gray
  5. 05Worked example: a 3072 × 2048 mono8 frame logged at 1 fps
  6. 06Check that the conversion worked
  7. 07Common mistakes
  8. 08Where this loop runs on the line
  9. 09The decision, case by case
  10. 10Questions

You draw a bounding box or a pass/fail mark on a camera frame, and the function hands back a three-channel color image even though the camera, the algorithm and the log format are all grayscale. insertShape and insertText are specified to return truecolor, and there is no output-format argument to change that. This page gives you the conversion step that belongs right after the draw call, the numbers behind it: which overlay colors survive rgb2gray, what the channel count does to buffers and log volume, and the checks that prove the gray copy still matches the original frame.

Why the output is always truecolor

The output of insertShape is specified as truecolor: an M × N × 3 image, whatever the input. Pass a mono8 frame and the library broadcasts your single channel to three identical planes before drawing, then draws the shape in color on top. insertText follows the same convention, so text comes back in color too. Nothing in the call selects a single-channel return, which is why the conversion is left to you — and why rgb2gray exists as the companion step for exactly this.

If your mono8 capture is already arriving as three identical channels, fix that at the source: why a mono8 camera image arrives as three channels walks through the usual causes.

What you need

  • The draw calls: insertShape for rectangles, lines, polygons and circles; insertText for labels.
  • The luma conversion: rgb2gray, or the weighted sum in the formula block below if your environment has no helper.
  • A dimension check: the gray copy must be M × N, the truecolor return is M × N × 3.
  • The raw frame variable, kept untouched for every measurement.

Convert the output right after drawing

The variable names below are illustrative; insertShape, insertText and rgb2gray are the function names the library documents.

frame          % mono8, M x N, your capture
annotated_rgb  = insertShape(frame, ...)   % truecolor uint8, M x N x 3
annotated_gray = rgb2gray(annotated_rgb)   % uint8, M x N, one channel
  1. Capture into frame and leave it alone. Blob, match and threshold steps run on this mono8 original; the overlay is for people and for logs.
  2. Draw on a copy: annotated_rgb = insertShape(frame, ...) or insertText(frame, ...). Accept the truecolor return; no setting changes it.
  3. Convert in the same call chain: annotated_gray = rgb2gray(annotated_rgb). Convert every frame before it enters any queue, file writer or network sender.
  4. Pick pen colors by luma, not hue — the next section gives the numbers.
  5. Route annotated_gray to thresholds, matching and storage. Keep annotated_rgb only for the one display window a person watches.
  6. Run the four checks in the verification section on the first frame of every new recipe.
gray = round(0.2989·R + 0.5870·G + 0.1140·B) bytes_gray = rows × cols × 1 bytes_truecolor = rows × cols × 3

The coefficients are the BT.601 luma weights rgb2gray applies; the result rounds back to 8-bit. If part of your team works in graphical software instead, the same one-call conversion exists in RGB to grayscale conversion in LabVIEW Vision.

Which overlay colors survive rgb2gray

The pen brightness after conversion decides whether your mark is visible. Two worked lines: a green pen keeps 0.5870 × 255 = 149.7 → 150; a red pen falls to 0.2989 × 255 = 76.2 → 76. The rest of the palette follows the same sum.

Pen (R, G, B) Gray value after rgb2gray Visible on a dark frame?
White 255, 255, 255 255 Yes
Yellow 255, 255, 0 226 Yes
Cyan 0, 255, 255 179 Yes
Green 0, 255, 0 150 Yes
Red 255, 0, 0 76 Faint
Blue 0, 0, 255 29 No

Worked example: a 3072 × 2048 mono8 frame logged at 1 fps

Inputs: a 3072 × 2048 frame = 6,291,456 pixels (a typical 6 MP mono layout; scale the arithmetic to your own resolution); 1 byte per pixel in, 3 bytes per pixel for the truecolor return; a conservative 1 fps of continuous annotated frames into the log; luma coefficients 0.2989, 0.5870 and 0.1140. The rule in words: drawing changes no pixel count, only the channel count, and conversion restores it.

  • Mono8 per frame: 6,291,456 px × 1 B = 6,291,456 B = 6.0 MiB.
  • insertShape return: 6,291,456 px × 3 B = 18,874,368 B = 18.0 MiB.
  • After rgb2gray: 6,291,456 px × 1 B = 6.0 MiB — a saving of 12.0 MiB per frame, 67 %.
  • One hour at 1 fps: the truecolor log writes 18.0 MiB × 3,600 = 64,800 MiB ≈ 63 GiB; the gray log writes 6.0 MiB × 3,600 = 21,600 MiB ≈ 21 GiB.

Decision: convert before anything stores or transmits the frame. From 1 fps of continuous logging upward, the truecolor return costs three times the disk and the wire — 63 GiB against 21 GiB per hour. Keep a truecolor copy only for the display window a person watches; let every machine consumer (threshold, matcher, file writer, network link) take annotated_gray.

Check that the conversion worked

  • Dimensions: annotated_gray is M × N; the truecolor return was M × N × 3. A surviving third dimension means the conversion did not run — see checking the multi-channel buffer size before processing.
  • Background fidelity: pick one pixel far from every drawn shape. Because 0.2989 + 0.5870 + 0.1140 = 0.9999 and all three planes carry the same value, rgb2gray returns that pixel to its exact original value after rounding. Gray copy and raw frame must match there.
  • Overlay contrast: sample the pen. Green reads about 150, red about 76. Compare both with your binarization floor — auto threshold methods for grayscale images shows why a mark under the floor disappears in the binary image.
  • Log size: after an hour the gray log sits near one third of the truecolor size. If not, a sender upstream is still writing RGB — smart camera data logging to the network has the bandwidth math.

Common mistakes

  1. Drawing red fail marks, then converting. At 76 the mark can sink inside a mid-gray part's own histogram; switch to green or white.
  2. Assuming a grayscale input returns a grayscale output. The spec says truecolor either way, so the conversion is owed regardless.
  3. Measuring on the annotated image. The box's own pixels corrupt blob areas and edge positions; measure on frame.
  4. Thresholding one plane of the RGB copy. The shape spans all three planes, so the overlay bleeds into whichever channel you pick.
  5. Sending the truecolor return across the network or into long-term storage unconverted — a permanent 3× on bandwidth and disk.

If the overlay must reach a display but never the stored image, text overlay on a live camera image without altering the frame is the cleaner pattern. To write a region permanently into a stored gray image, see burning a drawn ROI into a grayscale image.

Where this loop runs on the line

A mono sensor is the natural capture side: you convert the annotated copy back to gray because the sensor never had color to begin with. The MV-SC5060XM-08M-WBN is a 6 MP mono GigE smart camera with an M12 mount.

Smart cameras & vision sensorsHikrobot MV-SC5060XM-08M-WBN SC5000X series Smart Camera, 6 MP, Mono, 8 mm focal length, M12-mountMV-SC5060XM-08M-WBN
Interface
GigE
Resolution
6-10 MP
Sensor format
1/2.4"
Shutter
Rolling
$1,629.00View part

In a multi-camera cell, the draw-and-convert loop belongs on the controller, next to the matching and decision logic. The MV-VC3301H-128G66 is an Intel Core i3 unit with 8 GB memory, 5+ LAN ports and 4 built-in light channels.

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

The decision, case by case

  • Frames that feed thresholds, matching, training sets or log files: draw, convert with rgb2gray in the same call chain, store the gray copy. Never store the truecolor return at production rates.
  • The one display a person watches: keep the truecolor copy there if color classes carry meaning (green pass, red fail); it costs 3× on that single stream, which a display link tolerates.
  • Pen colors: white and green by default, yellow or cyan for a third class, red and blue only with a lowered threshold floor — never on dark parts.
  • Every new recipe: run the four checks (dimensions, background pixel match, pen contrast, log size) on the first frame before the line runs.

If you are choosing the camera or controller this loop runs on, the camera finder filters by resolution and interface, the smart cameras and vision controllers pages list the current lines, and the systems topic page collects the rest of the processing-side guides. For an overlay pipeline with a wrinkle this page does not cover, contact us.

Questions

Why does insertShape return a truecolor image when my input frame is grayscale?

The function output is specified as truecolor only; there is no name-value option to request a single-channel return. When you pass a mono frame, its one channel is broadcast to three identical planes and the shapes are drawn in color on top. The path the library intends is to convert yourself: call rgb2gray on the output right after drawing and keep that as your processing and logging copy.

Which overlay color stays visible after rgb2gray?

Judge the pen by its luma, not its hue. White at 255 stays 255, yellow lands near 226, green near 150. Red falls to about 76 and blue to about 29, so both can sink below a dark-image threshold and vanish. Draw results in green or white; if you need two classes on one image, use white and green rather than red and blue.

How much smaller is the grayscale image than the truecolor one?

Exactly one third, because the pixel count does not change and the byte depth drops from three channels to one. A 3072 × 2048 mono8 frame is 6.0 MiB; the insertShape return is 18.0 MiB; after rgb2gray you are back to 6.0 MiB, saving 12.0 MiB per frame. Logged continuously at 1 fps, that is the difference between roughly 63 GiB and 21 GiB per hour of disk.

Does converting to grayscale lose detail in the underlying image?

Not for a mono capture. The truecolor return carries your gray frame in all three planes, and the luma weights sum to 0.9999, so a pixel with R = G = B converts back to its original value after rounding. Only the pixels the shapes touched change. Spot-check one pixel far from any drawn shape against the raw frame; the two values must match.

Can I draw shapes directly on the mono frame and skip the conversion?

You can pass the mono frame as the input, but the output is still specified as truecolor, so you end up converting anyway. Drawing first and converting once is the shorter path. The one thing to control is the pen: pick it by luma (white or green), because the anti-aliased edge of a dark pen converts into a faint ring around the shape.