Untitled
To modify the plot_model
function so that it takes only the floor_image
instead of the entire Floor
object, you need to adapt the function to work directly with the image data and remove dependencies on other attributes and methods related to the Floor
object.
Here’s a step-by-step approach to refactor the function:
-
Extract the
floor_image
directly: Instead of accessingfloor.rect
and other properties from theFloor
object, use thefloor_image
parameter to directly work with the image. -
Compute image patches: If you need to process specific regions of the image (e.g., patches), you should adapt your code to work with image coordinates and directly extract these patches from
floor_image
. -
Modify plotting logic: Update the plotting logic to work with the given
floor_image
and remove references to other parts of theFloor
object.
Here's a refactored version of the plot_model
function:
import matplotlib.pyplot as plt import skimage.color import skimage.io from skimage.filters import sobel import time import numpy as np def plot_model(self, floor_image: np.ndarray, model: 'BaseFloorPhotoModel', save: str, outline: bool = False, verbose: bool = False) -> None: """ Plot a given model into a rasterized image. :param floor_image: Image of the floor :param model: Model :param save: Save file :param outline: If true, apply outline algorithm :param verbose: Verbose mode """ ax: 'plt.Axes' fig, ax = plt.subplots(dpi=DEFAULT_PLOT_DPI) ax.set_aspect('equal') ax.set_facecolor('#000000') # Display the floor image ax.imshow(floor_image, cmap='gray', origin='upper') # Get image limits lim_x = ax.get_xlim() lim_y = ax.get_ylim() # Compute segmentation t0 = time.time() # Assume you need to create patches for the segmentation; # You may need to adjust this part based on your use case patches = self._generate_image_patches(floor_image) # Custom function to generate image patches for (xmin, xmax, ymin, ymax) in patches: patch_image = self._process_photo(xmin, xmax, ymin, ymax, floor_image) segmentation = model.predict_image(patch_image, threshold=False) plt.imshow( 1 - segmentation, cmap='binary', extent=[xmin, xmax, ymin, ymax], origin='upper') if verbose: print(f'Segmentation model computing time: {time.time() - t0:.2f}') plt.xlim(lim_x) plt.ylim(lim_y) ax.axis('off') fig.patch.set_visible(False) fig.tight_layout(pad=0) with open(make_dirs(save), 'wb') as outfile: fig.canvas.print_png(outfile) plt.close() _crop_image(save) # Apply outline if outline: gray_img = skimage.color.rgb2gray(skimage.color.rgba2rgb(floor_image)) fig, ax = plt.subplots(ncols=1, nrows=1, dpi=DEFAULT_PLOT_DPI) fig.tight_layout() ax.imshow(sobel(gray_img), cmap='gray') ax.axis('off') fig.patch.set_visible(False) plt.savefig(save) plt.close() _crop_image(save) def _generate_image_patches(self, floor_image: np.ndarray) -> list: """ Generate patches from the floor image. This is a placeholder for your actual patch generation logic. :param floor_image: The image of the floor. :return: List of tuples defining the bounding boxes for patches. """ # Example implementation; you need to adapt it to your specific use case. height, width = floor_image.shape[:2] patch_size = 256 # Example patch size patches = [] for x in range(0, width, patch_size): for y in range(0, height, patch_size): xmin = x xmax = min(x + patch_size, width) ymin = y ymax = min(y + patch_size, height) patches.append((xmin, xmax, ymin, ymax)) return patches def _process_photo(self, xmin: int, xmax: int, ymin: int, ymax: int, floor_image: np.ndarray) -> np.ndarray: """ Process a patch of the floor image. This is a placeholder for your actual photo processing logic. :param xmin: X coordinate of the patch :param xmax: X coordinate of the patch :param ymin: Y coordinate of the patch :param ymax: Y coordinate of the patch :param floor_image: The image of the floor. :return: Processed patch image. """ return floor_image[ymin:ymax, xmin:xmax]
Changes Made:
- Removed: All references to
floor.rect
and related attributes. - Added: Direct use of
floor_image
for plotting and processing. - Modified: The patch generation and processing logic to work with the
floor_image
directly. - Assumed: You might have or need to implement functions for generating image patches and processing them.
You will need to adjust _generate_image_patches
and _process_photo
according to your specific needs and how you intend to process patches of the image.