Python-OpenCV实现像素点修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | import os import numpy as np import cv2 # 更新后存放的图片地址 dirs = './update_images' # 定义要处理的图片文件包含 image_name_has_label = 'label' image_name_has_output = 'output' # 定义图片处理的函数 def images_update_method(old_file_dir, image_name): # 通过OpenCV读取图片信息 img = cv2.imread(old_file_dir + '/' + image_name) # 获取图片的大小 sp = img.shape w = sp[0] # height(rows) of image h = sp[1] # width(colums) of image color_size = sp[2] # the pixels value is made up of three primary colors【3原色组成】 # print('width: %d \nheight: %d \nnumber: %d' % (w, h, color_size)) # 如果是3原色的,不是不错做 if color_size == 3: # 遍历宽度和高度 for x in range(w): for y in range(h): bgr = img[x, y] # -------------------------------------可以优化的部分:因为都是42的倍数,可以按照倍数优化代码,比如除以42,如果是0,如果是1,如果是2,这种,不用这么多if # 将制定像素点的数据设置为想要的, 要注意的是这三个参数对应的值是Blue, Green, Red。 # img[0, 0] = [0, 0, 0] # RGB(42,42,42)改成(255,255,255) if ((bgr[2], bgr[1], bgr[0]) == (42, 42, 42)): img[x, y] = [255, 255, 255] # RGB(84,84,84)改成(255,255,0) if ((bgr[2], bgr[1], bgr[0]) == (84, 84, 84)): img[x, y] = [255, 255, 0] # RGB(126,126,126)改成(255,0,0) if ((bgr[2], bgr[1], bgr[0]) == (126, 126, 126)): img[x, y] = [255, 0, 0] # RGB(168,168,168)改成(255,0,255) if ((bgr[2], bgr[1], bgr[0]) == (168, 168, 168)): img[x, y] = [255, 0, 255] # RGB(210,210,210)改成(0,255,0) if ((bgr[2], bgr[1], bgr[0]) == (210, 210, 210)): img[x, y] = [0, 255, 0] # RGB(252,252,252)改成(0,255,255) if ((bgr[2], bgr[1], bgr[0]) == (252, 252, 252)): img[x, y] = [0, 255, 255] # 拼接新地址 if old_file_dir.startswith("./"): old_file_dir = old_file_dir.replace("./", "") # 判断更新后存放的图片地址是否存在,如果不存在就创建 if not os.path.exists(dirs + '/' + old_file_dir): os.makedirs(dirs + '/' + old_file_dir) new_image = dirs + '/' + old_file_dir + '/' + image_name print(new_image) # 将图像进行输出,使用show()也是可以显示的。 cv2.imwrite(new_image, img) else: print("图片不是三原色:", image_name) # 定义遍历目录的函数 def file_name_walk(file_dir): for root, dirs, files in os.walk(file_dir): # print("root", root) # 当前目录路径 # print("dirs", dirs) # 当前路径下所有子目录 # 有子目录递归调用 if len(dirs)>0: for child_dir in dirs: # print(file_dir+'/'+child_dir) file_name_walk(file_dir+'/'+child_dir) # print("files", files) # 当前路径下所有非目录子文件 # 遍历所有的文件,获取图片文件 for file_name in files: # print(file_name) # 如果不是图片 if not file_name.endswith(('jpg', 'png', 'jpeg', 'bmp')): continue else: # 是图片,且需要处理的类型 if image_name_has_label in file_name or image_name_has_output in file_name: # print(file_name) # 调用图片转换函数 images_update_method(file_dir, file_name) else: continue # 调用遍历目录的函数:待处理的图片的目录 file_name_walk("./train_record") # 所有操作完成 print("所有都操作完成了") # ========================================================= # 遗憾的部分:没有对规律的RGB做运算,因为有可能真的不是规律的 # 开心的部分:实现待处理的递归调用目录,待处理的目录可以有多级 # ========================================================= |