英文:
Building nested mask from contours with openCV
问题
以下是翻译好的代码部分:
import cv2
import numpy as np
import matplotlib.pyplot as plt
test_im = cv2.imread("contours.png")
im_gray = cv2.cvtColor(test_im, cv2.COLOR_RGB2GRAY)
# 使用OpenCV查找轮廓和层次关系
cnts, hierarchy = cv2.findContours(im_gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = np.array(cnts)
mask = np.zeros_like(test_im)
# 使用cv2.fillPoly从轮廓绘制嵌套掩码
for i, cnt in enumerate(cnts):
# 寻找外部轮廓
if hierarchy[0][i][3] == -1:
cnt = cnt.reshape((cnt.shape[0], 2))
# 填充外部轮廓
cv2.fillPoly(mask, [cnt], 255)
# 寻找孙子轮廓并用零填充它们(得到嵌套掩码作为输出)
child_ix = hierarchy[0][i][2]
same_level_ix = hierarchy[0][child_ix][0]
# 由于某种原因,外部轮廓有两个子轮廓
# (根据我的理解,应该只有一个)
if same_level_ix == -1:
grandchild_ix = hierarchy[0][child_ix][2]
else:
child_ix = hierarchy[0][child_ix][2]
grandchild_ix = hierarchy[0][same_level_ix][2]
if grandchild_ix != -1:
cnt = cnts[grandchild_ix]
cnt = cnt.reshape((cnt.shape[0], 2))
cv2.fillPoly(mask, [cnt], 0)
same_level_ix = hierarchy[0][grandchild_ix][0]
while same_level_ix != -1:
cnt = cnts[same_level_ix]
cnt = cnt.reshape((cnt.shape[0], 2))
cv2.fillPoly(mask, [cnt], 0)
same_level_ix = hierarchy[0][same_level_ix][0]
希望对您有所帮助。如果您有任何其他问题,请随时提出。
英文:
I'd like to build a nested mask (mask with holes) from contours that I've drawn.
The input contour image is attached to this message - called contours.png -, and here is the code I used to build my nested mask.
import cv2
import numpy as np
import matplotlib.pyplot as plt
test_im = cv2.imread("contours.png")
im_gray = cv2.cvtColor(test_im, cv2.COLOR_RGB2GRAY)
# find contours and hierarchy with OpenCV
cnts, hierachy = cv2.findContours(im_gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = np.array(cnts)
mask = np.zeros_like(test_im)
# draw nested mask from contours using cv2.fillPoly
for i, cnt in enumerate(cnts):
# look for external contours
if hierachy[0][i][3] == -1:
cnt = cnt.reshape((cnt.shape[0], 2))
# fill the external contour entirely
cv2.fillPoly(mask, [cnt], 255)
# look for grandchild contours to fill them with zeros (and have a nested mask as output)
child_ix = hierachy[0][i][2]
same_level_ix = hierachy[0][child_ix][0]
# for an akward reason, the extrenal contour has two child contours
# (should get only one in my understanding)
if same_level_ix == -1:
grandchild_ix = hierachy[0][child_ix][2]
else:
child_ix = hierachy[0][child_ix][2]
grandchild_ix = hierachy[0][same_level_ix][2]
if grandchild_ix != -1:
cnt = cnts[grandchild_ix]
cnt = cnt.reshape((cnt.shape[0], 2))
cv2.fillPoly(mask, [cnt], 0)
same_level_ix = hierachy[0][grandchild_ix][0]
while same_level_ix != -1:
cnt = cnts[same_level_ix]
cnt = cnt.reshape((cnt.shape[0], 2))
cv2.fillPoly(mask, [cnt], 0)
same_level_ix = hierachy[0][same_level_ix][0]
Even if it works on this example, my code doesn't seem really robust. Additionaly, I found that the external contour has two child contours which is weird to me: should get only one in my understanding.
Do you have any better solution ?
Thanks for your help, have a nice day !
答案1
得分: 0
基于层次结构设置,您可以通过制作2个蒙版并相减来获得结果。第一个蒙版是通过填充最外层的轮廓而完成的,第二个蒙版是通过填充最内层的轮廓而完成的:
这是提取轮廓并填充它们所需的设置(代码是C ++,但设置与Python等效):
Mat img__1, img__2, img__ = imread("E:/img.jpg", 0);
threshold(img__, img__1, 0, 255, THRESH_BINARY);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(img__1, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE);
Mat tmp = Mat::zeros(img__1.size(), CV_8U);
Mat tmp2 = Mat::zeros(img__1.size(), CV_8U);
for (size_t i = 0; i < contours.size(); i++)
if (hierarchy[i][3] < 0)
drawContours(tmp, contours, i, Scalar(255, 255, 255), -1); # 第一个蒙版
for (size_t i = 0; i < contours.size(); i++)
if (hierarchy[i][2] < 0 && hierarchy[i][3] > -1)
drawContours(tmp2, contours, i, Scalar(255, 255, 255), -1); # 第二个蒙版
imshow("img", img__1);
imshow("first_mask", tmp);
imshow("second_mask", tmp2);
tmp = tmp - tmp2; # 相减两个蒙版以去除空洞
imshow("final image", tmp);
waitKey(0);
英文:
Based on the hierarchy settings, you can get the result by making 2 mask, and subtracting them. First mask is done by filling the outermost contour, and the second mask is done by filling the innermost contours:
Here is the necessary setting for extracting contours and filling them (the code in c++ but the settings are equivalent with python):
Mat img__1, img__2,img__ = imread("E:/img.jpg", 0);
threshold(img__, img__1, 0, 255, THRESH_BINARY);
vector<vector<Point>> contours;
vector< Vec4i > hierarchy;
findContours(img__1, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE);
Mat tmp = Mat::zeros(img__1.size(), CV_8U);
Mat tmp2 = Mat::zeros(img__1.size(), CV_8U);
for (size_t i = 0; i < contours.size(); i++)
if (hierarchy[i][3]<0)
drawContours(tmp, contours, i, Scalar(255, 255, 255), -1); # first mask
for (size_t i = 0; i < contours.size(); i++)
if (hierarchy[i][2]<0 && hierarchy[i][3]>-1)
drawContours(tmp2, contours, i, Scalar(255, 255, 255), -1); # second mask
imshow("img", img__1);
imshow("first_mask", tmp);
imshow("second_mask", tmp2);
tmp = tmp - tmp2; # subtracting the two masks to remove the holes
imshow("final image", tmp);
waitKey(0);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论