How to use open CV to remove object from the selected part of an image without distorting the background image?

74 Views Asked by At

Everyone I'm trying to implement the openCV to remove objects from images by selecting on it but when I select the part to remove by making a square on it then that part will blur with removing the object but I'm not getting the expected result I want without distorting the background part of an image inPainting will use so also I have provided the code please anyone who know the better solution so provide me.

Here, I have provided to you Input Image, Selected Region Image, Output Image

Input Image -

Selected Region Image -

Output Image -

import numpy as np

# Function to handle mouse events
def draw_square(event, x, y, flags, param):
    global drawing, mask, mask_history, mask_index, square_start, square_end

    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        square_start = (x, y)
        square_end = (x, y)
        # Clear the redo history when a new action is initiated
        mask_history = mask_history[:mask_index + 1]
        mask_index += 1

    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing:
            square_end = (x, y)

    elif event == cv2.EVENT_LBUTTONUP:
        if drawing:
            drawing = False
            x1, y1 = square_start
            x2, y2 = square_end
            # Draw a white rectangle on the mask to mark the area to remove
            cv2.rectangle(mask, (x1, y1), (x2, y2), 255, -1)  # Fill the rectangle
            mask_history.append(mask.copy())

# Load the image
img = cv2.imread('chair_beach.jpg')

# Set the zoom ratio (adjust this value as needed)
zoom_ratio = 1.0  # No zoom

# Resize the image according to the zoom ratio
img = cv2.resize(img, None, fx=zoom_ratio, fy=zoom_ratio)

# Get the image size for window creation
image_height, image_width = img.shape[:2]

# Create a mask for user-drawn areas to remove
mask = np.zeros(img.shape[:2], dtype=np.uint8)

# Create a window with a fixed size matching the image size
cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Image', image_width, image_height)

# Set the mouse event callback function
cv2.setMouseCallback('Image', draw_square)

drawing = False
square_start = (-1, -1)
square_end = (-1, -1)
mask_history = []
mask_index = -1

while True:
    # Display the image with the mask
    masked_image = cv2.inpaint(img, mask, 2, cv2.INPAINT_TELEA)

    # Draw the outline of the square on the image
    x1, y1 = square_start
    x2, y2 = square_end
    if drawing:
        cv2.rectangle(masked_image, (x1, y1), (x2, y2), (0, 0, 255), 2)  # Red outline

    cv2.imshow('Image', masked_image)

    # Check for key press
    key = cv2.waitKey(1) & 0xFF

    # Break the loop if 'q' is pressed
    if key == ord('q'):
        break

    # Undo the last action if 'z' is pressed
    if key == ord('z') and mask_index > 0:
        mask_index -= 1
        mask = mask_history[mask_index].copy()

    # Redo the last undone action if 'y' is pressed
    if key == ord('y') and mask_index < len(mask_history) - 1:
        mask_index += 1
        mask = mask_history[mask_index].copy()

    # Reset the mask if 'r' is pressed
    if key == ord('r'):
        mask = np.zeros(img.shape[:2], dtype=np.uint8)
        mask_history = []
        mask_index = -1

cv2.destroyAllWindows()

I'm wanted to get an accurate result by removing object from Image using OPenCV without any distortion of an image.

0

There are 0 best solutions below