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 get the background of a noisy image based on this code https://www.kaggle.com/rdokov/background-removal
But, I'm getting ValueError. How to resolve this?
import glob
import cv2
from scipy import signal
import numpy as np
a = cv2.imread(glob.glob('lp_train/*.jpg')[0])
import matplotlib.pyplot as plt
a2 = np.asarray(a)/255.
aa = signal.medfilt2d(a2, 11)
plt.imshow(aa)
plt.show()
Error: ValueError: object too deep for desired array
.jpg has 3 channels, so you are sending 3-dimensional array but medfilt2d accepts 2d array.
Two ways you can solve this.
Just read as a grayscale image
import glob
import cv2
from scipy import signal
import numpy as np
a = cv2.imread(glob.glob('lp_train/*.jpg')[0], 0) # grayscale, single channel
import matplotlib.pyplot as plt
a2 = np.asarray(a)/255.
aa = signal.medfilt2d(a2, 11)
plt.imshow(aa)
plt.show()
Take a specific channel (R/G/B)
import glob
import cv2
from scipy import signal
import numpy as np
a = cv2.imread(glob.glob('lp_train/*.jpg')[0])
import matplotlib.pyplot as plt
a2 = np.asarray(a)/255.
aa = signal.medfilt2d(a2[:,:,0], 11) # a specific channel
plt.imshow(aa)
plt.show()
Both should work.
Python: How to change a "penny" image color (copper) to different gray levels? Example given in image
import numpy as np
import scipy.io as sio
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
plt.clf()
p = plt.imread ('penny.jpg')
plt.imshow(p)
penny = p.copy()
The conversion to grayscale code comes from https://stackoverflow.com/a/12201744/1092820
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Import the image
p = plt.imread('penny.png')
# Convert to grayscale
gray = np.dot(p[...,:3],[0.299, 0.587, 0.114])
# Round gray to nearest 1/n, where n is how many grays:
grayCount = 4
roundedGray = np.floor(gray * float(grayCount)) / float(grayCount)
# Display using matplotlib's copper color mapping
plt.imshow(roundedGray, cmap=plt.get_cmap('copper'))
plt.show()
So I am trying to convert RGB TIF file to grayscale. This is the Python code
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import tifffile as tfl
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.587, 0.114])
imName = 'three_band/6120_2_0.tif'
im_rgb=tfl.imread(imName).transpose([1,2,0])
gray = rgb2gray(im_rgb)
plt.imshow(gray)
But when I try to view it using pyplot, I get a blue image like this
Can anyone point to what's wrong with my code?
Thanks.
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')