栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python-图像处理-{二值化,矩形小图分割}

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python-图像处理-{二值化,矩形小图分割}

二值化
from PIL import Image

## use the "open" function of PIL class
img=Image.open("C:\Users\LENOVO\Desktop\exp9\01-02-00015.jpg")

## im.convert(mode)⇒ image 
## turn current image into other mode and return a new image
## "convert" function have 9 mode =>1,L,P,RGB,RGBA,CMYK,YCbCr,I,F
## L mode is pray mode
Img=img.convert("L")
Img.save("C:\Users\LENOVO\Desktop\exp9\text1.jpg")

threshold=200

## user-define pray degree
table=[]
for i in range(256):
    if i 
基于labelimg坐标的小图分割 
import os
import cv2
label_path="C:\Users\LENOVO\Desktop\exp9\z_exp\label"
image_path="C:\Users\LENOVO\Desktop\exp9\z_exp\image"

image=cv2.imread(image_path+"\19-03-00341.jpg")
print(image.shape)## (2048, 2048, 3)

lines=[];len=0;w=h=2048
## file read
file_object = open(label_path+"\19-03-00341.txt",'r+')
try: 
    for line in file_object:
        lines.append([line])
        len=len+1
finally:
    file_object.close()

for i in range(len):
    lines[i]=lines[i][0].split(" ")

print(lines)
## line [0 ,x_center ,y_center ,width ,height ]
for i in range(len):
    tl=float(lines[i][1])*w-float(lines[i][3])*w/2
    tr=float(lines[i][1])*w+float(lines[i][3])*w/2
    bl=float(lines[i][2])*h-float(lines[i][4].strip())*h/2
    br=float(lines[i][2])*h+float(lines[i][4].strip())*h/2
    crop=image[int(bl):int(br),int(tl):int(tr)]## slice indices must be integers or None or have an __index__ method
    cv2.imwrite("C:/Users/LENOVO/Desktop/exp9/z_exp/imgsave/" + str(i) + ".jpg", crop)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/331116.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号