当前位置: 代码迷 >> 综合 >> OpenCV-Python官方文档中文翻译11:Arithmetic Operations on Images图片的算术操作
  详细解决方案

OpenCV-Python官方文档中文翻译11:Arithmetic Operations on Images图片的算术操作

热度:79   发布时间:2023-10-17 09:20:47.0

Arithmetic Operations on Images

Goal

  • Learn several arithmetic operations on images, like addition, subtraction, bitwise operations, and etc.
  • Learn these functions: cv.add(), cv.addWeighted(), etc.
  • 学习图片的几种算术运算,比如加法,减法,按位运算等等。
  • 学习这些函数:cv.add(),cv.addWeighted()等等。

Image Addition

You can add two images with the OpenCV function, cv.add(), or simply by the numpy operation res = img1 + img2. Both images should be of same depth and type, or the second image can just be a scalar value.

  • Note

    There is a difference between OpenCV addition and Numpy addition. OpenCV addition is a saturated operation while Numpy addition is a modulo operation.

For example, consider the below sample:

你可以用OpenCV的函数cv.add()将两个图片添加,或者用numpy的操作 res = img1+img2。两个图片应该是相同的深度和类型,或者第二个图片只是一个标量。

  • note

    OpenCV加法和Numpy加法有区别。OpenCV加法是饱和运算,而Numpy是模运算。

例如,参考下面的例子:

>>>x = np.uint8([250])
>>>y = np.uint8([10])
>>>print(cv.add(x,y))#250+10 = 260=>255
[[255]]
>>>print(x+y)#250+10 =260%256 = 4
[4]

This will be more visible when you add two images. Stick with OpenCV functions, because they will provide a better result.

当添加两个图像的时候,它将更可见。坚持使用OpenCV功能,因为它们将提供更好的结果。

Image Blending

This is also image addition, but different weights are given to images in order to give a feeling of blending or transparency. Images are added as per the equation below:

g(x)=(1?α)f0(x)+αf1(x)

By varying α from 0→1, you can perform a cool transition between one image to another.

Here I took two images to blend together. The first image is given a weight of 0.7 and the second image is given 0.3. cv.addWeighted() applies the following equation to the image:

dst=α?img1+β?img2+γ

Here γ is taken as zero.

这也是一个图像加法,但是给与图片不同的权重会有一种融合和透明的感觉。根据以下等式添加图像:

g(x)=(1?α)f0(x)+αf1(x)

通过 α 从0到1的变化,你可以在一张图片到另一张图片之间执行很酷的过渡。

这里我用两张图片来进行融合。第一张图片赋予0.7的权重,第二张图片赋予0.3的权重。cv.addWeighted()在图像上应用下列等式:

dst=α?img1+β?img2+γ

在这里γ 取0。

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.destroyAllWindow()

OpenCV-Python官方文档中文翻译11:Arithmetic Operations on Images图片的算术操作

Bitwise Operations按位运算

This includes the bitwise AND, OR, NOT, and XOR operations. They will be highly useful while extracting any part of the image (as we will see in coming chapters), defining and working with non-rectangular ROI’s, and etc. Below we will see an example of how to change a particular region of an image.

I want to put the OpenCV logo above an image. If I add two images, it will change the color. If I blend them, I get a transparent effect. But I want it to be opaque. If it was a rectangular region, I could use ROI as we did in the last chapter. But the OpenCV logo is a not a rectangular shape. So you can do it with bitwise operations as shown below:

包括AND,OR,NOT,XOR操作。当提取图片的任何一部分的时候,定义和处理非矩形感兴趣区域等等的时候,它们都会很有用(就像在接下来的章节我们所看见的那样)。下面我们将看一个如何改变图片特定区域的一个例子。

我想将OpenCV标志放在一个图片上面。如果我相加两个图片,颜色会改变。如果我混合它们,我将得到一个透明的效果。但是我想它变为半透明的。如果是一个矩形区域,我可以像上一章节那样用感兴趣区域。但是OpenCV logo不是一个矩形形状,所以你可以用如下所示用位运算:

#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.cvtColor(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()

See the result below. Left image shows the mask we created. Right image shows the final result. For more understanding, display all the intermediate images in the above code, especially img1_bg and img2_fg.

看下面的结果。左图显示了我们创建的mask。右图显示了最后的结果。为了更好的理解,显示上述代码的所有z中间映像,尤其是img1_bg和img2_fg。
OpenCV-Python官方文档中文翻译11:Arithmetic Operations on Images图片的算术操作

Additional Resources

Exercises

  1. Create a slide show of images in a folder with smooth transition between images using cv.addWeighted function

    用cv.addWeighted函数在一个文件夹中创建一个平滑过渡的幻灯片

  相关解决方案