为什么图像被用作前一个过程的图像?

时间:2017-10-16 04:47:54

标签: python python-3.x opencv

很简单,我正在学习如何使用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)运行后的图像。

libonion

以及new_image_2 = ...之后的运行。

enter image description here

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

1 个答案:

答案 0 :(得分:1)

在注释中,当您执行edited_image = image_source时,只将指针复制到图像数组,而不是克隆数组本身。你可以做到

edited_image = image_source.copy()

image_source复制到edited_image