Shahroz Atiq
Shahroz Atiq

Reputation: 11

How to generate a 3d mesh from a 2d mask image

I have trained a segmentation model to detect walls from an image of a floorplan. Now, I wish to create a 3D mesh from the obtained masked array. I also need a mesh format that is compatible with a web viewer.

Below is the code that I used to extract the walls in my 2D image and some sample images.

This is the Masked Generated with AI:

These are the walls:

I imagine the next step is to create the 3D model out of it by extruding the footprint of the walls to a given height. How can I do that?

import cv2
import os
import numpy as np
import matplotlib.pyplot as plt

# Parameters
wall_height = 150
img_path = 'output_mask.png'

# Load the image
img = cv2.imread(img_path)

# Get the original dimensions of the image
original_height, original_width = img.shape[:2]

# Resize the image while preserving aspect ratio
new_width = 1024
new_height = int((new_width / original_width) * original_height)
img_resized = cv2.resize(img, (new_width, new_height))

# Create a blank mask for the walls
walls = np.zeros(img_resized.shape[:2], dtype=np.uint8)

# Extract the walls (walls are white in the image)
walls[(img_resized == [255, 255, 255]).all(axis=2)] = 255

Upvotes: 0

Views: 153

Answers (1)

el_grezeq
el_grezeq

Reputation: 133

One solution would be to:

  1. Detect lines in your second image using either the Hough Transform, RANSAC or any other suitable technique.
  2. Check for the intersections of these lines in the 2D/image space. These are your 3D mesh vertices (with z coordinates = 0). The lines connecting these vertices are edges.
  3. Create new vertices by using the x and y coordinates of the previous vertices and replacing z by your walls height (z_height).
  4. Connect these vertices to the others: vertices with the same x and y coordinates (but z=0 and z=z_height) share an edge and the edges in the z=0 plane are the same than in the z=z_height plane.
  5. Create quad faces from the edges (or break them into tri faces if needed). Be careful that the vertices/edges order must stay consistent (usually counter-clockwise).
  6. Save the result in a file, in whatever format you want: STL, OBJ, OFF, etc.

Upvotes: 0

Related Questions