I am trying to apply random_shear image augmentation from Keras but the images are completely distorted after this.
shape = inputs.shape #(32,512,512,3)
temp = np.empty(shape=(shape[0], shape[1],shape[2], shape[3]))
for i in range(shape[0]):
array_inputs = tf.keras.preprocessing.image.img_to_array(inputs[i])
sheared = tf.keras.preprocessing.image.random_shear(array_inputs, .2,
row_axis=0, col_axis=1,
channel_axis=2)
temp[i]= sheared
return tf.convert_to_tensor(temp)
I am not sure what is wrong here.
Can anybody help me here?
For what is worth, here is the code I tested:
import tensorflow as tf
import numpy as np
from PIL import Image
inputs = [Image.open('./homersimpson.0.0.jpg')]
shape = (1,1400,1400,3)
temp = np.empty(shape=(shape[0], shape[1],shape[2], shape[3]))
for i in range(shape[0]):
array_inputs = tf.keras.preprocessing.image.img_to_array(inputs[i])
sheared = tf.keras.preprocessing.image.random_shear(array_inputs, 50,
row_axis=0, col_axis=1,
channel_axis=2)
temp[i]= sheared
for i in range(shape[0]):
tf.keras.preprocessing.image.array_to_img(temp[i]).show()
Turning this image:
To this:
Related
Is there a way to use bbox_inches='tight' not inside plt.savefig()?
This beacuse I'm converting a Matplotlib figure to a PIL Image without saving it, but I would like to discard all the white space around it.
Thanks in advance.
I'm using this function with this code.
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import io
def fig2img(fig):
buf = io.BytesIO()
fig.savefig(buf)
buf.seek(0)
img = Image.open(buf)
return img
H_plt = plt.figure(dpi=300)
levels = np.linspace(-1, 1, 21)
CS1 = plt.contourf(h_P, levels=levels, cmap=h_cmap)
cb1 = plt.colorbar(CS1)
ticks1 = np.linspace(-1, 1, 11)
labels = [str(int(t1)) for t1 in ticks1]
cb1.set_ticks(ticks1, labels=labels)
plt.axis('scaled')
plt.grid()
new_frame_H = fig2img(H_plt)
I can't understand how to recognize it.
Hope someone can get me out.
import cv2
import numpy as np
import pytesseract
from PIL import Image
image = cv2.imread('b.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Morph open to remove noise and invert image
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
invert = 255 - opening
data = pytesseract.image_to_string(image, lang='eng', config='--psm 10')
print(data)
I have a simple ndarray with shape as:
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(trainImg[0]) #can display a sample image
print(trainImg.shape) : (4750, 128, 128, 3) #shape of the dataset
I intend to apply Gaussian blur to all the images. The for loop I went with:
trainImg_New = np.empty((4750, 128, 128,3))
for idx, img in enumerate(trainImg):
trainImg_New[idx] = cv2.GaussianBlur(img, (5, 5), 0)
I tried to display a sample blurred image as:
plt.imshow(trainImg_New[0]) #view a sample blurred image
but I get an error:
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
It just displays a blank image.
TL;DR:
The error is most likely caused by trainImg_New is float datatype and its value is larger than 1. So, as #Frightera mentioned, try using np.uint8 to convert images' datatype.
I tested the snippets as below:
import numpy as np
import matplotlib.pyplot as plt
import cv2
trainImg_New = np.random.rand(4750, 128, 128,3) # all value is in range [0, 1]
save = np.empty((4750, 128, 128,3))
for idx, img in enumerate(trainImg_New):
save[idx] = cv2.GaussianBlur(img, (5, 5), 0)
plt.imshow(np.float32(save[0]+255)) # Reported error as question
plt.imshow(np.float32(save[0]+10)) # Reported error as question
plt.imshow(np.uint8(save[0]+10)) # Good to go
First of all, cv2.GaussianBlur will not change the range of the arrays' value and the original image arrays's value is legitimate. So I believe the only reason is the datatype of the trainImg_New[0] is not match its range.
So I tested the snippets above, we can see when the datatype of trainImg_New[0] matter the available range of the arrays' value.
I suggest you use tfa.image.gaussian_filter2d from the tensorflow_addons package. I think you'll be able to pass all your images at once.
import tensorflow as tf
from skimage import data
import tensorflow_addons as tfa
import matplotlib.pyplot as plt
image = data.astronaut()
plt.imshow(image)
plt.show()
blurred = tfa.image.gaussian_filter2d(image,
filter_shape=(25, 25),
sigma=3.)
plt.imshow(blurred)
plt.show()
I am working on mnist_fashion data. The images in mnist_data are 28x28 pixel. For the purpose of feeding it to a neural network(multi-layer perceptron), I transformed the data into (784,) shape.
Further, I need to again reshape it back to the original size.
For this, I used below given code:-
from keras.datasets import fashion_mnist
import numpy as np
import matplotlib.pyplot as plt
(train_imgs,train_lbls), (test_imgs, test_lbls) = fashion_mnist.load_data()
plt.imshow(test_imgs[0].reshape(28,28))
no_of_test_imgs = test_imgs.shape[0]
test_imgs_trans = test_imgs.reshape(test_imgs.shape[1]*test_imgs.shape[2], no_of_test_imgs).T
plt.imshow(test_imgs_trans[0].reshape(28,28))
Unfortunately, I am not getting the similar image. I am not able to understand why this is happening.
expected image:
recieved image:
Kindly help me to resolve the problem.
pay attention when you flatten the images in test_imgs_trans
(train_imgs,train_lbls), (test_imgs, test_lbls) = tf.keras.datasets.fashion_mnist.load_data()
plt.imshow(test_imgs[0].reshape(28,28))
no_of_test_imgs = test_imgs.shape[0]
test_imgs_trans = test_imgs.reshape(no_of_test_imgs, test_imgs.shape[1]*test_imgs.shape[2])
plt.imshow(test_imgs_trans[0].reshape(28,28))
I'm working on pedestrian detection with a team. I am trying to figure out an error that keeps showing up that says "TypeError: samples is not a numpy array, neither a scalar" which when appear points to the line of code that is svm.train(X_data, cv2.ml.ROW_SAMPLE, labels12)
i tried following dozens of online guides but i still couldn't solve the problem, and im also very new to this
import cv2
import numpy as np
from skimage import feature
from skimage import exposure
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# training
X_data = []
labels1 = []
label = []
files = glob.glob ("new_pos_1/crop*.PNG")
for myFile in files:
# print(myFile)
image = cv2.imread(myFile,)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
X_data.append (image)
labels1.append('Pedestrian')
print('X_data shape:', np.array(X_data).shape)
labels12 = np.array([labels1])
print('labels12 shape:',np.array(labels12).shape)
print('labels shape:', np.array(labels1).shape)
#Testing
Y_data = []
files = glob.glob ("new_pos_1/person*.PNG")
for myFile in files:
# print(myFile)
image = cv2.imread (myFile)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Y_data.append (image)
label.append('Pedestrian')
print('Y_data shape:', np.array(Y_data).shape)
print('label shape:', np.array(label).shape)
hog_features = []
for image in np.array(X_data):
(fd, hogImage) = feature.hog(image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2),
transform_sqrt=True, block_norm="L2-Hys", visualise=True)
hogImage = exposure.rescale_intensity(hogImage, out_range=(0, 255))
hogImage = hogImage.astype("uint8")
hog_features.append(fd)
print("I'm done hogging")
print(hog_features)
svm = cv2.ml.SVM_create()
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setType(cv2.ml.SVM_C_SVC)
svm.setC(2.67)
svm.setGamma(5.383)
print("Done initializing SVM parameters")
# Train SVM on training data
svm.train(X_data, cv2.ml.ROW_SAMPLE, labels12)
print("Done trainning")
svm.save('svm_data.dat')
print("SAVED.")
#testResponse = svm.predict(testData)[1].ravel()
cv2.waitKey(0)
The line at the beginning that says labels12 = np.array([labels1]) i used to try and fix the error that showed up to no avail.
This is the original website that helped me write this code: https://www.learnopencv.com/handwritten-digits-classification-an-opencv-c-python-tutorial/
you should also do X_data2 = np.array([X_data]) and call svm.train(X_data2, cv2.ml.ROW_SAMPLE, labels12)