如何使用 numpy.compress 方法减少图像部分? (numpy+scikit-image)

How to reduce image portion with numpy.compress method ? (numpy+scikit-image)

您好,使用示例图像 phantom.png 我正在使用 numpy + skimage 库进行一些操作,经过一些修改后,最后一个练习要求:

Compress the size of center spots by 50% and plot the final image.

这些是我之前做的步骤。

我读图像做

img = imread(os.path.join(data_dir, 'phantom.png'))

然后应用以下使其成为黑白

img[np.less_equal(img[:,:,0],50)] = 0
img[np.greater_equal(img[:,:,0],51)] = 255

用给定的坐标拍摄了几个图像切片(黑点)

img_slice=img.copy() 
img_slice=img_slice[100:300, 100:200]
img_slice2=img.copy() 
img_slice2=img_slice2[100:300, 200:300]

现在翻转它们

img_slice=np.fliplr(img_slice)
img_slice2=np.fliplr(img_slice2)

并将它们放回图像副本中

img2=img.copy()
img2[100:300, 200:300]=img_slice 
img2[100:300, 100:200]=img_slice2

这是最终 ("compress") 练习之前的结果图像:

然后我被要求使用 numpy.compress 方法 "reduce" 黑点。

使用"compress"方法后的预期结果如下图(截图),其中黑点减少了50%:

但我不知道如何在图像或图像切片上使用 numpy.compress 方法来获得该结果,甚至没有接近,我得到的只是看起来像裁剪或它的拉伸部分。

对于 help/explanation 关于 numpy.compress 方法如何处理这个问题,以及是否可行,我将不胜感激。

我想你可以先通过以下方式切掉中心点: center_spots = img2[100:300,100:300]

然后你可以用255(白色)替换原始图像中的中心点值 img2[100:300,100:300] = 255

然后沿两个轴将 center_spots 压缩 50%,并将结果添加回 img2 压缩后的图像形状将为 (100,100),因此添加到 img2[150:250,150:250]

您似乎可以进行裁剪和提取,但只是停留在 compress 方面。因此,裁剪掉中间部分并将其保存为 im,我们将在下一步中对其进行压缩。用白色填充您裁剪的区域。

现在,压缩您裁剪的部分。为了减少50%,需要隔行隔列,所以:

# Generate a vector alternating between True and False the same height as "im"
a = [(i%2)==0 for i in range(im.shape[0])]

# Likewise for the width
b = [(i%2)==0 for i in range(im.shape[1])]

# Now take alternate rows with numpy.compress()
r = np.compress(a,im,0)

# And now take alternate columns with numpy.compress()
res = np.compress(b,r,1)

最后将 res 放回原始图像,相对于您剪切的位置偏移其宽度和高度的一半。

检查以下代码以获得您想要的输出。如果您需要对以下代码进行解释,请发表评论。

import os.path
from skimage.io import imread
from skimage import data_dir
import matplotlib.pyplot as plt
import numpy as np

img = imread(os.path.join(data_dir, 'phantom.png'))
img[np.less_equal(img[:,:,0],50)] = 0
img[np.greater_equal(img[:,:,0],51)] = 255

img_slice=img[100:300,100:200]
img_slice2=img[100:300,200:300]

img_slice=np.fliplr(img_slice)
img_slice2=np.fliplr(img_slice2)

img2=img.copy()
img2[100:300, 200:300]=img_slice 
img2[100:300, 100:200]=img_slice2

#extract the left and right images
img_left = img2[100:300,100:200]
img_right = img2[100:300,200:300]

#reduce the size of the images extracted using compress
#numpy.compress([list of states as True,False... or 1,0,1...], axis = (0 for column-wise and 1 for row-wise))
#In state list whatever is False or 0 that particular row should will be removed from that matrix or image
#note: len(A) -> number of rows and len(A[0]) number of columns

#reducing the  height-> axis  = 0
img_left = img_left.compress([not(i%2) for i in range(len(img_left))],axis = 0)
#reducing the  width-> axis  = 1
img_left = img_left.compress([not(i%2) for i in range(len(img_left[0]))],axis = 1)

#reducing the  height-> axis  = 0
img_right = img_right.compress([not(i%2) for i in range(len(img_right))],axis = 0)
#reducing the  width-> axis  = 1
img_right = img_right.compress([not(i%2) for i in range(len(img_right[0]))],axis = 1)

#clearing the area before pasting the left and right minimized images
img2[100:300,100:200] = 255 #255 is for whitening the pixel
img2[100:300,200:300] = 255

#paste the reduced size images back into the main picture(but notice the coordinates!)
img2[150:250,125:175] = img_left
img2[150:250,225:275] = img_right
plt.imshow(img2)

numpy.compress 文档 here.

eyes = copy[100:300,100:300]
eyes1 = eyes
e = [(i%2 == 0) for i in range(eyes.shape[0])]
f = [(i%2 == 0) for i in range(eyes.shape[1])]
eyes1 = eyes1.compress(e,axis = 0)
eyes1 = eyes1.compress(f,axis = 1)
# plt.imshow(eyes1)
copy[100:300,100:300] = 255
copy[150:250,150:250] = eyes1
plt.imshow(copy)