python+opencv第6節画像の基本操作

3814 ワード

# 1.        
import cv2
import numpy as np

img = cv2.imread('salary.jpg')
#       [100, 100]    
px = img[100, 100] #    [B, G, R]
print(type(px))
print(px)
blue = img[100, 100, 2]#      [100, 100]  R(    2)  ,(B  0, G  1)
print(type(blue))
print(blue)

#     
#img[100, 100] = [255, 255, 255] #     
#img[100] = [255, 255, 255] #                   
#img[:, 100] = [255, 255, 255] #              ,         ( )   ,   ( )  100  
#img[:] = [255, 255, 255] #      
print(type(img))


#       2:    Numpy   array.item
# print(type(img.item(10, 10, 2)))
# print(img[10, 10])
# print(img.item(10, 10, 0))
# img.itemset((10,10,2),100)
# print(img.item(10,10,2))

# 2.      
#  ,  ,   ,         
print(img.ndim) #          3
print(img.shape) #      , ,         : (640, 1024, 3)
print(img.dtype) #             : uint8

# 3.   ROI      

# roi_1 = img[100:300, 500:550] #          100:300  x 200   ,500:550 y 50   
# img[300:500, 700:750] = roi_1 #  ROI       ,    x  y             

# 4.      
# def copyMakeBorder(src, top, bottom, left, right, borderType, dst=None, value=None)
# src :   
# top, bottom, left, right :                
# borderType :           
#         BORDER_REPLICATE  #       ,          aaaaaa|abcdefgh|hhhhhhh
#         BORDER_REFLECT    #    fedcba|abcdefgh|hgfedcb
#         BORDER_REFLECT_101 #    ,        ,   gfedcb|abcdefgh|gfedcba                   ,           ,           
#         BORDER_WRAP       #               ,            cdefgh|abcdefgh|abcdefg
#         BORDER_CONSTANT   #                   value
# value     ,         cv2.BORDER_CONSTANT,         

# replicate_ = cv2.copyMakeBorder(img, 100, 100, 100, 100, cv2.BORDER_REPLICATE)
# cv2.namedWindow('replicate')
# cv2.imshow('replicate', replicate_)

# reflect_ = cv2.copyMakeBorder(img, 100, 100, 100, 100, cv2.BORDER_REFLECT)
# cv2.namedWindow('reflect')
# cv2.imshow('reflect', reflect_)

# wrap_ = cv2.copyMakeBorder(img, 100, 100, 100, 100, cv2.BORDER_WRAP)
# cv2.namedWindow('wrap')
# cv2.imshow('wrap', wrap_)

# constant_ = cv2.copyMakeBorder(img, 100, 100, 100, 100, cv2.BORDER_CONSTANT, value = [255, 0, 0])
# cv2.namedWindow('constant_')
# cv2.imshow('constant_', constant_)

# 5.      (         )
# def addWeighted(src1, alpha, src2, beta, gamma, dst=None, dtype=None)
#      : dst = α · img1 + β · img2 + γ   
# α + β = 1
# gamma :              ,         
#    :        ,       

# img1 = cv2.imread('1024_663_fengyelu.jpg')
# img2 = cv2.imread('1024_663_lvshuiqingshan.jpg')
#
# dst = cv2.addWeighted(img1, 0.5, img2, 0.5, 200)
#
# cv2.namedWindow('     ')
# cv2.imshow('     ', dst)

# cv2.namedWindow('salary')
# cv2.imshow('salary', img)

logo1 = cv2.imread('opencv.jpg')
rows, cols, channel = logo1.shape
roi_logo = img[0:rows, 0:cols]

# def threshold(src, thresh, maxval, type, dst=None)
#             
# src :          
# thresh :   
# maxval :         255
# type :
#         cv2.THRESH_BINARY:          maxVal,       0
#         cv2.THRESH_BINARY_INV:         maxVal,      0
#         cv2.THRESH_TOZERO:        0
#         cv2.THRESH_TOZERO_INV:        maxVal
#         cv2.THRESH_TRUNC:        maxval

# threshold        ,        (   mask)         。
#           thresh,      ret       thresh.    ,           thresh。

logo2gray = cv2.cvtColor(logo1, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(logo2gray, 10, 255, cv2.THRESH_BINARY)
print(ret)
mask_inv = cv2.bitwise_not(mask)

img1_bg = cv2.bitwise_and(roi_logo, roi_logo, mask = mask_inv)

img2_bg = cv2.bitwise_and(logo1, logo1, mask = mask)

dst = cv2.add(img1_bg, img1_bg)
img[0:rows, 0:cols] = dst

cv2.imshow('res', img)


k = cv2.waitKey(0)
if k == 27:
    cv2.destroyAllWindows()