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:
Desired:
if there is any better way to remove gradient background of image please do share
Hi I have a problem with my image color. I'm very new to this all but I was trying to follow some tutorials. I want to know how I can get rid off the blue shade over my pictures?
[blue bottle][1]
Code:
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm
DATADIR = "C:\\Users\woute\Datasets\Garbage classification"
CATEGORIES = ["glass", "cardboard"]
for category in CATEGORIES:
path = os.path.join(DATADIR,category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_COLOR)
plt.imshow(img_array)
plt.show() # display!
break
break
[1]: https://i.stack.imgur.com/DaZtA.png
[2]: https://i.stack.imgur.com/MP0sQ.png
I'm trying to convert a grayscale image's pixels into a numpy array.
Working on google colab.
it shows an error saying: TypeError: 'numpy.uint8' object is not iterable
enter code here
##load Library
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from google.colab import files
from scipy import misc #to see image
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from PIL import Image
pil_im = Image.open('papa.png')
pil_imgray = pil_im.convert('LA')
img = np.array(list(pil_imgray.getdata(band=0)), float)
img.shape = (pil_imgray.size[1], pil_imgray.size[0])
plt.imshow(img)
for eachRow in img:
for eachPixel in eachRow:
x_test.append(sum(eachPixel)/3.0)
You can directly load the image using matplotlib:
plt.imread('papa.png')
or you can convert your PIL image with:
img = np.asarray(pil_im)
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
import Image
from scipy import ndimage
import Image, ImageDraw
import PIL
import cv
import cv2
from scipy.ndimage import measurements, morphology
from PIL import Image
from numpy import *
from scipy.ndimage import filters
import pylab
import mahotas
from mamba import*
import mambaDraw
from PIL import Image, ImageDraw
img = np.asarray(Image.open('test.tif').convert('L'))
img = 1 * (img < 127)
draw = ImageDraw.Draw(img)
draw.line((100,200, 150,300), fill=128)
plt.imshow(img, cmap=cm.Greys_r)
plt.show()
I want to put some grid lines on image, but get the following error:
Traceback (most recent call last):
File "C:\Documents and Settings\All Users.WINDOWS\Документыline 24, in <module>
draw = ImageDraw.Draw(img)
File "C:\Python27\lib\site-packages\PIL\ImageDraw.py", line 296, in Draw
return ImageDraw(im, mode)
File "C:\Python27\lib\site-packages\PIL\ImageDraw.py", line 61, in __init__
im.load()
AttributeError: 'numpy.ndarray' object has no attribute 'load'
What is wrong with this code? How do I put a 100x100 grid on an image?
Your error here is that you convert a PIL image to a numpy array, but then you use the PIL ImageDraw library on the numpy array.
You can draw the lines in either PIL or Numpy, whichever you prefer, but you need to use Numpy to work with Numpy objects and PIL to work with PIL objects. Saullo showed how to do it in PIL, in numpy you could do:
img[:, 100:110] = 0
or for a grid 10 pixels wide, every 100:
for i in range(100,1000,100):
img[i:i+10,:] = 0
img[:,i:i+10] = 0
As a side note, your imports are a bit crazy and are messing up your namespace. For what you're doing, you can just do:
import numpy as np
import Image, ImageDraw
# and for a reasonable import of other packages you've listed
from matplotlib import cm
from matplotlib import pyplot as plt
from scipy import ndimage
import cv2
import mahotas
import mambaDraw
For example, you only need one of from numpy import * or import numpy as np, but once you've already imported it, it complicates things to reimport it as some other name.
It seems you cannot convert it to a np.ndarray. Also, you should set the width parameter to a value different than 0:
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
from PIL import Image, ImageDraw
img = Image.open( 'test.tif' ).convert('L')
draw = ImageDraw.Draw(img)
draw.line((0,200, 1000,1000), fill=123., width=4)
plt.imshow(img, cmap=cm.Greys_r)
plt.savefig('test_changed.tif')
I'm trying to display a grayscale TIFF file using Python and MatPlotLib,
So far I have read the file this:
import scipy as N
import gdal
import sys
import matplotlib.pyplot as pyplot
try:
tif = gdal.Open('filename.tif')
tifArray = tif.ReadAsArray()
except:
print 'The file does not exist.'
sys.exit(0)
band1 = tif.GetRasterBand(1)
band2 = tif.GetRasterBand(2)
band3 = tif.GetRasterBand(3)
band1Array = band1.ReadAsArray()
band2Array = band2.ReadAsArray()
band3Array = band3.ReadAsArray()
But then I don't know what else should I do... I'm so clueless.
Anyone would help me in this?
Once you processed your file into a 2 Array, you could use ANY function in matplotlib that plots 2D arrays, e.g. cmap, imshow etc.
Here is the output with the marbles example
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
img=mpimg.imread('MARBLES.TIF ')
imgplot = plt.imshow(img)
Here is what you get if you view only band3 of the image:
imgplot2 = plt.imshow(band3Array)
plt.show()
Look further into image viewing in MPL and 2D array functions...