【问题标题】:How to reduce image portion with numpy.compress method ? (numpy+scikit-image)如何使用 numpy.compress 方法减少图像部分? (numpy+scikit-image)
【发布时间】:2020-01-01 15:17:46
【问题描述】:

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

将中心点的大小压缩 50% 并绘制最终图像。

这些是我之前做的步骤。

我正在阅读图片

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

这是最终(“压缩”)练习之前的结果图像:

然后要求我使用numpy.compress 方法“减少”黑点。

使用“压缩”方法后的预期结果是以下图像(截图),其中黑点减少了 50%:

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

我将不胜感激任何有关 numpy.compress 方法如何解决此问题的帮助/解释,即使为此使用它是可行的。

【问题讨论】:

  • 我不明白您使用的是哪种compress 方法,但是您是否尝试过仅用两个黑点切出图像的中心部分,“压缩”并放回原图?
  • @Ardweaden 抱歉,我说的是 numpy compress 方法,可能是由于要求使用 numpy.compress 但不确定该练习没有得到很好的解释或不正确。我尝试只压缩切片的点,但无法得到最终结果

标签: python-3.x numpy scikit-image


【解决方案1】:

你似乎对裁剪和提取没问题,但只是停留在 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 放回原始图像中,相对于剪切位置偏移一半的宽度和高度。

【讨论】:

    【解决方案2】:

    我想您可以先通过以下方式切掉中心点: 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]

    【讨论】:

      【解决方案3】:

      检查以下代码以获得所需的输出。如果您需要解释以下代码,请评论。

      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

      【讨论】:

        【解决方案4】:
        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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-26
          • 2017-04-22
          • 2022-10-18
          • 2020-12-01
          • 1970-01-01
          • 2018-01-30
          • 2020-05-03
          • 1970-01-01
          相关资源
          最近更新 更多