Opencv 图像处理、稀释、交集、补全 [英] Opencv Image processing, dilution, intersection, complement
问题描述
我正在尝试从我阅读过的研究中得出的一个过程,并遇到了这个过程.我曾尝试阅读所涉及的流程,但我似乎无法理解它.
I'm trying out a process from a research that I've read and came across this procedure. I have tried reading about the processes involved but I can't seem to wrap my head around it.
- 取二进制图像
I
- 创建一个灰度值
255
的标记图像F
除了沿边界的那些像素之外的所有像素,这些像素不是细胞图像中的对象像素,它是0
. - Dilate
F
byB
,一个5×5
掩码,所有像素的灰度值为0
.让这个膨胀的图像成为F ⊕ B
. - 取
I
和F⊕B
的补码的交集.让它成为H
. - 使
F
等于H
. - 重复上述步骤 3 到 5
t
次(实验-t 被视为1000
). - 取
I
的补码和补码的交集H
的注释.这给了我们孔的图像.让这成为G
. - 取
I
和G
的并集得到最终图像,没有非外围孔.
- Take the binary image
I
- Create a marker image
F
which has gray value255
in all pixels except for those pixels along the boundary which are not object pixels in the cell image, where it is0
. - Dilate
F
byB
, a5×5
mask that has gray value0
in all pixels. Let this dilated image beF ⊕ B
. - Take intersection of complement of
I
andF ⊕ B
. Let this beH
. - Make
F
equal toH
. - Repeat the above steps 3 to 5 for
t
times (experimen- tally, t is taken as1000
). - Take intersection of complement of
I
and comple- ment ofH
. This gives us the image of holes. Let this beG
. - Take union of the
I
andG
to get the final image, which is free of non peripheral holes.
这是他们过程的结果:
我想使用这个二进制图像获得相同的结果:
I wanted to have the same result using this binary image:
谁能解释一下完整的过程并达到相同的结果.
Can someone please explain the thorough process and achieve the same result.
这是我目前所在的位置:
This is where I'm currently at:
# LOAD IMAGE
img = cv2.imread('resources/rbc2.png')
# CONVERT TO GRAYSCALE
imgGray = cv2.cvtColor(imgBrightness, cv2.COLOR_BGR2GRAY)
# APPLY MEDIAN BLUR
medianImg = cv2.medianBlur(imgGray,9)
# OTSU THRESHOLDING
ret, otsu = cv2.threshold(medianImg,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)
complimentI = cv2.bitwise_not(otsu)
推荐答案
如果您只想填充掩码中的孔,我们可以通过使用 opencv 的 findContours 更简单地做到这一点.我们可以过滤小轮廓并在蒙版上填充这些轮廓.
If all you're looking to do is fill holes in a mask, we can do that much more simply by using opencv's findContours. We can filter for small contours and fill in those contours on the mask.
我使用的是 Opencv 3.4.如果您使用的是 Opencv 2.* 或 4.*,则 findContours 返回 2 个参数,并且应如下所示:
I am using Opencv 3.4. If you are using Opencv 2.* or 4.* then findContours returns 2 arguments and should look like this:
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE);
填充面膜
import cv2
# load image
gray = cv2.imread("blobs.png", cv2.IMREAD_GRAYSCALE);
# mask with otsu
_, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU);
# find contours
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE);
# filter out contours by size
small_cntrs = [];
for con in contours:
area = cv2.contourArea(con);
if area < 1000: # size threshold
small_cntrs.append(con);
cv2.drawContours(mask, small_cntrs, -1, (0), -1);
# show
cv2.imshow("mask", mask);
cv2.waitKey(0);
这篇关于Opencv 图像处理、稀释、交集、补全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!