我正在尝试从图像中分割出组织斑点。我进行了一些初步的预处理,并获得了以下结果。我担心的是边界上的噪音。如果我使用水平/垂直内核进行侵蚀,那么我也会在中间释放一些数据。我不确定哪种方法能更好地获得结果,还是应该采用其他方法进行细分。
这是示例图片:
import numpy as np
import os
import matplotlib.pyplot as plt
from skimage import io
from skimage.filters import threshold_mean
from skimage.exposure import adjust_sigmoid, adjust_gamma
from skimage.morphology import opening
from skimage import morphology
import scipy.ndimage as ndi
def create_binary_mask(path_to_file):
file = io.imread(path_to_file)
#APPLY FILTERS FOR BETTER THRESHOLD
img_med = ndi.median_filter(file, size=20) #REDUCE NOISE
adjusted_gamma = adjust_gamma(img_med, 1.8, 2) #INCREASE GAMMA
adjusted_sigmoid = adjust_sigmoid(adjusted_gamma, 0.01) #INCREASE CONTRAST
#DO THE MEAN THRESHOLD
thresh = threshold_mean(adjusted_sigmoid)
binary = adjusted_sigmoid > thresh
#REMOVE SMALL NOISE WITHIN THE IMAGE
disk = morphology.disk(radius=7)
opening = morphology.binary_opening(binary, disk)
fig, axis = plt.subplots(1,4, figsize=(10,10))
axis[0].imshow(file, cmap='gray')
axis[0].set_title('Original File')
axis[1].imshow(adjusted_sigmoid, cmap='gray')
axis[1].set_title('Adjusted Sigmoid')
axis[2].imshow(binary, cmap='gray')
axis[2].set_title('Mean Threshold')
axis[3].imshow(opening, cmap='gray')
axis[3].set_title('After opening')
plt.savefig('results.png')
plt.show()
path_to_file = "sample_img.png"
create_binary_mask(path_to_file)
参考方案
一种简单的方法是对二进制图像执行形态学闭合,以将噪声连接在一起成为单个轮廓。从这里我们可以找到轮廓并使用轮廓区域进行过滤。我们可以通过绘制轮廓来有效消除边框上的噪点。这是将噪声与背景色一起着色的结果。如果要在面罩上将其移除,则只需用黑色(0,0,0)
进行涂漆即可。
import cv2
import numpy as np
# Load image, grayscale, Gaussian blur, adaptive threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,61,5)
# Morph close to connect noise
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=5)
# Filter using contour area and fill in contour to remove noise
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area > 250000:
cv2.drawContours(image, [c], -1, (43,43,43), -1)
cv2.imwrite('image.png', image)
cv2.waitKey()
Python:图像处理可产生皱纹纸效果 - python也许很难描述我的问题。我正在寻找Python中的算法,以在带有某些文本的白色图像上创建皱纹纸效果。我的第一个尝试是在带有文字的图像上添加一些真实的皱纹纸图像(具有透明度)。看起来不错,但副作用是文本没有真正起皱。所以我正在寻找更好的解决方案,有什么想法吗?谢谢 参考方案 除了使用透明性之外,假设您有两张相同尺寸的图像,一张在皱纹纸上明亮,一张在白色背景上有深…
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
Python exchangelib在子文件夹中读取邮件 - python我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
Python ThreadPoolExecutor抑制异常 - pythonfrom concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…