Python 使用连续输入创建图像
Python create image with continous input
我有一个代码可以将图像转换为 B/W。
现在我想参考原始图像构建一个新图像。
原始图像的输出是X-/Y-coordinates和黑白的“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
我花了一些时间,但我能够在没有任何复杂列表或数组的情况下解决它。
可能不是最优雅的方式,但至少它是有效的!
我创建了第二张白色图片(=参考),如果坐标已经被使用,它就会被比较。
如果尚未使用坐标,它将创建一个黑色像素。
下次检查这个坐标时,它会发现一个黑色像素,因此知道它已被使用。
最终白色图像将包含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
我有一个代码可以将图像转换为 B/W。 现在我想参考原始图像构建一个新图像。
原始图像的输出是X-/Y-coordinates和黑白的“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
我花了一些时间,但我能够在没有任何复杂列表或数组的情况下解决它。 可能不是最优雅的方式,但至少它是有效的!
我创建了第二张白色图片(=参考),如果坐标已经被使用,它就会被比较。 如果尚未使用坐标,它将创建一个黑色像素。 下次检查这个坐标时,它会发现一个黑色像素,因此知道它已被使用。
最终白色图像将包含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