How to get rid off the blue shade? - python

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

Related

Python window-objects run order

To understand the Pyton Interpreter,
MY QUESTION :
If i select the first 9 line and RUN Selected, the plt codes work…
But If i RUN all the codes, plts codes doesnt work, jump to second img.show() section…
WHY?
and
How all codes work sequential…
CODE :
from PIL import Image
import numpy as np
import matplotlib.image as gg
import matplotlib.pyplot as plt
img2 = gg.imread("C:/Users/John/Desktop/BOUTTEQA/data/1.jpg")
# print(type(img2))
# print(img2.shape)
plt.imshow(img2)
plt.colorbar()
img = Image.open("C:/Users/John/Desktop/BOUTTEQA/data/1.jpg")
print(type(img))
img.show()
print(img.format)
img1 = np.asarray(img)

How to increase the width/size of markers line in watershed?

import cv2
import numpy as np
from matplotlib import pyplot as plt
from scipy import ndimage
from skimage import measure, color, io
from skimage.restoration import denoise_nl_means, estimate_sigma
from skimage.filters import roberts, sobel, scharr, prewitt
from tqdm import tqdm
import os
import glob
from skimage.io import imread, imshow
from skimage import exposure
sure_bg = cv2.dilate(invert,kernel,iterations=2)
#plt.imshow(sure_bg)
dist_transform = cv2.distanceTransform(invert,cv2.DIST_L2,3)
ret2, sure_fg = cv2.threshold(dist_transform,0.2*dist_transform.max(),255,0)
sure_fg = np.uint8(sure_fg)
#plt.imshow(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)
#plt.imshow(unknown)
ret3, markers = cv2.connectedComponents(sure_fg)
markers = markers+10
markers[unknown==255] = 0
markers = cv2.watershed(img1,markers)
org_img[markers == -1] = [255,69,0]
img2 = color.label2rgb(markers, bg_label=0)
I use the function watershed from the module sci-kit image, because it has the parameter watershed_line. You can dilate the watershed lines after you detect the lines.
from skimage.segmentation import watershed
from skimage.morphology import dilation
from skimage.morphology import square
import numpy as np
markers = watershed(-dist_transform, markers, mask=sure_fg , watershed_line=True)
watershed_lines = np.zeros(shape=np.shape(markers))
watershed_lines(markers==0)=1 # ws lines are labeled as 0 in markers
watershed_lines_thick = dilation(bright_pixel, square(3))
There might be some typos since I did not check any results. But hope you get the idea. Let me know if it helps!

Converting image into pixel array

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)

Python: How to change a "penny" image color (copper) to different gray levels?

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()

How can I convert the blue color of water into another even if there are different shades of blue present?

How can I convert the blue color of of the water in an image into white, especially if there are different shades of blue. And then separate any other color and give it for instance black. An example would be this pollution in the sea and you want to detect the contaminated area and give it a color clearly distinguishable from the water:
import cv2
import numpy as np
from matplotlib import pyplot as plt
BLUE = [255,0,0]
img1 = cv2.imread('11.jpg')
replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP)
constant=
cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()
cv2.waitKey()
cv2.destroyAllWindows()
import cv2
import numpy as np
from PIL import Image
import colorsys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
img = mpimg.imread('0.jpg')
gray = rgb2gray(img)
plt.imshow(gray, cmap = plt.get_cmap('gray'))
plt.show()
cv2.waitKey()
cv2.destroyAllWindows()

Categories

Resources