我试图找到这张图片中的区域最大值:
像这样在其位置上进行切割:
我找到了一种过滤区域最大值的方法 here但我无法让它适用于我的情况。
到目前为止我的代码:
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)
输出看起来像这样:
如果您的“染色单体”具有不同的方向,则可以根据染色单体的“主要成分”在上述代码之前使用一些旋转。
希望对您有所帮助!
关于python - 如何在图像中找到区域最大值/最小值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55952498/