0

program is about removing the gradient background color Currently it takes about 20 second for single image of size 420X560 size code is


from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
import matplotlib.pyplot as plt  
from matplotlib.colors import LogNorm 
import numpy as np
import os
import cv2
  
def backgroundRemovel(url):    
    img = cv2.imread(url)    
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.imshow(img)
    plt.title('original Image')
    plt.show() 
    color=img[0,0]
    firstPixelRGB = sRGBColor(color[0], color[1], color[2], is_upscaled=True);
    firstPixelLab = convert_color(firstPixelRGB, LabColor);
    t=img.shape
    x_dim=t[0]
    y_dim=t[1]
    for i in range(x_dim):
          for j in range(y_dim):
            rgbCurPixel=img[i,j]
            curPixelRGB=sRGBColor(rgbCurPixel[0], rgbCurPixel[1], rgbCurPixel[2], is_upscaled=True);
            curPixelLab=convert_color(curPixelRGB, LabColor);
            delta_e = delta_e_cie2000(firstPixelLab, curPixelLab);
            #print("difference is "+str(delta_e))
            if delta_e<15: 
                img[i, j] = (0, 0, 0)
   
    return img  


fnmae="image.jpeg"
open_cv_image = backgroundRemovel(fname)
plt.imshow(open_cv_image)
plt.title('Background Removed Image')
plt.show() 

Input Image: "Input" Desired: "Output"

if there is any better way to remove gradient background of image please do share

  • 1
    If your final result is some NumPy array, why not use NumPy's ability to perform on whole arrays in first place? Please [edit](https://stackoverflow.com/posts/64590846/edit) your question, and describe, what you actually want to achieve. Provide a [mre], include all necessary code to test your solution. – HansHirse Oct 29 '20 at 12:14
  • done the change but still it is taking about 20 seconds – Karan Rajbhar Oct 29 '20 at 12:17
  • You need to provide `sRGBColor`, `convert_color`, etc. All these functions need to be modified to work on whole NumPy arrays rather than single pixels. In the end, you then apply all these functions once to your image, i.e. no loops! – HansHirse Oct 29 '20 at 12:20
  • how do i modify the function of a library , library i am using is colormath – Karan Rajbhar Oct 29 '20 at 12:24
  • but i do have to check rgb value of each pixel how is it possible without looping – Karan Rajbhar Oct 29 '20 at 12:26
  • That's why I mentioned to provide a [mre]! Your code still doesn't reflect which libraries you use (`import` statements), etc. – HansHirse Oct 29 '20 at 12:27
  • import statements has been added – Karan Rajbhar Oct 29 '20 at 12:31
  • Please include input and expected output images else your code is not a *"minimal reproducible example"*. – Mark Setchell Oct 29 '20 at 12:34
  • Ok, now the actual problem is: [`colormath` isn't vectorised](https://stackoverflow.com/questions/37608210/how-to-convert-a-numpy-array-of-rgb-values-to-lab-values-with-colormath-color-co#comment62757991_37608210). So, if you're tied to `colormath`, you need loops, thus you'll have to accept long execution times. – HansHirse Oct 29 '20 at 12:34
  • You could remove that grey background by floodfilling, example here https://stackoverflow.com/a/64593222/2836621 See in code where comment says about *"Segment interesting part"*. – Mark Setchell Oct 30 '20 at 08:48

0 Answers0