我正在构建一个网络应用程序,它需要从具有白色背景的图像(或至少从与它有一些对比的背景中)“检测”服装。
我有以下函数应该将图像中的所有像素替换为白色像素,就其 RGB 值而言,它们与图像中心的像素相差超过 70%。
def crop(self, image_data):
'''"Crops" image by changing all RGB values to [255, 255, 255] if they differ more than
70% from the pixel in the center of the image.
'''
image_data.setflags(write=1)
height, width, channels = image_data.shape
new_image_data = np.full((height, width, 3), 255)
middle_pixel = image_data[(height // 2), (width // 2)]
middle_pixel_avg = np.mean(middle_pixel)
difference_limit = middle_pixel_avg * 0.7
for row in range(height):
for col in range(width):
pixel_avg = np.mean(image_data[row, col])
if (abs(middle_pixel_avg - pixel_avg) <= difference_limit):
new_image_data[row, col] = image_data[row, col]
return new_image_data
它的效率极低,在 iPhone 上拍摄的常规图像上运行大约需要 30 秒。这也很可怕,并且似乎是检测图像背景的完全错误的方法。
首先,我想知道如何使我的这个功能更有效。其次,我想知道是否有更好,更可靠的方法可以做到这一点?

