上一篇的改进版:加上了读取文件夹中所有图片,循环读取,并判断是否需要放大。
找到图像很小的图,并把图像主体部分放大
读取图片像素,找到主体部分像素,统计该部分像素比例,判断比例是否合适,对图片进行分割,再恢复原大小并保存。
一个小的工具吧…有点冗余,再接再厉~
# encoding:utf-8
import cv2
import os.path
rootdir = "D:\\Learning\\Computer\\check_data\\backups\\Success\\1"
def access_pixels():
for dirpath, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
file = os.path.splitext(filename)[0]
# 文件扩展
filetype = os.path.splitext(filename)[1]
if filetype == ".png" :
image = cv2.imread(os.path.join(rootdir,filename)) # 读取图像,只读groundtruth
# image_original = cv2.imread("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674.jpg") # 读取图像
(b, g, r) = image[200, 20] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
print "位置(0,0)处的像素 - 红:%d,绿:%d,蓝:%d" % (r, g, b) # 显示像素值
print(image.shape) # shape内包含三个元素:按顺序为高、宽、通道数
height = image.shape[0]
weight = image.shape[1]
channels = image.shape[2]
print("weight : %s, height : %s, channel : %s" % (weight, height, channels))
white_count = 0
for row in range(height): # 遍历高
for col in range(weight): # 遍历宽
for c in range(channels): # 遍历通道
(b, g, r) = image[row, col] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
if r==255: #白色部分
white_count=white_count+1
# pv = frame[row, col, c]
# frame[row, col, c] = 255 - pv # 全部像素取反,实现一个反向效果
print("white point is : %s" % (white_count))
size = float(white_count)/(height*weight)
print("size is : %s" % (size))
#cv2.imshow("fanxiang", frame)
#cv2.waitKey(0)
cropped = image
#cropped_original = image_original
height_start_original = 0
height_end_original = height
weight_start_original = 0
weight_end_original = weight
if size < 0.03 :
image_original = cv2.imread(os.path.join(rootdir,file+".jpg"))
cropped_original = image_original
while size < 0.03:
height_start = (height_end_original-height_start_original)/6
height_end = (height_end_original-height_start_original)*5/6
weight_start = (weight_end_original-weight_start_original) / 6
weight_end = (weight_end_original-weight_start_original)* 5 / 6
height_start_original = height_start
height_end_original = height_end
weight_start_original = weight_start
weight_end_original = weight_end
print("height_start: %s" % height_start)
print("height_end: %s" % height_end)
print("weight_start: %s" % weight_start)
print("weight_end: %s" % weight_end)
height_new = height_end-height_start
weight_new = weight_end-weight_start
cropped = cropped[height_start:height_end, weight_start:weight_end] # 裁剪坐标为[y0:y1, x0:x1]
cropped_original = cropped_original[height_start:height_end, weight_start:weight_end]
cv2.imwrite(os.path.join(rootdir,filename), cropped)
cv2.imwrite(os.path.join(rootdir,file+".jpg"), cropped_original)
for row in range(height_new): # 遍历高
for col in range(weight_new): # 遍历宽
for c in range(channels): # 便利通道
(b, g, r) = cropped[row, col] # 读取(0,0)像素,Python中图像像素是按B,G,R顺序存储的
if r == 255: # 白色部分
white_count = white_count + 1
print("white point is : %s" % (white_count))
size = float(white_count) / (height*weight)
print("size is : %s" % (size))
cropped_changesize = cropped
cropped_save = cv2.resize(cropped_changesize,(720, 720))
cropped_changesize2 = cropped_original
cropped_save2 = cv2.resize(cropped_changesize2, (720, 720))
cv2.imwrite(os.path.join(rootdir,filename), cropped_save)
cv2.imwrite(os.path.join(rootdir,file+".jpg"), cropped_save2)
access_pixels()