繁体   English   中英

如何在图像中找到区域最大值/最小值?

[英]How to find regional maxima/minima in images?

我正在尝试在此图中找到区域最大值:

在此处输入图片说明

切成这样的位置:

在此处输入图片说明

我在这里找到了一种方法来过滤区域最大值但是我无法使其适用于我的情况。

到目前为止,我的代码:

import numpy as np
import cv2
import skimage as sm
from skimage.morphology import reconstruction
import scipy as sp

img = cv2.imread('img.png', 0)

img = sm.img_as_float(img)
img = sp.ndimage.gaussian_filter(img, 1)

seed = np.copy(img)
seed[1:-1,1:-1] = img.min()
mask = img
dilated = reconstruction(seed, mask, method = 'dilation')
img = img - dilated

cv2.imshow('img', img)
cv2.waitKey()

我的解决方案:

import numpy as np
import cv2

img = cv2.imread('img.png', 0)

_, thresh = cv2.threshold(img, 250, 255, cv2.THRESH_BINARY)

rows = np.sum(thresh/255, axis = 1)
ol = len(np.nonzero(rows)[0])
L = []
z = 0
for idx, row in enumerate(rows):
    if row > 0:
        if z > 5 and z < ol - 5:
            L.append(idx)
        z += 1
split = np.min(rows[L])
thresh[np.where(rows == split)[0][0]] = 0

cv2.imshow('img', thresh)
cv2.waitKey()

HansHirse写了一个更专业的方法:

import numpy as np
import cv2

img = cv2.imread('img.png', 0)

_, thresh = cv2.threshold(img, 250, 255, cv2.THRESH_BINARY)

rows = np.sum(thresh/255, axis = 1)
exclude = 5
idx = np.where(rows > 0)[0]
idx = idx[exclude : len(idx) - exclude]
cut = idx[np.argmin(rows[idx])]
thresh[cut] = 0

cv2.imshow('img', thresh)
cv2.waitKey()

两者都导致:

在此处输入图片说明

看到一种不限于水平像素的方法会很有趣。

如果您的“染色单体”(我将以这种方式引用所示的结构,因为它看起来像一个)都以这种方式对齐,则您可以简单地计算每行的白色像素并搜索最小值。

请看下面的代码,希望它是不言自明的:

import cv2
import numpy as np

# Load input image
input = cv2.imread('images/Q6YM9.png', cv2.IMREAD_GRAYSCALE)

# Extract "chromatid" (the structure looks like one...)
_, chromatid = cv2.threshold(input, 250, 255, cv2.THRESH_BINARY)

# Sum row-wise pixel values
rowPixelSum = np.sum(chromatid / 255, axis=1)

# Detect all rows with non-zero elements
ind = np.where(rowPixelSum > 0)[0]

# Exclude n rows at the top and bottom of the "chromatid"
# Caveat: Check for plausibility (index out of bounds, etc.)
nEx = 15
ind = ind[15:len(ind)-nEx]

# Detect index of row with minimum pixel count
cutRow = ind[np.argmin(rowPixelSum[ind])]

# Detect start and end of "chromatid" on row with minimum pixel count
row = np.where(chromatid[cutRow, :] > 0)[0]
xStart = row[0]
xEnd = row[-1]

# For visualization: Draw black line through row with minimum pixel count
cv2.line(input, (xStart, cutRow), (xEnd, cutRow), 0, 3)
cv2.line(chromatid, (xStart, cutRow), (xEnd, cutRow), 0, 3)

# Write output image
cv2.imwrite('images\input.png', input)
cv2.imwrite('images\chromatid.png', chromatid)

输出看起来像这样:

染色单体

输入项

如果您的“染色单体”具有不同的方向,则可以根据染色单体的“主要成分”在上述代码之前利用一些旋转。

希望有帮助!

您可以在过滤区域最大值后尝试使用形态学打开操作将这两部分分开。 使用更大的内核或多次打开调用,具体取决于您的区域最小厚度。

要找到开口的确切切割位置,您可以执行多个后续的打开操作调用,直到单个斑点分裂为两个。 您可以检测到通过cv::detectContours()获得的轮廓分析位置。

另外,您可能会发现distanceTransform()有用。 其结果是从每个点到最近边界的距离。 这个想法是做图像骨架化,并沿着骨架线取distanceTransform()结果的最小值以找到切割位置。

您还可以尝试根据白色像素位置使用k = 2的k均值聚类。 切割线将在群集之间。

编辑:您可能会发现此页面有用,因为那里的人们讨论了类似的问题。 答案之一是使用cv::convexityDefects()查找分离点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM