homework 1

topic:

Python program design, install and import opencv library:
For example: the install opencv Conda
Import CV2, installed as a result, on behalf of the installation was successful:
Here Insert Picture Description
then cv2.imread () reads a color image is arbitrary numpy matrix, then the following actions:
( 1) the three-channel image is sequentially changed, the changed RGB BRG, and washed with imshow () function or matplotlib display pictures in the relevant

  • Cv2.imread channel function will read BGR image sequence into an array by np.array (), the following code:
# 读入图像
img = cv2.imread('rgb.jpg', 1)

# 转化数组
imgArr = np.array(img)
cv2.imshow('cv2-imgArr',imgArr)
  • Use cv2.imshow shown below:
    Here Insert Picture Description
  • cv2.split function separated B, G, R is a single-channel image, the parameters are output from the array in layer 0, layer 1, layer 2, respectively, is stored in blue 0-Blue, 1-Green , 2-Red gradation values ​​of three colors.
  • Characterized monochromatic gradation value just light or dark.
    Each matrix layer (400 300 1/2/3) respectively corresponding to the gradation value R / G / B, the matrix here is merely indicate corresponding monochromatic gray scale value, not a color image.
  • Now read Red, blue, green gradation value corresponding to each function stored as cv2.split by: r, b, g, the separated gradation value, respectively, other colors layer filled with 0 values, according to the order of separation The combined, as follows:
# 提取BGR数组
(b, g, r) = cv2.split(imgArr)

zero = np.zeros(imgArr.shape[:2], dtype = "uint8")
bl=cv2.merge([b,zero,zero])
gr=cv2.merge([zero,g,zero])
re=cv2.merge([zero,zero,r])
cv2.imshow("blue",bl)
cv2.imshow("red",re)
cv2.imshow("green",gr)
cv2.waitKey(0)
  • Are shown below:
    Here Insert Picture Description
  • Experiments aimed at changing the color channels of the image: BRG changes from RGB, that is, the gradation value of the original channel becomes Red Blue channel gray value, the gray gradation values ​​becomes Green channel Red gray scale value, becomes Green channel Blue gradation value, as follows:
imgChange = cv2.merge([g,r,b]) #RGB->GBR  应该是BRG,但是显示结果为GBR
#imgChange = cv2.merge([r,b,g]) # ok RGB -> BRG   显示结果为BRG。?

cv2.imshow("imgChange-cv2merge", imgChange)
cv2.imwrite('imgChange-cv2.jpg', imgChange)
cv2.waitKey(0)
  • cv.2 when displaying an image, the order of the channel used for the BGR. Then new merged order, g, r, b, the results achieved in this experiment are as follows:
    Here Insert Picture Description
    can be found, the original red becomes green, green to blue, blue to red.
    But I look forward to the result, red to blue, green to red, blue to green, to achieve this result, the merger of the order when the channel is r, b, g, when combined, rbg order the results obtained were as follows:
    Here Insert Picture Description

(2) to change the image using Numpy channel order in the specified position marked with red box, wherein the top left and bottom right coordinates of the red frame mode is defined as: assuming student number is 12069028, then the upper-left corner coordinates (12, 06), the lower right corner coordinates (12 + 90, 06 + 28). (Do not use opencv frame comes with tools)

  • I school No. 18023022, residues because I was left corner (18,02), the bottom right coordinates (48,02 + 18 + 30 = 22 = 24), the corresponding row of the array 18 to the image line 48, to column 2 of 24, I first gray value matrix to maximum position the red channel, the green and the gray and blue channels to the minimum value, the frame code is as follows:
(blue,gre,red)=cv2.split(imgChange) # 从交换通道后的图像数组中分离出新通道

red[18:48,2]=255
red[18:48,24]=255
red[18,2:24]=255
red[48,2:24]=255

blue[18:48,2]=0
blue[18:48,24]=0
blue[18,2:24]=0
blue[48,2:24]=0


gre[18:48,2]=0
gre[18:48,24]=0
gre[18,2:24]=0
gre[48,2:24]=0

  • Frame results are as follows:
    Here Insert Picture Description

(3) the use of cv2.imwrite () function plus the red box to save the picture.

  • Save the code as follows:
cv2.imshow("redRect-imgChange",imgRect)
cv2.imwrite('redRect-imgChange.jpg', imgRect)
cv2.waitKey(0)
  • Save results are as follows:
    Here Insert Picture Description

Guess you like

Origin blog.csdn.net/yinxian9019/article/details/89514960