我是数据可视化、机器学习和深度学习主题的新手。我正在努力提高自己,但是当我尝试实现可视化时,我陷入了一个话题。
具体来说,我尝试在“Kaggle”上实现一个内核,我的数据是猫和狗的图像。我有训练和测试数据。问题是我的训练数据有太多不同大小、像素和形状的图片。我想将它们全部制作成 1 种形状。(例如,我想将所有图片制作为 64x64 像素或 128x128 等)
""" I tried different codes to adjust its shapes and create plot:
I tried to reach 2 goals:
1. Convert the dog and cat images from RGB to Grayscale
2. Make all of the images 128x128, or 64x64 """
# One of the codes I've tried
img_size = 128
basewidth = 128
for image in tqdm(os.listdir(train_cat)):
path = os.path.join(train_cat, image)
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
try:
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
except:
pass
np_img=np.asarray(img)
for image2 in tqdm(os.listdir(train_dog)):
path = os.path.join(train_dog, image2)
img2 = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
try:
wpercent = (basewidth / float(img2.size[0]))
hsize = int((float(img2.size[1]) * float(wpercent)))
img2 = img2.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
except:
pass
np_img2=np.asarray(img2)
plt.figure(figsize=(10,10))
plt.subplot(1, 2, 1)
plt.imshow(np_img.reshape(img_size, img_size))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(np_img2.reshape(img_size, img_size))
plt.axis('off')
# ---------------------------------------------------------------------
# Another way:
image_size = 128
for image in tqdm(os.listdir(train_dog)):
path = os.path.join(train_dog, image)
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (image_size, image_size)).flatten()
np_img=np.asarray(img)
for image2 in tqdm(os.listdir(train_cat)):
path = os.path.join(train_cat, image2)
img2 = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img2 = cv2.resize(img2, (image_size, image_size)).flatten()
np_img2=np.asarray(img2)
plt.figure(figsize=(10,10))
plt.subplot(1, 2, 1)
plt.imshow(np_img.reshape(image_size, image_size))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(np_img2.reshape(image_size, image_size))
plt.axis('off')
plt.title("Cat and Dogs in GrayScale")
第一个输出
ValueError Traceback (most recent call last)
<ipython-input-47-0984b7467972> in <module>
28 plt.figure(figsize=(10,10))
29 plt.subplot(1, 2, 1)
---> 30 plt.imshow(np_img.reshape(img_size, img_size))
31 plt.axis('off')
32 plt.subplot(1, 2, 2)
ValueError: cannot reshape array of size 76964 into shape (300,300)
第二个输出
error Traceback (most recent call last)
<ipython-input-67-99d190c6fd41> in <module>
4 path = os.path.join(train_dog, image)
5 img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
----> 6 img = cv2.resize(img, (image_size, image_size)).flatten()
7 np_img=np.asarray(img)
8
error: OpenCV(4.1.0) /io/opencv/modules/imgproc/src/resize.cpp:3718: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
正如我上面提到的,我尝试将 RGB 转换为灰度,并将图像的大小设为 1 类型(可选 128x128)。我相信转换没有错误,但调整大小是一个我无法解决 2-3 天的问题,尽管我做了大量的研究。我不确定,但这可能是一个简单的问题,就像有人说的,我现在是初学者。
注意:我添加了 2 种代码方式,因为其中有我最理解的代码。我希望,你能帮助我,或者教我一种新的方法,提前谢谢你:)
#Appendixes:
Cat and Dog Dataset:
https://www.kaggle.com/tongpython/cat-and-dog
The resizes and grayscale codes I've tried to implement are taken from: (In[5])
Base kaggle I'm saw in order to data that has images (In 2 )
最佳答案
在执行图像处理时,应避免混合使用 OpenCV 和 PIL。原因之一是因为 OpenCV 使用 BGR
格式,而 PIL 使用 RGB
格式。选择一个图书馆并坚持下去。要使用 OpenCV 将图像转换为灰度,您可以使用 cv2.cvtColor()
和 cv2.COLOR_BGR2GRAY
或 cv2.COLOR_RGB2GRAY
标志
image = cv2.imread('image.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
或者在读取图像时只传递一个标志
image = cv2.imread('image.png', 0) # OR
# image = cv2.imread('image.png', cv2.IMREAD_GRAYSCALE)
要调整大小,您可以简单地使用
cv2.resize()
resized = cv2.resize(image, (64, 64)) # OR
# resized = cv2.resize(image, (128, 128))
请注意,
cv2.resize()
不保持纵横比。如果要保持纵横比,请查看 imutils.resize()
。虽然您可能无法获得确切的形状尺寸resized = imutils.resize(image, width=64) # OR
# resized = imutils.resize(image, width=128)
关于python - 使用 Cat 和 Dog 数据的 Python 图像大小调整问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57931520/