tensorflow学习笔记1:图像数据的一些简单操作

        博主学习TensorFlow不久,学习路上也是遇到不少问题。所以决定写一个系列的学习笔记,算是记录下学习历程,方便以后翻阅。当然如果可以帮助到一些新手的话就更好了,高手请绕道。

1.图像数据的采集:

         博主是在从事计算机视觉方面的研究,要知道我们平时往往需要大量的图像数据。网上有很多的数据集而且也都做了比较比较好的标记。但是有的时候还是需要自己来采集一些图像来完成一些特定的任务。比如博主就因为要做道路分割的问题自己采集了一些道路图像。好了下面开始所怎么采集图像。一般我们采集图像是通过拍摄视频的形式来采集图像然后将视频裁剪成图像,当然一般使用手机拍摄就可以了注意拍摄的角度等问题尽量减少晃动。下面贴将视频处理成图像的代码,这样就将图像裁剪成了图像并规范命名好了:

import cv2

vc = cv2.VideoCapture('video.mp4')  # 读入视频文件
c = 1

if vc.isOpened():  # 判断是否正常打开
    rval, frame = vc.read()
else:
    rval = False

timeF = 1000  # 视频帧计数间隔频率,根据需要图像的差异性大小调整

while rval:  # 循环读取视频帧
    rval, frame = vc.read()
    if (c % timeF == 0):  # 每隔timeF帧进行存储操作
        cv2.imwrite('image/' + str(c) + '.jpg', frame)  # 存储为图像
    c = c + 1
    cv2.waitKey(1)
vc.release()

2.图像数据的对称处理

        我们知道有时候采集的图像数据不够怎么办可以采用多种方法将原有数据集中的图像做一些处理来扩充数据集,博主之前就通过将图像做对称处理的方式扩充了数据集:

#-*coding:-UTF-8-*-
import cv2
import numpy as np
import os
import os.path
def files(filedir):
    filenames = []
    for i in os.listdir(filedir):
        filenames.append(i)
    return filenames
def file_head(str1):
    head = str1.strip('.jpg')#如果后缀名不是jpg,改成对应的后缀名就可以了
    return head

file_path="./image_SWJTU/"#读取图像的位置
save_path="./image_SWJTU2/"#存储图像的位置
for filename in files(file_path):
    index = file_head(filename)
    string_file = file_path+filename
    image = cv2.imread(string_file)
    width, height, channels = image.shape
    image1 = np.zeros(image.shape, np.uint8)
    for i in range(height):
        for j in range(width):
            image1[j, height - i - 1] = image[j, i]
    cv2.imwrite(save_path+str(int(index))+".jpg",image1)#str(int(index))
#注意我是存在了两个地方如果将图像也保存在读取图像的地方str(int(index))这个地方要改,不然会覆盖
#原图像

3.图像数据与其标签的读取

          好吧这个没啥好介绍的直接贴代码好了,如果目录结果不同可以做相应的调整:

# -*- coding: UTF-8 -*-

from skimage import io,transform
import os
import glob
import numpy as np
w =256
h =128
c = 1
path='./image/'#在image里面有很多的文件夹,每个文件夹中又存储着一类的图片
def read_img(path):
    cate=[path+x for x in os.listdir(path) if os.path.isdir(path+x)]
    imgs=[]
    labels=[]
    for idx,folder in enumerate(cate):#遍历所有的文件夹
        att=Att[idx]
        for im in glob.glob(folder+'/*.jpg'):#遍历每个文件夹中的图像文件
            #print('reading the images:%s'%(im))
            img=io.imread(im)
            img=transform.resize(img,(w,h,c))
            imgs.append(img)
            labels.append(idx)
    return np.asarray(imgs,np.float32),np.asarray(labels,np.int32),np.asarray(attribute,np.float32),np.asarray(att_scoore,np.float32),count
data,label=read_img(path)

猜你喜欢

转载自blog.csdn.net/qq_35916487/article/details/81185810