Python用连续inpu创建图像

2024-04-26 11:00:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个代码把图像转换成黑白。 现在我想建立一个新的形象,在参考原始形象。你知道吗

原始图像的输出是X-/Y坐标,黑白为“1”和“0”。你知道吗

新图像将接收这些信息,但不按时间顺序。 因此,如果它已经接收到关于特定坐标的信息,它必须检查并提供负输出,这样就可以避免双重输入。你知道吗

我还没有找到许多类似的例子,只有一些例子是在大约的方向。你知道吗

有人知道如何实现这一点吗?你知道吗

更新:

我构建了一个代码,如果原始图像中的参考像素是黑色的(否则它将保留为白色),则将白色图像中的像素转换为黑色。你知道吗

此外,使用的坐标被输入到一个列表中,并在使用时进行检查。 但是,这部分工作不正常。 虽然坐标[10,10]以前在循环中使用过,但是代码显示Coordinate not in the system

任何帮助都将不胜感激!你知道吗

import cv2
import numpy

white = cv2.imread('white.jpg') #loading white image
white = cv2.resize(white,(640,480)) #adjusting it to the size of the original image


y = 0 #for testing purposes the white image gets blackened manually
x = 0
j = 0

while j < 50:
    content = numpy.zeros((200, 2)) #creating a list with 200 entries, every entry contains 2 values
    content = ([x, y]) #adding two values to the list

    if condition[y, x] = 1: #condition = 1 means that in the reference picture at this coordinate the pixel is black
        white[y,x] = 0 #"0" creates a black pixel at the specified coordinate on the white image
    x += 5
    y += 5
    j += 1

x = 10 #taking a value which already has been used 
y = 10

try:
    b = content.index([x, y]) #check if coordinate is in the list
except ValueError:
    print("Coordinate not in the system")
else:
    print("Coordinate already in the system")


i = 0

while i < 100:

    cv2.imshow('Bild', white) #displays the image
    if cv2. waitKey(1) == ord('q'):
        break

Tags: the代码in图像image信息coordinateif
1条回答
网友
1楼 · 发布于 2024-04-26 11:00:50

这花了我一段时间,但我能够解决它没有任何复杂的列表或数组。 可能不是最优雅的方式,但至少它是工作!你知道吗

我创建了第二个白色图片(=参考),比较坐标是否已经被使用。 如果没有使用坐标,它将创建一个黑色像素。 下次检查这个坐标时,它会发现一个黑色像素,因此知道它已经被使用了。你知道吗

最后,白色图像将包含49个黑色像素(因为位置[10,10]已经被使用并且不会被绘制)。你知道吗

import cv2
import numpy

white = cv2.imread('C:\white.jpg') #loading white image
reference = cv2.imread('C:\white.jpg') #loading white image
white = cv2.resize(white,(640,480)) #adjusting it to the size of the original image
reference = cv2.resize(white,(640,480)) #adjusting it to the size of the original image

y = 0 #for testing purposes the white image gets blackened manually
x = 0
j = 0

reference[10,10] = 0


while j < 50:

    if [255,255,255] in reference[y,x]:
        reference[y,x] = 0 #"0" creates a black pixel at the specified coordinate on the reference image
        white[y,x] = 0 #"0" creates a black pixel at the specified coordinate on the white image
        print("Coordinate not in system")
    else:
        print("coordinate already in system")
    x += 5
    y += 5
    j += 1


i = 0

while i < 100:

    cv2.imshow('image copy', white) #displays the image
    if cv2. waitKey(1) == ord('q'):
        break

相关问题 更多 >