调节图片亮度,生成xml和图片旋转脚本

本文介绍了一种调整图片亮度的方法,并提供了相应的Python代码实现。此外,还详细讲解了如何从特定格式的文本文件中读取标注信息并转换成XML格式,以及如何根据图片和XML文件生成旋转后的图片和XML。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

调整图片亮度代码

#-*- coding=utf-8
import cv2 as cv
import numpy as np
import os

def contrast_brightness_image(src1, a, g):  
    h, w, ch = src1.shape#获取shape的数值,height和width、通道  

    #新建全零图片数组src2,将height和width,类型设置为原图片的通道类型(色素全为零,输出为全黑图片)  
    src2 = np.zeros([h, w, ch], src1.dtype)  
    dst = cv.addWeighted(src1, a, src2, 1-a, g)#addWeighted函数说明如下  
    return dst
def mk_dir(im_dir):
    if not os.path.exists(im_dir):
        os.mkdir(im_dir)

im_dir='/media/jc/Elements/adajust_pic/JPEGImages_adapth'
out_dir='/media/jc/Elements/add_picture/light_adjust_img'
mk_dir(out_dir)
#mk_dir(os.path.join(im_dir,'rt'))
im_names=os.listdir(im_dir)
for i ,im_name in enumerate (im_names):
    img1=cv.imread(os.path.join(im_dir,im_name))
    cv.imwrite(out_dir+'/'+im_name,img1)
    print i
    for j in [10,20,30,40,50]:
        img2=contrast_brightness_image(img1,1,j)
        cv.imwrite(out_dir+'/'+im_name[:-4]+'_'+str(j)+'.jpg',img2)

生成xml文件代码

将txt文件由
3333bf6f-57cb-4d90-afe1-636dcd88a255.jpg,498_237_173_172;1049_20_18_82
形式变为
3333bf6f-57cb-4d90-afe1-636dcd88a255.jpg 498 237 671 409 1049 20 1067 102
形式,并且还提供在图上画框的副产品。

import csv,cv2,os

def mkdir(im_dir):
    if not os.path.exists(im_dir) :
        os.mkdir(im_dir)

def draw_sign(im,bbox) :
    cv2.rectangle(im,(int(bbox[0]),int(bbox[1])),(int(bbox[0])+int(bbox[2]),int(bbox[1])+int(bbox[3])),(255,0,0),1)
    return im


image_file='./train_b/'
image_save_file='./show_test_b/'
txt=open('./test_b.txt','w')


mkdir(image_save_file)
csv_file=csv.reader(open('./train_b.csv','r'))

bbox=[0,0,0,0]
for i,s in enumerate (csv_file):
    print '----------------------'
    print '---process:%s,%d---'%(s[0],i+1)
    print '----------------------'

    txt.write(s[0])

    #image=cv2.imread(os.path.join(image_file+s[0]))

    for i ,gts in enumerate (s[1].split(';')[:]):
        bbox[0]=gts.split('_')[0].split('.')[0]
        bbox[1]=gts.split('_')[1].split('.')[0]
        bbox[2]=gts.split('_')[2].split('.')[0]
        bbox[3]=gts.split('_')[3].split('.')[0]
        txt.write(' '+bbox[0]+' '+bbox[1]+' '+str(int(bbox[0])+int(bbox[2]))+' '+str(int(bbox[1])+int(bbox[3])))
        #image=draw_sign(image,bbox)
    txt.write('\n')
    #cv2.imwrite(image_save_file+s[0],image)

由上各代码生成xml的代码

from xml.dom.minidom import Document  
import os  
import os.path   

def writeXml(tmp, imgname, w, h, objbud):  
    doc = Document()  
    #owner  
    annotation = doc.createElement('annotation')  
    doc.appendChild(annotation)  
    #owner  
    folder = doc.createElement('folder')  
    annotation.appendChild(folder)  
    folder_txt = doc.createTextNode("VOC2007")  
    folder.appendChild(folder_txt)  

    filename = doc.createElement('filename')  
    annotation.appendChild(filename)  
    filename_txt = doc.createTextNode(imgname)  
    filename.appendChild(filename_txt)  
    #ones#  
    source = doc.createElement('source')  
    annotation.appendChild(source)  

    database = doc.createElement('database')  
    source.appendChild(database)  
    database_txt = doc.createTextNode("My Database")  
    database.appendChild(database_txt)  

    annotation_new = doc.createElement('annotation')  
    source.appendChild(annotation_new)  
    annotation_new_txt = doc.createTextNode("VOC2007")  
    annotation_new.appendChild(annotation_new_txt)  

    image = doc.createElement('image')  
    source.appendChild(image)  
    image_txt = doc.createTextNode("flickr")  
    image.appendChild(image_txt) 
    #owner
    owner = doc.createElement('owner')  
    annotation.appendChild(owner)  

    flickrid = doc.createElement('flickrid')  
    owner.appendChild(flickrid)  
    flickrid_txt = doc.createTextNode("NULL")  
    flickrid.appendChild(flickrid_txt) 

    ow_name = doc.createElement('name')  
    owner.appendChild(ow_name)  
    ow_name_txt = doc.createTextNode("idannel")  
    ow_name.appendChild(ow_name_txt)
    #onee#  
    #twos#  
    size = doc.createElement('size')  
    annotation.appendChild(size)  

    width = doc.createElement('width')  
    size.appendChild(width)  
    width_txt = doc.createTextNode(str(w))  
    width.appendChild(width_txt)  

    height = doc.createElement('height')  
    size.appendChild(height)  
    height_txt = doc.createTextNode(str(h))  
    height.appendChild(height_txt)  

    depth = doc.createElement('depth')  
    size.appendChild(depth)  
    depth_txt = doc.createTextNode("3")  
    depth.appendChild(depth_txt)  
    #twoe#  
    segmented = doc.createElement('segmented')  
    annotation.appendChild(segmented)  
    segmented_txt = doc.createTextNode("0")  
    segmented.appendChild(segmented_txt)  

    for i in range(0,len(objbud)/4):  
        #threes#  
        object_new = doc.createElement("object")  
        annotation.appendChild(object_new)  

        name = doc.createElement('name')  
        object_new.appendChild(name)  
        name_txt = doc.createTextNode('car')  
        name.appendChild(name_txt)  

        pose = doc.createElement('pose')  
        object_new.appendChild(pose)  
        pose_txt = doc.createTextNode("Unspecified")  
        pose.appendChild(pose_txt)  

        truncated = doc.createElement('truncated')  
        object_new.appendChild(truncated)  
        truncated_txt = doc.createTextNode("0")  
        truncated.appendChild(truncated_txt)  

        difficult = doc.createElement('difficult')  
        object_new.appendChild(difficult)  
        difficult_txt = doc.createTextNode("0")  
        difficult.appendChild(difficult_txt)  
        #threes-1#  
        bndbox = doc.createElement('bndbox')  
        object_new.appendChild(bndbox)  

        xmin = doc.createElement('xmin')  
        bndbox.appendChild(xmin)  
        xmin_txt = doc.createTextNode(objbud[i*4])  
        xmin.appendChild(xmin_txt)  

        ymin = doc.createElement('ymin')  
        bndbox.appendChild(ymin)  
        ymin_txt = doc.createTextNode(objbud[i*4+1])  
        ymin.appendChild(ymin_txt)  

        xmax = doc.createElement('xmax')  
        bndbox.appendChild(xmax)  
        xmax_txt = doc.createTextNode(objbud[i*4+2])  
        xmax.appendChild(xmax_txt)  

        ymax = doc.createElement('ymax')  
        bndbox.appendChild(ymax)  
        ymax_txt = doc.createTextNode(objbud[i*4+3])  
        ymax.appendChild(ymax_txt)  
        #threee-1#  
        #threee#  

    tempfile = tmp +"%s.xml"%imgname[:-4]  
    with open(tempfile, 'w') as f:
        f.write(doc.toprettyxml(indent='\t', encoding='utf-8'))
    return  



gt_txt=open('/home/jc/add_picture/test_b.txt','r')
xml_path = '/home/jc/add_picture/xml_test_6000/'

if not os.path.exists(xml_path):  
    os.mkdir(xml_path)  

index=0
lines=gt_txt.readlines()
for line in lines:
    index+=1
    image_name=line.split()[0]
    print image_name,'-------',index
    gts=line.split()[1:]
    print gts
    writeXml(xml_path, image_name, 1069, 500, gts)
    #for i in [10,20,30,40,50]:
    #    writeXml(xml_path, image_name[:-4]+'_'+str(i)+'.jpg', 1069, 500, gts)

根据图片和对应的xml生成旋转之后的图片和xml的代码

# -*- coding: utf-8 -*-
"""
Created on Wed Jun 20 20:25:00 2018

@author: jc
"""

# -*- coding:utf-8 -*-
# !/usr/bin/env python

import os
import cv2
import xml.dom.minidom
from xml.dom.minidom import Document  
import math

#获取路径下所有文件的完整路径,用于读取文件用
def GetFileFromThisRootDir(dir,ext = None):
  allfiles = []
  needExtFilter = (ext != None)
  for root,dirs,files in os.walk(dir):
    for filespath in files:
      filepath = os.path.join(root, filespath)
      extension = os.path.splitext(filepath)[1][1:]
      if needExtFilter and extension in ext:
        allfiles.append(filepath)
      elif not needExtFilter:
        allfiles.append(filepath)
  return allfiles
#图像旋转用,里面的angle是角度制的
def im_rotate(im,angle,center = None,scale = 1.0):
    h,w = im.shape[:2]
    if center is None:
        center = (w/2,h/2)
    M = cv2.getRotationMatrix2D(center,angle,scale)
    im_rot = cv2.warpAffine(im,M,(w,h))
    return im_rot

#读取xml文件,xmlfile参数表示xml的路径 
def readXml(xmlfile):
    DomTree = xml.dom.minidom.parse(xmlfile)  
    annotation = DomTree.documentElement  
    sizelist = annotation.getElementsByTagName('size') #[<DOM Element: filename at 0x381f788>]  
    heights = sizelist[0].getElementsByTagName('height')
    height = int(heights[0].childNodes[0].data)
    widths =sizelist[0].getElementsByTagName('width')
    width = int(widths[0].childNodes[0].data)
    depths = sizelist[0].getElementsByTagName('depth')
    depth = int(depths[0].childNodes[0].data)
    objectlist = annotation.getElementsByTagName('object')        
    bboxes = []
    for objects in objectlist:  
        namelist = objects.getElementsByTagName('name')  
        class_label = namelist[0].childNodes[0].data  
        bndbox = objects.getElementsByTagName('bndbox')[0]     
        x1_list = bndbox.getElementsByTagName('xmin')  
        x1 = int(float(x1_list[0].childNodes[0].data))  
        y1_list = bndbox.getElementsByTagName('ymin') 
        y1 = int(float(y1_list[0].childNodes[0].data)) 
        x2_list = bndbox.getElementsByTagName('xmax')  
        x2 = int(float(x2_list[0].childNodes[0].data))  
        y2_list = bndbox.getElementsByTagName('ymax')  
        y2 = int(float(y2_list[0].childNodes[0].data))
        #这里我box的格式【xmin,ymin,xmax,ymax,classname】
        bbox = [x1,y1,x2,y2,class_label]
        bboxes.append(bbox)
    return bboxes,width,height,depth

#写xml文件,参数中tmp表示路径,imgname是文件名(没有尾缀)ps有尾缀也无所谓
def writeXml(tmp, imgname, w, h, d, bboxes):  
    doc = Document()  
    #owner  
    annotation = doc.createElement('annotation')  
    doc.appendChild(annotation)  
    #owner  
    folder = doc.createElement('folder')  
    annotation.appendChild(folder)  
    folder_txt = doc.createTextNode("VOC2007")  
    folder.appendChild(folder_txt)  

    filename = doc.createElement('filename')  
    annotation.appendChild(filename)  
    filename_txt = doc.createTextNode(imgname)  
    filename.appendChild(filename_txt)  
    #ones#  
    source = doc.createElement('source')  
    annotation.appendChild(source)  

    database = doc.createElement('database')  
    source.appendChild(database)  
    database_txt = doc.createTextNode("My Database")  
    database.appendChild(database_txt)  

    annotation_new = doc.createElement('annotation')  
    source.appendChild(annotation_new)  
    annotation_new_txt = doc.createTextNode("VOC2007")  
    annotation_new.appendChild(annotation_new_txt)  

    image = doc.createElement('image')  
    source.appendChild(image)  
    image_txt = doc.createTextNode("flickr")  
    image.appendChild(image_txt) 
    #owner
    owner = doc.createElement('owner')  
    annotation.appendChild(owner)  

    flickrid = doc.createElement('flickrid')  
    owner.appendChild(flickrid)  
    flickrid_txt = doc.createTextNode("NULL")  
    flickrid.appendChild(flickrid_txt) 

    ow_name = doc.createElement('name')  
    owner.appendChild(ow_name)  
    ow_name_txt = doc.createTextNode("idannel")  
    ow_name.appendChild(ow_name_txt)
    #onee#  
    #twos#  
    size = doc.createElement('size')  
    annotation.appendChild(size)  

    width = doc.createElement('width')  
    size.appendChild(width)  
    width_txt = doc.createTextNode(str(w))  
    width.appendChild(width_txt)  

    height = doc.createElement('height')  
    size.appendChild(height)  
    height_txt = doc.createTextNode(str(h))  
    height.appendChild(height_txt)  

    depth = doc.createElement('depth')  
    size.appendChild(depth)  
    depth_txt = doc.createTextNode(str(d))  
    depth.appendChild(depth_txt)  
    #twoe#  
    segmented = doc.createElement('segmented')  
    annotation.appendChild(segmented)  
    segmented_txt = doc.createTextNode("0")  
    segmented.appendChild(segmented_txt)  

    for bbox in bboxes:
        #threes#  
        object_new = doc.createElement("object")  
        annotation.appendChild(object_new)  

        name = doc.createElement('name')  
        object_new.appendChild(name)  
        name_txt = doc.createTextNode(str(bbox[4]))  
        name.appendChild(name_txt)  

        pose = doc.createElement('pose')  
        object_new.appendChild(pose)  
        pose_txt = doc.createTextNode("Unspecified")  
        pose.appendChild(pose_txt)  

        truncated = doc.createElement('truncated')  
        object_new.appendChild(truncated)  
        truncated_txt = doc.createTextNode("0")  
        truncated.appendChild(truncated_txt)  

        difficult = doc.createElement('difficult')  
        object_new.appendChild(difficult)  
        difficult_txt = doc.createTextNode("0")  
        difficult.appendChild(difficult_txt)  
        #threes-1#  
        bndbox = doc.createElement('bndbox')  
        object_new.appendChild(bndbox)  

        xmin = doc.createElement('xmin')  
        bndbox.appendChild(xmin)  
        xmin_txt = doc.createTextNode(str(bbox[0]))
        xmin.appendChild(xmin_txt)  

        ymin = doc.createElement('ymin')  
        bndbox.appendChild(ymin)  
        ymin_txt = doc.createTextNode(str(bbox[1]))
        ymin.appendChild(ymin_txt)    

        xmax = doc.createElement('xmax')  
        bndbox.appendChild(xmax)  
        xmax_txt = doc.createTextNode(str(bbox[2]))
        xmax.appendChild(xmax_txt)  

        ymax = doc.createElement('ymax')  
        bndbox.appendChild(ymax)  
        ymax_txt = doc.createTextNode(str(bbox[3]))
        ymax.appendChild(ymax_txt)  

    tempfile = tmp +"%s.xml"%imgname  
    with open(tempfile, 'w') as f:
        f.write(doc.toprettyxml(indent='\t', encoding='utf-8'))
    return  

#voc路径
root = '/media/jc/Elements/add_picture/voc_6000/'
img_dir = root + 'light_adjust_img/'
anno_path = root + 'xml_test_6000/' 
#存储新的anno位置
anno_new_path = root + 'NewAnnotations/' 
if not os.path.isdir(anno_new_path):
    os.makedirs(anno_new_path)
#读取原图全路径  
imgs_path=GetFileFromThisRootDir(img_dir)
#存储旋转后图片位置
pro_dir = root+'train_translate_scale_rotate/'
if not os.path.isdir(pro_dir):
    os.makedirs(pro_dir)
#旋转角的大小,整数表示逆时针旋转
angles = [2,358,359]#角度im_rotate用到的是角度制
angle_rad = [angle*math.pi/180.0 for angle in angles] #cos三角函数里要用到弧度制的    
j=0 # 计数用
angle_num = len(angles)
for img_path in imgs_path :
    #读取原图像
    im = cv2.imread(img_path)

    for i in range(angle_num):
        gt_new = []


        im_rot = im_rotate(im,angles[i])
        file_name = img_path.split('/')[-1][:-4]
        #画出旋转后图像
        cv2.imwrite(os.path.join(pro_dir,'P%s_%s.jpg'%(angles[i],file_name)),im_rot)
        anno = os.path.join(anno_path,'%s.xml'%file_name)
        #读取anno标签数据
        [gts,w,h,d] =readXml(anno) 
        #计算旋转后gt框四点的坐标变换
        [xc,yc] = [float(w)/2,float(h)/2]
        for gt in gts:
            #计算左上角点的变换
            x1 = (gt[0]-xc)*math.cos(angle_rad[i]) - (yc-gt[1])*math.sin(angle_rad[i]) + xc
            if x1<0 : x1=0
            if x1>w-1 : x1=w-1                        
            y1 = yc - (gt[0]-xc)*math.sin(angle_rad[i]) - (yc-gt[1])*math.cos(angle_rad[i]) 
            if y1<0 : y1=0         
            if y1>h-1 : y1=h-1 
            #计算右上角点的变换   
            x2 = (gt[2]-xc)*math.cos(angle_rad[i]) - (yc-gt[1])*math.sin(angle_rad[i]) + xc
            if x2<0 : x2=0
            if x2>w-1 : x2=w-1                        
            y2 = yc - (gt[2]-xc)*math.sin(angle_rad[i]) - (yc-gt[1])*math.cos(angle_rad[i])
            if y2<0 : y2=0         
            if y2>h-1 : y2=h-1 
            #计算左下角点的变换
            x3 = (gt[0]-xc)*math.cos(angle_rad[i]) - (yc-gt[3])*math.sin(angle_rad[i]) + xc
            if x3<0 : x3=0
            if x3>w-1 : x3=w-1                        
            y3 = yc - (gt[0]-xc)*math.sin(angle_rad[i]) - (yc-gt[3])*math.cos(angle_rad[i])
            if y3<0 : y3=0         
            if y3>h-1 : y3=h-1
            #计算右下角点的变换
            x4 = (gt[2]-xc)*math.cos(angle_rad[i]) - (yc-gt[3])*math.sin(angle_rad[i]) + xc
            if x4<0 : x4=0          
            if x4>w-1 : x4=w-1
            y4 = yc - (gt[2]-xc)*math.sin(angle_rad[i]) - (yc-gt[3])*math.cos(angle_rad[i])
            if y4<0 :y4=0      
            if y4>h-1 : y4=h-1
            xmin = min(x1,x2,x3,x4)
            xmax = max(x1,x2,x3,x4)
            ymin = min(y1,y2,y3,y4)
            ymax = max(y1,y2,y3,y4)
            #把因为旋转导致的特别小的 长线型的去掉
            w_new = xmax-xmin+1
            h_new = ymax-ymin+1
            ratio1 = float(w_new)/h_new
            ratio2 = float(h_new)/w_new
            if(1.0/6.0<ratio1<6 and 1.0/6.0<ratio2<6 and w_new>9 and h_new>9):
                classname = str(gt[4])
                gt_new.append([xmin,ymin,xmax,ymax,classname])
            #写出新的xml
            writeXml(anno_new_path,'P%s_%s'%(angles[i],file_name) , w, h, d, gt_new)
    j = j+1
    if j%100==0 : print '----%s----'%j


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值