# encoding:utf-8
import cv2
image = cv2.imread("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674.png") # 读取图像
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) # 显示像素值
def access_pixels(frame):
print(frame.shape) # shape内包含三个元素:按顺序为高、宽、通道数
height = frame.shape[0]
weight = frame.shape[1]
channels = frame.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) = frame[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)/518400
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 = 720
weight_start_original = 0
weight_end_original = 720
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)
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("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674_new1.png", cropped)
cv2.imwrite("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674_new1.jpg", cropped_original)
for row in range(height): # 遍历高
for col in range(weight): # 遍历宽
for c in range(channels): # 便利通道
(b, g, r) = frame[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))
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("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674_new2.png", cropped_save)
cv2.imwrite("D:\\Learning\\Computer\\check_data\\backups\\Success\\1\\3674_new2.jpg", cropped_save2)
access_pixels(image)