"AI Basics" python:opencv--image arithmetic operations (1)

Source: Internet
Author: User

Image arithmetic Operations

1. Image addition
Use Cv2.add () to add two images, or you can use NUMPY,RES=IMG1+IMG2 directly. The size of two images, the type must be the same, or the second image can be a simple scalar value.
The addition of OPENCV is a kind of saturation operation, while the addition of NumPy is a kind of modulo operation.
The results of OPENCV will be better.

import cv2import numpy as npx=np.uint8([250])y=np.uint8([10])print (x+y)print (cv2.add(x,y))
[4][[255]]

2. Image blending
This is also an addition, the difference is that the weight of the two images is different, this will give a person a mixed or transparent feeling.
The function cv2.addweighed () blends the pictures by the following formula.
Example: Mix two images, the first weight is 0.7. The second weight is 0.3.

import cv2import numpy as npimg1=cv2.imread(‘1c.jpg‘)img2=cv2.imread(‘1d.jpg‘)dst = cv2.addWeighted(img1,0.7,img2,0.3,0)cv2.imshow(‘dst‘,dst)cv2.waitKey(0)cv2.destroyAllWindows()

3. Cvtcolor function
Most of our lives see the color of XXX films are RGB type, but in the image processing, need to use grayscale, binary, HSV, HSI and other color formats, OPENCV provides the Cvtcolor () function to achieve these functions.
Grammar:

img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)

RGB Conversion Format Parameters:

"AI Basics" python:opencv--image arithmetic operations (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.