程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Image addition, subtraction, division and multiplication in opencv (implemented in Python)

編輯:Python

List of articles

    • 1. Add up
    • 2. Subtraction
    • 3. Multiplication
    • 4. division

1. Add up

Code combat

import os
import cv2
import numpy as np
# Read and zoom pictures
lenna=cv2.imread('images/lenna.png')
lenna=cv2.resize(src=lenna,dsize=(450,450))
# Create a picture of the same size
npimg=np.ones(shape=(lenna.shape[0],lenna.shape[1],lenna.shape[2]),dtype=np.uint8)*100
# And two pictures
dst=cv2.add(src1=lenna,src2=npimg)
# display picture
cv2.imshow('lenna',lenna)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')


You can see that the resulting image is similar to the exposure .


2. Subtraction

def Subtract():
# Read and zoom pictures
lenna = cv2.imread('images/lenna.png')
lenna = cv2.resize(src=lenna, dsize=(450, 450))
# Create a picture of the same size
npimg = np.ones(shape=(lenna.shape[0], lenna.shape[1], lenna.shape[2]), dtype=np.uint8) * 100
# First, add two pictures
dst = cv2.add(src1=lenna, src2=npimg)
# Subtract the added image , Restore to original drawing
original=cv2.subtract(src1=dst,src2=npimg)
print(" The maximum pixel value in the original image : {}".format(lenna.max()))
print(' The maximum pixel value after adding : {}'.format(dst.max()))
print(' The maximum pixel value after subtraction : {}'.format(original.max()))
# display picture
cv2.imshow('lenna', lenna)
cv2.imshow('dst', dst)
cv2.imshow('original',original)
cv2.waitKey(0)
cv2.destroyAllWindows()


print(" The maximum pixel value in the original image : {}".format(lenna.max()))
print(' The maximum pixel value after adding : {}'.format(dst.max()))
print(' The maximum pixel value after subtraction : {}'.format(original.max()))

You can see that the maximum pixel value in the original image is 255, The maximum pixel value after adding must also be 255; But suppose there is a pixel value in the original image 160 Of , First add 100 after , Then the pixel value becomes 255( Because more than 255, So only for 255), So when subtracting , This pixel value becomes 155, And the original 160 Compared with , So even if the original graph first adds and then subtracts , The image will generally become a little darker .


3. Multiplication

def mutilpImg():
# Read and zoom pictures
lenna = cv2.imread('images/lenna.png')
lenna = cv2.resize(src=lenna, dsize=(450, 450))
# Create a picture of the same size
npimg = np.ones(shape=(lenna.shape[0], lenna.shape[1], lenna.shape[2]), dtype=np.uint8) * 100
# To multiply
dst=cv2.multiply(src1=lenna,src2=npimg)
# display picture
cv2.imshow('lenna', lenna)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

Why is the output mostly white , It is because many pixel values are greater than after multiplication 255, So the end result is 255, That is, many parts of the image are white .


4. division

def DividImg():
# Read and zoom pictures
lenna = cv2.imread('images/lenna.png')
lenna = cv2.resize(src=lenna, dsize=(450, 450))
# Create a picture of the same size
npimg = np.ones(shape=(lenna.shape[0], lenna.shape[1], lenna.shape[2]), dtype=np.uint8) * 100
# To multiply
dst = cv2.divide(src1=lenna, src2=npimg)
# display picture
cv2.imshow('lenna', lenna)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

print(“ The maximum pixel value in the original image : {}”.format(lenna.max()))
print(“ Maximum pixel value after division : {}”.format(dst.max()))

You can see that the maximum pixel value in the original image is 255, All pixel values in the original image except 100 after , Then the final result can only be <=3, So it looks black .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved