#!/usr/bin/env python 
# coding=utf-8
#1.编译器声明和2.编码格式声明
#1:为了防止用户没有将python安装在默认的/usr/bin目录，系统会先从env(系统环境变量)里查找python的安装路径，再调用对应路径下的解析器完成操作，也可以指定python3
#2:Python.X 源码文件默认使用utf-8编码，可以正常解析中文，一般而言，都会声明为utf-8编码

import cv2 #引用OpenCV功能包
import numpy as np #引用数组功能包

img_height=400 #设定图片高度
img_width=600  #设定图片宽度

Quit=0 #是否继续运行标志位
#提示停止方法
print ('Press key "Q" to stop.')

while Quit==0:
     keycode=cv2.waitKey(3) #每3ms刷新一次图片，同时读取3ms内键盘的输入
     if(keycode==ord('Q')): #如果按下“Q”键，停止运行标志位置1，调出while循环，程序停止运行
        Quit=1
       
     #创建一个img_height*img_width大小的数组，数组中的每个元素有3个子元素，子元素的数据类型为uint8
     #相当于创建img_height*img_width分辨率的图片，图片为3通道图片。最后+120相当于给所有原始赋值120
     img = np.zeros((img_height, img_width, 3), dtype=np.uint8) + 120

     #对图像第300列到400列的像素点赋值[255,0,0]([B,G,R])
     for col in range(img_width):   
          for row in range(img_height):
              if(col>300 and col<400):
                  img[row, col]=[255,0,0]         
     cv2.imshow("img1", img)

     #对图像第100行到200行的像素点赋值[0,255,0]([B,G,R])
     for col in range(img_width):   
          for row in range(img_height):
              if(row>100 and row<200):
                  img[row, col]=[0,255,0]            
     cv2.imshow("img2", img)
         
     #截取图片,第50行到第300行于第350列到500列的区域
     #img[(height_start:height_end), (width_start:height_end)]
     img_CutOff = img[50:300, 350:500]
     cv2.imshow("img_CutOff", img_CutOff)

     #创建的图片默认是BGR格式，可以转为其它格式后再查看效果
     img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #HSV格式
     cv2.imshow("img_hsv", img_hsv)

     img_lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) #LAB格式
     cv2.imshow("img_lab", img_lab)

print ('Quitted!') #提示程序已停止
cv2.destroyAllWindows() #程序停止前关闭所有窗口
