简体   繁体   English

如何屏蔽正方形的图像?

[英]How to mask an image in a square shape?

I am trying to mask a square shape out of a rectangular image.我试图从矩形图像中掩盖一个正方形。 Those area out of square shape will be painted white.那些非方形的区域将被漆成白色。 I write code as below.我写的代码如下。

photo_data = imageio.imread('./demo/dog.jpg')
total_rows,total_columns,layer=photo_data.shape
X,Y=np.ogrid[:total_rows,:total_columns]
center_rows=total_rows/2
center_columns=total_columns/2
upper_mask=X-center_rows>500
low_mask=X-center_rows<-500
left_mask=Y-center_columns>500
right_mask=Y-center_columns<-500
final_mask=np.logical_and(upper_mask,low_mask,left_mask,right_mask)
photo_data[final_mask]=0
plt.figure(figsize=(15,15))
plt.imshow(photo_data)

I guess np.logical_and cannot handle array with different number size.我猜 np.logical_and 无法处理具有不同数字大小的数组。 How to resolve this problem?如何解决这个问题?

I think that you make things a bit more complicated than you need to.我认为你让事情变得比你需要的更复杂。 Conceptually, I think that you need to do something along these lines:从概念上讲,我认为您需要按照以下方式做一些事情:

photo_data = imageio.imread('./demo/dog.jpg')
total_rows,total_columns,layer=photo_data.shape
mask_size = 500
photo_data[total_rows/2-mask_size/2:total_rows/2+mask_size/2,
    total_columns/2-mask_size/2:total_columns/2+mask_size/2,
    :] = 0
plt.figure(figsize=(15,15))
plt.imshow(photo_data)

EDIT:编辑:

I assume that there are even more elegant ways of performing this, but one way that I personally like is to use numpys elemet wise multiplication to apply the mask:我认为有更优雅的方法来执行此操作,但我个人喜欢的一种方法是使用 numpys elemet wise 乘法来应用掩码:

import numpy as np

photo_data = imageio.imread('./demo/dog.jpg')
total_rows,total_columns,layer=photo_data.shape
mask_size = 500

mask = np.zeros(photo_data.shape)
mask[total_rows/2-mask_size/2:total_rows/2+mask_size/2,
    total_columns/2-mask_size/2:total_columns/2+mask_size/2,
    :] = 1
photo_data = photo_data * mask
plt.figure(figsize=(15,15))
plt.imshow(photo_data)

Instead of代替

np.logical_and(upper_mask,low_mask,left_mask,right_mask)

you can use您可以使用

upper_mask & low_mask & left_mask & right_mask

but actually you need OR for your task, so correct way is to use:但实际上你需要OR来完成你的任务,所以正确的方法是使用:

upper_mask | low_mask | left_mask | right_mask

Then you code works well!那么你的代码运行良好!

Full corrected code down below:完整更正的代码如下:

Try it online!在线试试吧!

import imageio, numpy as np, matplotlib.pyplot as plt

photo_data = imageio.imread('https://i.stack.imgur.com/lb6U1.jpg')
total_rows,total_columns,layer=photo_data.shape
X,Y=np.ogrid[:total_rows,:total_columns]
center_rows=total_rows/2
center_columns=total_columns/2
upper_mask=X-center_rows>100
low_mask=X-center_rows<-100
left_mask=Y-center_columns>100
right_mask=Y-center_columns<-100
final_mask=upper_mask | low_mask | left_mask | right_mask
photo_data[final_mask]=0
plt.figure(figsize=(7,5))
plt.imshow(photo_data)
plt.show()

Input:输入:

img0

Output:输出:

图片1

Also if you want white surroundings instead of black then replace另外,如果您想要白色环境而不是黑色,请更换

photo_data[final_mask]=0

with

photo_data[final_mask]=255

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM