How can I remove a specific part of an image?


Akash Tripuramallu)

I have an image: original image

I want to remove the grey grid part of the image without affecting the rest of the image i.e. the part inside the black circle. I have written a code for this

import cv2
import numpy as np
from PIL import Image
imag = Image.open('results.jpg')
imag.show()

pixelMap = imag.load()

img = Image.new( imag.mode, imag.size)
pixelsNew = img.load()

for i in range(img.size[0]):
    for j in range(img.size[1]):        
        if (( pixelMap[i,j]> (200,0,0)) and (pixelMap[i,j]< (240,0,0))):
            pixelsNew[i,j] = (255,255,255)
        else:
            pixelsNew[i,j] = pixelMap[i,j]
img.show()

With this code I get the following output image: output image

However, some pixels inside the black circle also change to white, which is not what I want. I would like to know how to solve this problem.

username

You can find the index of the black circle and assign a value to the pixel to the left or right of the black circle. Here 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 cirlce
for i in range(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 empty
    if len(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 white
    else:
        img[i,:] = [255, 255, 255]

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

input image

input image

output image

output image

Related


How can I remove a specific part of an image?

Akash Tripuramallu) I have an image: original image I want to remove the grey grid part of the image without affecting the rest of the image i.e. the part inside the black circle. I have written a code for this import cv2 import numpy as np from PIL import Ima

How can I remove a specific part of an image?

Akash Tripuramallu) I have an image: original image I want to remove the grey grid part of the image without affecting the rest of the image i.e. the part inside the black circle. I have written a code for this import cv2 import numpy as np from PIL import Ima

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the last part of the url?

范 Taro I have this url:https://uk.soccerway.com/national/italy/serie-a/20172018/regular-season/r42011/?ICID=TN_02_01_02 I want to remove ?ICID=TN_02_01_02because this string is dynamic so I can't use .Replace()method, any ideas? Bryce Meister Using Uri try to

How can I remove the "base" part of the path?

Denali hardtail I need to recreate a series of folders in a source directory in another environment. The source folder looks like this: C:\temp\stuff\folder01\ C:\temp\stuff\folder02\ C:\temp\stuff\folder03\ C:\temp\stuff\folder02\dog C:\temp\stuff\folder02\ca

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the datetime part of a string?

username For example, I have a string "do something before 9am tomorrow morning". I am using parsedatetime module to parse string and get datetime object from it. I did this according to the parsedatetime documentation: from datetime import datetime import par

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the "base" part of the path?

Denali hardtail I need to recreate a series of folders in a source directory in another environment. The source folder looks like this: C:\temp\stuff\folder01\ C:\temp\stuff\folder02\ C:\temp\stuff\folder03\ C:\temp\stuff\folder02\dog C:\temp\stuff\folder02\ca

How can I remove the first part of a string?

Bill_Johnson77 I have an array containing strings of the following type: var Nameslist = []; Nameslist.push("home.mytest1.James Thomas 14-47"); Nameslist.push("home.mytest1.George Simon 7-2"); Nameslist.push("home.mytest1.Sandy Kylie 3-15"); Now I want to re

How can I remove the "base" part of the path?

Denali hardtail I need to recreate a series of folders in a source directory in another environment. The source folder looks like this: C:\temp\stuff\folder01\ C:\temp\stuff\folder02\ C:\temp\stuff\folder03\ C:\temp\stuff\folder02\dog C:\temp\stuff\folder02\ca

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the last part of the url?

范 Taro I have this url:https://uk.soccerway.com/national/italy/serie-a/20172018/regular-season/r42011/?ICID=TN_02_01_02 I want to remove ?ICID=TN_02_01_02because this string is dynamic so I can't use .Replace()method, any ideas? Bryce Meister Using Uri try to

How can I remove the first part of the for loop?

Caleb Libby For example, I have a variable already defined that I want to use as a counter in a for loop. I found that it is possible to replace the first expression with null, but I am wondering if it is possible to remove it completely using a different vers

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the noisy part of the heartbeat?

Juliet I have a large pulse oximeter signal. Part of it is noisy and destroys my data if I use it. Do you have any strategy to automatically remove noisy parts? (I can't really do it manually since the data is long and there are many channels). Please find the

How can I remove the "base" part of the path?

Denali hardtail I need to recreate a series of folders in a source directory in another environment. The source folder looks like this: C:\temp\stuff\folder01\ C:\temp\stuff\folder02\ C:\temp\stuff\folder03\ C:\temp\stuff\folder02\dog C:\temp\stuff\folder02\ca