📜  Mahotas – 删除给定位置的区域

📅  最后修改于: 2022-05-13 01:54:20.226000             🧑  作者: Mango

Mahotas – 删除给定位置的区域

在本文中,我们将了解如何在 mahotas 中删除给定位置的区域。标记图像是整数图像,其中值对应于不同区域。即,区域 1 是具有值 1 的所有像素,区域 2 是具有值 2 的像素,依此类推。按照惯例,区域 0 是背景,通常以不同方式处理。我们可以借助 mahotas.label 方法创建一个带标签的区域。
为此,我们将使用 mahotas.labelled.remove_regions 方法

示例 1:

Python3
# importing required libraries
import mahotas
import numpy as np
from pylab import imshow, show
import os
 
 
# loading image
img = mahotas.imread('dog_image.png')
     
 
# setting filter to the image
img = img[:, :, 0]
 
 
# setting gaussian filter
img = mahotas.gaussian_filter(img, 15)
 
# setting threshold value
img = (img> img.mean())
 
# creating a labelled image
labeled1, n_nucleus1 = mahotas.label(img)
 
# showing the labelled image
print("Labelled Image")
imshow(labelled1)
show()
 
 
 
# removing region
labelled2 = mahotas.labelled.remove_regions(labelled1, 1, 1)
 
 
# showing the labelled image
print("Labelled Image with removed region")
imshow(labelled2)
show()


Python3
# importing required libraries
import mahotas
import numpy as np
from pylab import imshow, show
  
# creating region
# numpy.ndarray
regions = np.zeros((10, 10), bool)
  
# setting 1 value to the region
regions[:3, :3] = 1
regions[6:, 6:] = 1
  
# getting labelled function
labelled, nr_objects = mahotas.label(regions)
  
print("Labelled Image")
# showing the image with interpolation = 'nearest'
imshow(labelled, interpolation ='nearest')
show()
  
# removing region
labelled2 = mahotas.labelled.remove_regions(labelled, 1, 1)
 
 
# showing the labelled image
print("Labelled Image with removed region")
imshow(labelled2)
show()


输出 :

示例 2:

Python3

# importing required libraries
import mahotas
import numpy as np
from pylab import imshow, show
  
# creating region
# numpy.ndarray
regions = np.zeros((10, 10), bool)
  
# setting 1 value to the region
regions[:3, :3] = 1
regions[6:, 6:] = 1
  
# getting labelled function
labelled, nr_objects = mahotas.label(regions)
  
print("Labelled Image")
# showing the image with interpolation = 'nearest'
imshow(labelled, interpolation ='nearest')
show()
  
# removing region
labelled2 = mahotas.labelled.remove_regions(labelled, 1, 1)
 
 
# showing the labelled image
print("Labelled Image with removed region")
imshow(labelled2)
show()

输出 :