opencv-python图像预处理后处理记录

0. 随机调50个文件放入另外一个文件夹

import os, random, shutil


def copyFile(fileDir,tarDir):
    pathDir = os.listdir(fileDir)
    sample = random.sample(pathDir, 50)
    print(sample)
    for name in sample:
        shutil.copy(fileDir+name, tarDir+name)

fileDir = "img/"
tarDir = "test/good/"
copyFile(fileDir, tarDir)

0.生成掩膜mask的ground_truth

# -*- coding: utf-8 -*-
# @Time    : 2020/12/29 上午11:47
# @Author  : yangninghua
# @File    : main.py


import os
import cv2
import json
import numpy as np
path = "mydata"
save_path = "ground_truth_new"
lines = os.listdir(path)

end_img = ".jpg"
for line in lines:
    if line.endswith(end_img):
        img = cv2.imread(os.path.join(path, line), -1)
        jsonfile = line.replace(end_img, ".json")

        H,W = img.shape[:2]
        im = np.zeros((H,W), dtype="uint8")
        with open(os.path.join(path, jsonfile), 'r') as load_f:
            load_dict = json.load(load_f)
            for m_key, m_value in enumerate(load_dict["shapes"]):
                points = np.array(m_value["points"])
                cv2.polylines(im, np.int32([points]), 1, 255)
                cv2.fillPoly(im, np.int32([points]), 255)
                mask_array = im
        cv2.imwrite(os.path.join(save_path, line), mask_array)

 

0.找不到python模块或者同文件夹下的模块添加如下解决

import sys
sys.path.append('/home/***/service/')

0.txt读取

id_list = []
with open("id.txt", "r") as f:
    for data in f.readlines():
        data = data.strip("\n")
        id_list.append(data)
        print(data)

0.通过载入json生成mask

mydata = os.listdir(path)
for key in mydata:
    if key.endswith(".json"):
        name = key.split(".json")[0]
        save_name = name + "_mask.png"
        with open(path+key, 'r', encoding='utf8') as fp:
            json_data = json.load(fp)
            points = json_data["shapes"][0]["points"]
            mask_array = np.zeros((1544, 2064), dtype="uint8")
            #cv2.polylines(img=mask_array, pts=np.int32([points]), isClosed=True, color=255, thickness=1, lineType=None, shift=0)
            #cv2.fillPoly(img=mask_array, pts=np.int32([points]), color=255, lineType=None, shift=None, offset=None)
            cv2.polylines(img=mask_array, pts=np.int32([points]), isClosed=True, color=255, thickness=1)
            cv2.fillPoly(img=mask_array, pts=np.int32([points]), color=255)
            cv2.imwrite(path+save_name, mask_array)

 

0. 保存可视化图像没必要太大

cv2.imwrite(os.path.join(argv.output_file, img['file_name']), cvImage,  [int(cv2.IMWRITE_JPEG_QUALITY), 30])

0.python-cpp扩展

主要有以下几个文件构成,其他为debug的temp文件
conversion.h
conversion.cpp
zd3dparse.h
libfile.so
LoadData.cpp
setup.py
mian_test_ZD.py
readme.txt


1直接安装,删除需要手动去python路径(lib/python3.6/site-packages)下
python setup.py install

2产生日志安装,方便删除:
python setup.py build
python setup.py install --record python_setup_log.txt
删除:
cat python_setup_log.txt | xargs rm -rf

容易产生问题:
ImportError: libfile.so cannot open shared object file: No such file or directory
解决方案:
sudo vim /etc/ld.so.conf
include /home/boyun/CLionProjects/myproject
#该动态库文件存在于此路径下(/home/boyun/CLionProjects/myproject/libfile.so)
sudo /sbin/ldconfig -v

1.

        image_array = cv2.imread(img_name, -1)
        if image_array.ndim == 2:
            image_array = cv2.cvtColor(image_array, cv2.COLOR_GRAY2BGR)
        image = Image.fromarray(image_array.astype('uint8')).convert('RGB')

空数组 最重要的还是shape=(0,5),其实ones也可以

temp = np.empty(shape=(0,5), dtype=np.float32)

关于空数组的使用,参考某一段代码:

出自github项目hpatches-benchmark

from utils.hpatch import *
import cv2
import os.path


# all types of patches
tps = ['ref','e1','e3','e5','h1','h3','h5','t1','t3','t5']
datadir = '/home/boyun/deepglint/ImageMatch_dataset/data'
#datadir = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "data"))

def vis_patches(seq,tp,ids):
    """Visualises a set of types and indices for a sequence"""
    h = len(tp)*65
    vis = np.empty((h, 0))
    # add the first column with the patch type names
    vis_tmp = np.empty((0,55))
    for t in tp:
        tp_patch = 255*np.ones((65,55))
        cv2.putText(tp_patch,t,(5,25),cv2.FONT_HERSHEY_DUPLEX , 1,0,1)
        vis_tmp = np.vstack((vis_tmp,tp_patch))
    vis = np.hstack((vis,vis_tmp))
    # add the actual patches
    for idx in ids:
        vis_tmp = np.empty((0,65))
        for t in tp:
            vis_tmp = np.vstack((vis_tmp,get_patch(seq,t,idx)))
        vis = np.hstack((vis,vis_tmp))
    return vis


# select a subset of types of patches to visualise
# tp = ['ref','e5','h5','t5']
#or visualise all - tps holds all possible types
tp = tps

# list of patch indices to visualise
ids = range(1,55)

# load a sample sequence
seq = hpatch_sequence(os.path.join(datadir, "hpatches-release", "v_calder"))
vis = vis_patches(seq,tp,ids)

# show
cv2.imshow("HPatches example", vis/255)
cv2.waitKey(0)

# or save
cv2.imwrite("patches.png", vis)

 

2.

import os
import shutil
import numpy as np
import pcl
import cv2

def searchDirFile(rootDir, list_, path_):
    for dir_or_file in os.listdir(rootDir):
        filePath = os.path.join(rootDir, dir_or_file)
        if os.path.isfile(filePath):
            if os.path.basename(filePath).endswith('_DepthMap.tif'):
                temp = filePath.split('/')[-2]
                list_.append(temp)
                path_.append(filePath)
                #print(temp)
            else:
                continue
        elif os.path.isdir(filePath):
            searchDirFile(filePath, list_, path_)
        else:
            print('not file and dir '+os.path.basename(filePath))

path = "/home/dataset/"
namelist = []
pathlist = []
searchDirFile(path, namelist, pathlist)

or

import os
import cv2
import numpy as np

def searchDirFile(rootDir, list_, path_, endswith_):
    for dir_or_file in os.listdir(rootDir):
        filePath = os.path.join(rootDir, dir_or_file)
        if os.path.isfile(filePath):
            if os.path.basename(filePath).endswith(endswith_):
                temp = filePath.split('/')[-2]
                list_.append(temp)
                path_.append(filePath)
            else:
                continue
        elif os.path.isdir(filePath):
            searchDirFile(filePath, list_, path_, endswith_)
        else:
            print('not file and dir '+os.path.basename(filePath))

path = "/home/dataset"
namelist = []
pathlist = []
endswith = 'Texture_8Bit.png'
searchDirFile(path, namelist, pathlist, endswith)

 

3

# -- coding: utf-8 --
import os
import sys
import shutil
import numpy as np
from pathlib2 import Path

if __name__ == '__main__':

    lines = '**********'

    out_dir = './select_data'
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    # 数据地址
    before_dir = os.path.abspath(os.path.join(os.getcwd(), ".."))

    save_index = 200
    num = 0
    for dir_or_file in os.listdir(before_dir):
        num += 1
        print(num, "/", len(os.listdir(before_dir)))

        filePath = os.path.join(before_dir, dir_or_file)
        # 判断是否为文件
        if os.path.isfile(filePath):
            continue
        # 判断是否为目录
        elif os.path.isdir(filePath):
            sub_filePath1 = os.path.join(filePath, "Disk1")
            sub_filePath2 = os.path.join(filePath, "Disk2")
            if Path(sub_filePath1).exists() and Path(sub_filePath2).exists():
                for device_id_path in [sub_filePath1, sub_filePath2]:
                    for file in os.listdir(device_id_path):
                        m_id = file.split('-')
                        sub_id = "{}-{}-{}-{}".format(m_id[1],m_id[2],m_id[3],m_id[4])
                        id =  "{}-{}-{}-{}-{}".format(m_id[0],m_id[1],m_id[2],m_id[3],m_id[4])
                        if sub_id == lines:
                            subsubFilePath = os.path.join(device_id_path, file)

                            currentPng = os.path.join(subsubFilePath, 'OtherSampleFrame_IMG_Texture_8Bit.png')
                            currentTif = os.path.join(subsubFilePath, 'OtherSampleFrame_IMG_DepthMap.tif')
                            if Path(currentPng).exists() and Path(currentTif).exists():

                                shutil.copy(currentPng, os.path.join(out_dir, id+"_{:0>5d}_".format(save_index)+"OtherSampleFrame_IMG_Texture_8Bit.png"))
                                save_index += 1
                                #print(save_index)

4

python float 精度问题 和opencv-cpp有出入

# python的round函数将0.5向上入,OpenCV的cvRound函数将0.5向下舍
def cvRound(value):
    fractpart, intpart = math.modf(value)
    flag1 = (math.fabs(fractpart) < 0.50)
    if flag1:
        return int(intpart)
    else:
        return int(value + (0.5 if value >= 0 else -0.5))

def cut(num, c):
    str_num = str(num)
    return float(str_num[:str_num.index('.') + 1 + c])

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用OpenCV进行灰度图像二值化时,可以使用以下函数进行操作: ```python import cv2 # 读取图像 img = cv2.imread('image.jpg', 0) # 以灰度模式读取图像 # 应用全局阈值二值化 _, img_bin = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # 显示二值化后的图像 cv2.imshow('Binary Image', img_bin) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在上述代码中,`cv2.threshold()`函数用于将灰度图像进行二值化处理。其中,`img`是输入的灰度图像,`127`是设定的阈值,`255`是二值化后的最大值,`cv2.THRESH_BINARY`表示使用二进制阈值化方法。函数返回两个值,第一个值是阈值,第二个值是二值化后的图像。最后,使用`cv2.imshow()`函数显示二值化后的图像。 请注意,上述代码中的`image.jpg`是示例图像的文件名,你需要根据实际情况修改为你要处理的图像文件名。 #### 引用[.reference_title] - *1* [OpenCV python(四)图像预处理:二值化 && 滤波操作](https://blog.csdn.net/xztli/article/details/126175975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Python - OpenCV 图像二值化处理](https://blog.csdn.net/bugang4663/article/details/109589177)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Python-opencv 灰度直方图+图像二值化](https://blog.csdn.net/weixin_43909400/article/details/123378690)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值