opencv如何填充阈值图像(一些白色部分)?

发布于 2025-01-18 03:05:06 字数 992 浏览 2 评论 0原文

我创建了一个脚本来阈值微观图像,我想知道如何填充图像的某些白色空间。如您在图片中所见,我的目的是制作一个过滤器,以仅分开蓝色区域。应将所有其他白色区域(红色箭头)擦除或转换为黑色。

这是我的代码:

original = cv2.imread(inputfile + ".jpg")
#cv2.imread(inputfile + ".jpg")
original = cv2.resize(original, (1280, 960)) #resize of original image
img = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
median = cv2.medianBlur(img, 3)
ret, th = cv2.threshold(median, 160, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
kernel = np.ones((1,1), np.uint8)
opening = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel)
edge_touching_removed = clear_border(opening)
contours, hierarchy = cv2.findContours(edge_touching_removed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

事实是...应用此阈值后,我将在蓝色区域应用轮廓以使区域陷入困境。没关系...但是我不知道如何擦除其他区域。

我应该导出该阈值图像,然后手动绘画?我不想这样做...我在想也许有手动填写代码。我使用了CV2形态,但我不知道我是否需要为此进行另一个过滤器。

谢谢

I<ve created a script to threshold a microscopic image and I would like to know how to fill some white spaces of the image. As you see in the picture my aim is to make a filter to separate only the blue zones. All other white zones (red arrow) should be erased or converted (filled out) to black.

Here is my code:

original = cv2.imread(inputfile + ".jpg")
#cv2.imread(inputfile + ".jpg")
original = cv2.resize(original, (1280, 960)) #resize of original image
img = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
median = cv2.medianBlur(img, 3)
ret, th = cv2.threshold(median, 160, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
kernel = np.ones((1,1), np.uint8)
opening = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel)
edge_touching_removed = clear_border(opening)
contours, hierarchy = cv2.findContours(edge_touching_removed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

The thing is ... after apply this threshold I will apply contours to the blue zone to caculate area. That is ok... but the thing is I don<t know how to erase the others zones.

Should I export that threshold image and I painted manually? I dont wanna do that... I was thinking that maybe there is a manual fill out code. I used the cv2 morphology but I dont know if I need to do another filter for that.

Thanks

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文