OpenCV-Python image subtraction operation cv2.subtract function detailed explanation and comparison with matrix subtraction

☞ ░Go to LaoYuanPython blog https://blog.csdn.net/LaoYuanPython

I. Overview

In " OpenCV-Python image addition operation cv2.add function detailed explanation ", the image addition operation is introduced in detail. When there is addition, there is subtraction. This article introduces the image subtraction operation.

Image subtraction is usually used to find the difference in images. For example, the blood flow can be seen by comparing the medical blood vessel image with the blood vessel image after contrast. Of course, the subtraction can also be used in special image processing.

Two, cv2.subtract function syntax

Calling syntax:

subtract(src1, src2, dst=None, mask=None, dtype=None)

Parameter Description:

The parameters of subtraction are similar to those of addition:

  1. src1: The image array or a scalar as the subtracted
  2. src2: the image array or a scalar as a subtraction
  3. dst: Optional parameter, the variable for saving the output result, the default value is None, if it is not None, the output image is saved to the corresponding actual parameter of dst, and its size and channel number are the same as the input image. The depth of the image (that is, the image pixel Digits) is determined by the dtype parameter or the input image
  4. mask: Image mask, optional parameter, 8-bit single-channel grayscale image, used to specify the elements of the output image array to be changed, that is, the output image pixels are output only when the corresponding position element of the mask is not 0, otherwise All channel components of the pixel at this position are set to 0
  5. dtype: optional parameter, the depth of the output image array, that is, the number of bits of a single pixel value of the image (if RGB is represented by three bytes, it is 24 bits).
  6. Return value: the result image of the subtraction

Three, four scenes of subtraction operation

  1. To subtract two image matrices, the two matrices must have the same size and number of channels
    dst(I)=saturate(src1(I)−src2(I))if mask(I)≠0
  2. Subtract 1 image matrix and 1 scalar. It is required that src2 is a scalar or the same number of elements as the number of channels of src1. After actual testing, it should be a quadruple. If src1 is 3-channel, it will be compared with Subtract the first 3 elements of the quadruple
    dst(I)=saturate(src1(I)−src2)if mask(I)≠0
  3. Subtract 1 scalar from an image array, requires src1 to be a scalar or the same number of elements as the number of channels of src1
    dst(I)=saturate(src1−src2(I))if mask(I)≠0
  4. In the case of SubRS subtracting the matrix from the given value, it is the inverse difference of the subtraction of a scalar and an image array. The old ape thinks this is a special interpretation of the second scene
    dst(I)=saturate(src2−src1(I))if mask(I)≠0

If there is any doubt about the above calculation process, please refer to " opencv image processing learning essay: the meaning of saturate in the help document calculation formula ".

Four, subtraction operation case

In the following two pictures, the latter is based on the former with two additional text and a curve:

Original image seaside.jpg:

Insert picture description here

Add a changed new image seaside_new.jpg to the original image:Insert picture description here

Subtraction case code:

import numpy as np
import cv2

def  main():
    img1 = cv2.imread(r'F:\pic\seaside.jpg')
    img2 = cv2.imread(r'F:\pic\seaside_new.jpg')
  
    diffImg1 = cv2.subtract(img1,img2)
    diffImg2 = cv2.subtract(img2, img1)
    diffImg3 = img1 - img2
    diffImg4 =  img2 - img1

    cv2.imshow('subtract(img1,img2)',diffImg1)
    cv2.imshow('subtract(img2,img1)', diffImg2)
    cv2.imshow('img1 - img2',diffImg3)
    cv2.imshow('img2 - img1', diffImg4)

    cv2.waitKey(0)

main()

Subtract(img2, img1) result graph:

Insert picture description here

Subtract(img1,img2) result graph:

Insert picture description here

img1-img2 result graph:

Insert picture description here

img2-img1 result graph:

Insert picture description here

It can be seen that opencv's subtraction and matrix subtraction are more natural and smooth when dealing with differences. This situation is more obvious when the two pictures look similar on the surface but actually have a relatively large difference.

In the above case, the first picture was saved after using windows to draw and save, and the second picture was added with content on the basis of the previous saved picture. If the first picture is directly used as the original picture without saving, the content and the saved as There is a big difference, and the sizes of the two files are different. The following seaside2.jpg is the real original image saved without the drawing tool, and seaside.jpg is the original image saved with the drawing tool. The resolution of the two images is the same.

Insert picture description here
We use the real original image to compare with the converted content image. code show as below:

def  main():
    img1 = cv2.imread(r'F:\pic\seaside2.jpg')
    img2 = cv2.imread(r'F:\pic\seaside_new.jpg')
  
    print(img1.shape,img2.shape)
    diffImg1 = cv2.subtract(img1,img2)
    diffImg2 = cv2.subtract(img2, img1)
    diffImg3 = img1 - img2
    diffImg4 =  img2 - img1

    cv2.imshow('subtract(img1,img2)',diffImg1)
    cv2.imshow('subtract(img2,img1)', diffImg2)
    cv2.imshow('img1 - img2', diffImg3)
    cv2.imshow('img2 - img1',diffImg4)

    cv2.waitKey(0)

main()

The image of each processing result is as follows:

subtract(img1,img2):

Insert picture description here

subtract(img2,img1):Insert picture description here
img2 img1:

Insert picture description here

img1 img2:

Insert picture description here
It can be seen that OpenCV processing is much better than matrix subtraction for this kind of image processing.

After careful analysis of the relevant data, it is found that most of the pixel data of the image after saving as a small change in the three components of BGR, it may be a difference of 1-2 values ​​for each channel, some channels have increased a little, there is It has to be reduced a bit. Opencv is a saturation operation. When it is less than 0, it is set to 0, but when the matrix operation has a negative number, because the type is uint8, the negative number becomes 256 plus the value of the negative number, resulting in a big difference.

V. Summary

This article introduces the function, specific syntax and four usage scenarios of opencv image subtraction in detail, and gives examples of the difference between opencv image subtraction and matrix subtraction. It can be seen that OpenCV's subtraction function is more natural and smoother than matrix subtraction in image processing. .

For more introduction to OpenCV-Python, please refer to the column "OpenCV-Python Graphics and Image Processing"

Blog address : https://blog.csdn.net/laoyuanpython/category_9979286.html

Paid column about the old ape

Lao Yuan’s paid column "Developing Graphical Interface Python Applications Using PyQt" ( https://blog.csdn.net/laoyuanpython/category_9607725.html ) specifically introduces the basic tutorials for Python-based PyQt graphical interface development, and the paid column "moviepy audio and video development Column" ( https://blog.csdn.net/laoyuanpython/category_10232926.html ) introduces in detail the related methods of moviepy audio and video editing and synthesis and the use of related methods to process related editing and synthesis scenes. Both columns are suitable for certain Novice readers who have basic Python but no relevant knowledge will learn.

Paid column article directory : "moviepy audio and video development column article directory" ( https://blog.csdn.net/LaoYuanPython/article/details/107574583 ), "Use PyQt to develop graphical interface Python application column directory" ( https:// blog.csdn.net/LaoYuanPython/article/details/107580932 ).

For those who lack Python foundation, you can learn Python from scratch through Lao Yuan’s free column "Column: Python Basic Tutorial Directory" ( https://blog.csdn.net/laoyuanpython/category_9831699.html ).

If you are interested and willing to support the readers of Old Ape, welcome to buy paid columns.

Learn Python and OpenCV from the old ape!

☞ ░Go to LaoYuanPython blog post directory https://blog.csdn.net/LaoYuanPython

Guess you like

Origin blog.csdn.net/LaoYuanPython/article/details/109020778