gpt4 book ai didi

python - 为什么要使用该图像作为前一个过程的图像?

转载 作者:行者123 更新时间:2023-12-02 17:39:26 26 4
gpt4 key购买 nike

很简单,我正在学习如何使用openCV / numpy编辑照片。

我的问题是为什么第二个功能使用第一个创建的图像?

我运行两个函数-一个以黑色和白色为列上色,第二个以黑色和白色为行上色。

第一个函数运行良好,但是第二个函数使用在第一个函数中创建的图像,因此我得到了黑白的行和列。

import cv2
import numpy as np
from matplotlib import pyplot as plt

img_source = "brad.jpg"

def read_image(image_source):
#global img, width, height
img = cv2.imread(image_source, 1)
height, width = img.shape[:2]
print("Image size: x ", width, " y ", height)
return img, width, height

def black_and_white_cols(image_source):
width_adjustment = 100
total_cols = round(width / width_adjustment,0)
edited_image = image_source
bw_image = cv2.imread(img_source, 0)
# The next line is to convert to the right interface
# https://stackoverflow.com/questions/11067962/is-it-possible-to-have-black-and-white-and-color-image-on-same-window-by-using-o
bw_image_b = cv2.cvtColor(bw_image,cv2.COLOR_GRAY2BGR)

for x in range(1, int(total_cols), 2):
top_row = 0
bottom_row = height
left_col = x*width_adjustment
right_col = (x * width_adjustment) + width_adjustment
bw_part = bw_image_b[top_row:bottom_row, left_col:right_col]
edited_image[top_row:bottom_row, left_col:right_col] = bw_part
show_image(edited_image)

def black_and_white_cols(image_source):
width_adjustment = 100
total_cols = round(width / width_adjustment,0)
edited_image = image_source
bw_image = cv2.imread(img_source, 0)
# The next line is to convert to the right interface
# https://stackoverflow.com/questions/11067962/is-it-possible-to-have-black-and-white-and-color-image-on-same-window-by-using-o
bw_image_b = cv2.cvtColor(bw_image,cv2.COLOR_GRAY2BGR)

for x in range(1, int(total_cols), 2):
top_row = 0
bottom_row = height
left_col = x*width_adjustment
right_col = (x * width_adjustment) + width_adjustment
bw_part = bw_image_b[top_row:bottom_row, left_col:right_col]
edited_image[top_row:bottom_row, left_col:right_col] = bw_part
show_image(edited_image)
return edited_image

def black_and_white_rows(image_source):
width_adjustment = 100
edited_image = image_source
total_rows = round(height / width_adjustment,0)
bw_image = cv2.imread(img_source, 0)
bw_image_b = cv2.cvtColor(bw_image,cv2.COLOR_GRAY2BGR)

for x in range(1, int(total_rows), 2):
top_row = x * width_adjustment
bottom_row = (x * width_adjustment) + width_adjustment
left_col = 0
right_col = width
bw_part = bw_image_b[top_row:bottom_row, left_col:right_col]
edited_image[top_row:bottom_row, left_col:right_col] = bw_part

show_image(edited_image)

def show_image(image_source):
cv2.imshow('This is your image', image_source)
cv2.waitKey(0)
cv2.destroyAllWindows()

if __name__ == "__main__":
img, width, height = read_image(img_source)
new_image = black_and_white_cols(img)
new_image_2 = black_and_white_rows(img)

这是 new_image = black_and_white_cols(img)运行后的图像。

enter image description here

这是在 new_image_2 = ...运行之后。

enter image description here

为什么第二张图像保留黑白列?我通过 img_source使用非常原始的 read_image图像来调用它。为什么使用列编辑图像?

最佳答案

就像在注释中一样,当您执行edited_image = image_source时,您仅将指针复制到图像数组,而不克隆该数组本身。你可以做
edited_image = image_source.copy()
image_source复制到edited_image

关于python - 为什么要使用该图像作为前一个过程的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46763121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com