用python将两幅图像进行调整大小和通道数相同,并且不使用库函数进行对两幅图像进行加减
时间: 2023-05-28 22:06:04 浏览: 77
法操作。
代码如下:
```python
import cv2
# 读取两幅图像
img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')
# 调整大小
img1 = cv2.resize(img1, (img2.shape[1], img2.shape[0]))
# 调整通道数
if img1.shape[2] > img2.shape[2]:
img1 = img1[:, :, :img2.shape[2]]
else:
img2 = img2[:, :, :img1.shape[2]]
# 加法操作
add_img = img1 + img2
# 减法操作
sub_img = img1 - img2
# 显示结果
cv2.imshow('add_img', add_img)
cv2.imshow('sub_img', sub_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在上述代码中,我们首先读取了两幅图像,然后使用`cv2.resize()`函数将其中一幅图像调整为和另一幅图像相同的大小。接着,我们通过比较两幅图像的通道数,来决定调整哪一幅图像的通道数,从而保证两幅图像的通道数相同。最后,我们通过加减法操作,得到了两幅图像的加减法结果,并使用`cv2.imshow()`函数将结果显示出来。
相关问题
在python中用自己写的函数实现两幅图像的加减,要求不使用库函数,并且输出图像
以下是一个简单的实现示例:
```python
from PIL import Image
def add_images(image1, image2):
"""将两幅图像相加"""
# 获取图像尺寸
width, height = image1.size
# 创建新的空白图像
new_image = Image.new("RGB", (width, height))
# 循环遍历每个像素,并将两幅图像像素值相加
for x in range(width):
for y in range(height):
# 获取像素点的RGB值
r1, g1, b1 = image1.getpixel((x, y))
r2, g2, b2 = image2.getpixel((x, y))
# 将像素点的RGB值相加
r = min(r1 + r2, 255)
g = min(g1 + g2, 255)
b = min(b1 + b2, 255)
# 将新图像的像素点设置为相加后的RGB值
new_image.putpixel((x, y), (r, g, b))
return new_image
def subtract_images(image1, image2):
"""将两幅图像相减"""
# 获取图像尺寸
width, height = image1.size
# 创建新的空白图像
new_image = Image.new("RGB", (width, height))
# 循环遍历每个像素,并将两幅图像像素值相减
for x in range(width):
for y in range(height):
# 获取像素点的RGB值
r1, g1, b1 = image1.getpixel((x, y))
r2, g2, b2 = image2.getpixel((x, y))
# 将像素点的RGB值相减
r = max(r1 - r2, 0)
g = max(g1 - g2, 0)
b = max(b1 - b2, 0)
# 将新图像的像素点设置为相减后的RGB值
new_image.putpixel((x, y), (r, g, b))
return new_image
# 加载两幅图像
image1 = Image.open("image1.jpg")
image2 = Image.open("image2.jpg")
# 将两幅图像相加,并保存为新图像
new_image = add_images(image1, image2)
new_image.save("add_image.jpg")
# 将两幅图像相减,并保存为新图像
new_image = subtract_images(image1, image2)
new_image.save("subtract_image.jpg")
```
在上面的示例中,我们使用了Python的PIL库来加载和保存图像。我们自己编写了两个函数来实现图像加减操作。在这些函数中,我们使用了循环来遍历每个像素,并计算出相应的加或减的结果。最后,我们使用`putpixel`函数来设置新图像的像素点。
在python中用自己写的函数实现两幅图像的加减,要求不使用库函数
以下是实现两幅图像的加减的Python代码:
```python
import numpy as np
import cv2
# 加载两幅图像
img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')
# 定义加法函数
def add_image(img1, img2):
# 将图像转换为numpy数组
img1_array = np.array(img1)
img2_array = np.array(img2)
# 对图像数组进行加法运算
result_array = img1_array + img2_array
# 将结果数组转换为图像
result_image = cv2.cvtColor(result_array, cv2.COLOR_BGR2RGB)
return result_image
# 定义减法函数
def subtract_image(img1, img2):
# 将图像转换为numpy数组
img1_array = np.array(img1)
img2_array = np.array(img2)
# 对图像数组进行减法运算
result_array = img1_array - img2_array
# 将结果数组转换为图像
result_image = cv2.cvtColor(result_array, cv2.COLOR_BGR2RGB)
return result_image
# 调用加法函数,得到加法结果
add_result = add_image(img1, img2)
# 调用减法函数,得到减法结果
subtract_result = subtract_image(img1, img2)
# 显示加法结果和减法结果
cv2.imshow('Add Result', add_result)
cv2.imshow('Subtract Result', subtract_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在上述代码中,我们首先定义了两个函数 `add_image()` 和 `subtract_image()`,用于实现图像的加减运算。这两个函数都接受两幅图像作为输入,使用numpy库将这两幅图像转换为numpy数组,然后对数组进行加减运算,最后将结果数组转换为图像。
在主程序中,我们加载了两幅图像 `img1.jpg` 和 `img2.jpg`,然后调用上述两个函数分别进行加法和减法运算,并将结果显示出来。
需要注意的是,在对图像进行加减运算时,需要保证两幅图像的大小和通道数都相同,否则会出现错误。