Skip to content Skip to sidebar Skip to footer

How To Delete A Certain Part Of An Image?

I have an image: Original Image I want to remove the grey mesh part of the image without affecting the rest of the image i.e., the part inside the black circle. I have written a co

Solution 1:

You can find the indices of black circle and assign values to the pixels that are either to the left or to the right of black circle. Below is the sample code for this

import cv2
import numpy as np

# read the image
img = cv2.imread('original.png')
cv2.imshow("Image", img)

# convert image to numpy array and also to grayscale
img = np.array(img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# get height and width of image
[rows, cols] = gray.shape

# now extract one row from image, find indices of black circle# and make those pixels white which are to the left/right# of black cirlcefor i inrange(rows):
    row = gray[i, :] # extract row of image
    indices = np.where(row == 0)    # find indices of black circle
    indices = indices[0]

    # if indices are not emptyiflen(indices) > 0:
        # find starting/ending column index
        si = indices[0]
        ei = indices[len(indices)-1]

        # assign values to the range of pixels
        img[i, 0:si-1] = [255, 255, 255]
        img[i, ei+1:] = [255, 255, 255]
    # if indices is empty then make whole row whiteelse:
        img[i,:] = [255, 255, 255]

cv2.imshow("Modified Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Input Image

Input Image

Output Image

Output Image

Post a Comment for "How To Delete A Certain Part Of An Image?"