Skip to content

Bitwise AND Images

SUMMARY

Bitwise AND Images performs bitwise AND between two images.

Computes the pixel-wise logical AND of two images. For binary images, the result is 1 only where both images have 1. Useful for mask intersection, combining detection results, and logical masking operations.

Use this Skill when you want to intersect two binary masks or images.

The Skill

python
from telekinesis import pupil

result = pupil.bitwise_and_images(image_1=image, image_2=image_2)

API Reference

Example

Image A

Input 1

First Image

Image B

Input 1

Second Mask

Result

Output

Bitwise AND Result

The Code

python
from telekinesis import pupil
from datatypes import io
import pathlib
import numpy as np
from loguru import logger

DATA_DIR = pathlib.Path("path/to/telekinesis-data")

# Load image
filepath_1 = str(DATA_DIR / "images" / "bin_picking_metal_2.jpg")
image_a = io.load_image(filepath=filepath_1)

# Create empty mask with bounding box
bbox = [450, 210, 1040, 616]
x1, y1, x2, y2 = bbox
mask = np.zeros(image_a.to_numpy().shape[:2], dtype=np.uint8)
mask[y1:y2, x1:x2] = 255

# Perform bitwise AND between image and mask
result = pupil.bitwise_and_images(image_a=image_a, image_b=mask)

# Access results
result_np = result.to_numpy()
logger.success("Bitwise AND. Output shape: {}", result_np.shape)

The Explanation of the Code

The code begins by importing the necessary modules: pupil for image processing operations, io for data handling, pathlib for path management, and loguru for logging.

python
from telekinesis import pupil
from datatypes import io
import pathlib
from loguru import logger

Next, images are loaded or created. Both images must have the same dimensions. For binary images, AND yields 1 only where both are 1.

python
DATA_DIR = pathlib.Path("path/to/telekinesis-data")

filepath_1 = str(DATA_DIR / "images" / "bin_picking_metal_2.jpg")
image_a = io.load_image(filepath=filepath_1)
# Create mask or load second image...

The main operation uses the bitwise_and_images Skill from the pupil module. This Skill computes the pixel-wise logical AND of two images, useful for mask intersection and ROI masking.

python
result = pupil.bitwise_and_images(image_a=image_a, image_b=mask)
logger.success("Bitwise AND. Output shape: {}", result.to_numpy().shape)

Finally, the result is converted to a NumPy array for further processing or visualization.

python
result_np = result.to_numpy()
logger.success("Bitwise AND. Output shape: {}", result_np.shape)

This operation is particularly useful in robotics and vision pipelines for mask intersection, ROI masking, and logical filtering, where combining two binary masks or images is required.

Running the Example

Runnable examples are available in the Telekinesis examples repository. Follow the README in that repository to set up the environment. Once set up, you can run this specific example with:

bash
cd telekinesis-examples
python examples/pupil_examples.py --example bitwise_and_images

How to Tune the Parameters

The bitwise_and_images Skill has no tunable parameters. It requires two input images of the same dimensions.

TIP

Best practice: Ensure both images have identical width and height. Use for mask intersection, ROI masking, or logical filtering. Combine with bitwise_not_image to apply operations to non-masked regions.

Where to Use the Skill in a Pipeline

Bitwise AND Images is commonly used in the following pipelines:

  • Mask intersection - Combine multiple segmentation masks
  • ROI masking - Apply one mask within another
  • Logical filtering - Keep pixels satisfying multiple conditions

Related skills to build such a pipeline:

  • bitwise_or_images: Union of masks
  • bitwise_xor_images: Exclusive regions
  • bitwise_not_image: Invert masks

Alternative Skills

Skillvs. Bitwise AND
bitwise_or_imagesUse for union (OR) instead of intersection (AND).
bitwise_xor_imagesUse for exclusive regions (XOR).

When Not to Use the Skill

Do not use Bitwise AND Images when:

  • You need union of masks (Use bitwise_or_images)
  • You need exclusive regions (Use bitwise_xor_images)
  • Images have different sizes (Resize to match first)