美文网首页Python数据分析
[OpenCV-Python Tutorials]-Core o

[OpenCV-Python Tutorials]-Core o

作者: 六千宛 | 来源:发表于2021-08-05 15:13 被阅读0次

    Image Addition

    图像加法 cv.add

    可以通过OpenCV函数 cv.add() 或仅通过numpy操作 res = img1 + img2 添加两个图像。两个 图像应具有相同的深度和类型,或者第二个图像可以只是一个标量值。

    注意: OpenCV加法和Numpy加法之间有区别。OpenCV加法是饱和运算,而Numpy加法是模运算。见下代码

    >>> import numpy as np
    >>> a = np.uint([250])
    >>> b = np.uint([10])
    >>> print( cv.add(x,y)
    [[255]]
    >>> a + b
    array([260], dtype=uint32)
    

    Image Blending

    图像融合 cv.addWeighted

    这也是图像加法,但是对图像赋予不同的权重,以使其具有融合或透明的感觉。根据以下等式添加图像:


    image.png
    image.png
    img1 = cv.imread('ml.png')
    img2 = cv.imread('opencv-logo.png')
    dst = cv.addWeighted(img1,0.7,img2,0.3,0) cv.imshow('dst',dst)
    cv.waitKey(0)
    cv.destroyAllWindows()
    
    image.png

    Bitwise Operations

    使用掩码进行运算时,位运算符bitewise_and()、bitewise_or()、bitewise_xor()和bitewise_not()很有用!

    void bitwise_and(InputArray src1, InputArray src2, OutputArray dst,
                                InputArray mask = noArray());  //dst = src1 & src2
    void bitwise_or(InputArray src1, InputArray src2, OutputArray dst, 
                                InputArray mask = noArray());  //dst = src1 | src2
    void bitwise_xor(InputArray src1, InputArray src2, OutputArray dst, 
                                InputArray mask = noArray());   //dst = src1 ^ src2
    void bitwise_not(InputArray src, OutputArray dst, 
                                InputArray mask = noArray());  //dst = ~src 
    

    说明:
    (1)bitwise_and是对二进制数据进行“与”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“与”操作,1 & 1 = 1,1 & 0 = 0,0 & 1 = 0,0 & 0 = 0
    (2)bitwise_or是对二进制数据进行“或”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“或”操作,1 | 1 = 1,1 | 0 = 0,0 | 1 = 0,0 | 0 = 0
    (3)bitwise_xor是对二进制数据进行“异或”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“异或”操作,1 ^ 1 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 0 ^ 0 = 0
    (4)bitwise_not是对二进制数据进行“非”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“非”操作,~1 = 0,~0 = 1

    # Load two images
    img1 = cv.imread('messi5.jpg')
    img2 = cv.imread('opencv-logo-white.png')
    # I want to put logo on top-left corner, So I create a ROI
    rows,cols,channels = img2.shape
    roi = img1[0:rows, 0:cols]
    # Now create a mask of logo and create its inverse mask also
    img2gray = cv.cvtColor63b61574eb8cab)(img2,cv.COLOR_BGR2GRAY)
    ret, mask = cv.threshold(img2gray, 10, 255, cv.THRESH_BINARY)
    mask_inv = cv.bitwise_not(mask)
    # Now black-out the area of logo in ROI
    img1_bg = cv.bitwise_and(roi,roi,mask = mask_inv)
    # Take only region of logo from logo image.
    img2_fg = cv.bitwise_and(img2,img2,mask = mask)
    # Put logo in ROI and modify the main image
    dst = cv.add(img1_bg,img2_fg)
    img1[0:rows, 0:cols ] = dst
    cv.imshow('res',img1)
    cv.waitKey(0)
    cv.destroyAllWindows()
    

    相关文章

      网友评论

        本文标题:[OpenCV-Python Tutorials]-Core o

        本文链接:https://www.haomeiwen.com/subject/yxvmvltx.html