我完全被這個問題困住了——我需要隨機排列 2D 影像上一個圓圈內的像素。我需要這個用于我正在設計的實驗,在這個實驗中,搜索這個小圓圈將作為任務的低級感知部分。重要的是像素來自影像本身而不是隨機值,以保持修改后的影像和未修改的影像之間的影像強度均勻。
我不太了解Python,所以到目前為止我的代碼非常hacky - 我首先打開影像,并確保它是RGB。然后我制作一個深拷貝(因為我將使用蒙版修改原始檔案,但我仍然需要實際的原始像素值)。然后我畫了一個鮮紅色的圓圈(255,0,0),并將pix_mask定義為紅色為255的任何地方(我們的刺激是自然場景和物體,所以影像中不應該有任何其他地方) )。然后我使用創建一個新的蒙版影像pix_mask,并將蒙版乘以復制的影像資料以獲取圓圈內的資料。聽起來很亂,但結果如下:


這是我用來執行此操作的代碼:
def create_pixel_mask_images(image_fname: str):
# Open the image which requires a scrambled dot. Add an alpha channel and make a copy of the data
image: JpegImage = Image.open(image_fname)
image = image.convert("RGB")
image_copy = image.copy()
# Create the drawer and draw a filled red circle on the original image (255, 0, 0)
image_draw = ImageDraw.Draw(image)
image_draw.ellipse((150, 150, 250, 250), fill = "red")
image.save("TEST_DOT.png")
# Read the data from the copy and the now-modified original image into arrays
image_arr: NumpyArray = numpy.array(image)
image_copy_arr: NumpyArray = numpy.array(image_copy)
# Create a binary mask by testing for the red circle we drew on the original image (255 red)
pix_mask: NumpyArray = image_arr[:, :, 0] == 255
# Invert the mask (i.e., everything outside of the dot is now white, and the dot black)
# Write them out to file to error-check
mask_image: PILImage = Image.fromarray((pix_mask * 255).astype(numpy.uint8))
image.convert("RGB")
mask_image_inverse: PILImage = ImageOps.invert(mask_image)
image.convert("RGB")
pix_mask_inverse: NumpyArray = numpy.array(mask_image_inverse)
# Read the inverted pixel mask as a numpy array. Multiply the unmodified image by both masks
# in order to get the data both inside and outside of the mask
inner_data = image_copy_arr * pix_mask[..., None]
outer_data = image_copy_arr * pix_mask_inverse[..., None]
# Create new images from the masked data
inner_image: PILImage = Image.fromarray(inner_data.astype(numpy.uint8))
outer_image: PILImage = Image.fromarray(outer_data.astype(numpy.uint8))
outer_image = ImageOps.invert(outer_image)
# AS A CHECK multiply the inner- and outer-masked images together. We should get the original image back
image_data = image.load()
for y in range(image.size[1]):
for x in range(image.size[0]):
if image_data[x, y] == (255, 0, 0):
image_data[x, y] = (255, 255, 255)
recombined_image = ImageChops.multiply(inner_image, outer_image)
inner_image.save('INNER_DATA.png')
mask_image.save('MASK.jpg')
mask_image_inverse.save('MASK_INVERSE.jpg')
recombined_image.save('RECOMBINATION_TEST.jpg')
You can see at the bottom I create a recombined_image by multiplying the inner_image, which is the data within the circle mask, and the outer_image, which is the rest of the data outside of the circle mask, together; however, I just end up the same image as the second picture here (just the data inside the circle).
I use the nested for-loop here to set any red pixels in the original image to white as if I left the region black I would lose the data there (checked the docs for ImageChops.multiply() - 
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/439644.html
標籤:python python-3.x python-imaging-library
下一篇:`==`和`and`之間的區別
