0
簡単に、私はopenCV
/numpy
で写真を編集する方法を学んでいます。イメージが以前のプロセスのイメージで使用されているのはなぜですか?
は最初に作成された画像を使用していますか?
私は2つの機能を実行します.1つはカラーの列を白黒で、もう1つは行を白黒で表示します。
最初の関数は正常に実行されますが、2番目の関数は最初に作成されたイメージを使用するため、行はとなり、は白黒になります。
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)
の実行後のイメージです。
、ここnew_image_2 = ...
実行した後です。
なぜ第二の画像は黒と白の列を保持していますか?私は元のimg_source
イメージを使って、read_image
経由で呼び出しています。なぜ列編集されたイメージを使用していますか?
これらは同じ参照を共有しているためです。 – Sraw
'コピーインポートdeepcopy'が必要でしょうか? – Sraw
'img_source'と' edited_image'は、 'edited_image = image_source'というステートメントで定義されたのと同じイメージです。したがって、 'edited_image'を変更すると、' image_source'も変更されます。 – kindall