Python(一)[OpenCV實現像素點修改]

Python-OpenCV實現像素點修改

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做運算,因爲有可能真的不是規律的
# 開心的部分:實現待處理的遞歸調用目錄,待處理的目錄可以有多級
# =========================================================
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章