简体   繁体   English

Python opencv 最佳阈值

[英]Python opencv optimal thresholding

I am trying to detect roi in my image dataset using Otsu thresholding.我正在尝试使用 Otsu 阈值检测图像数据集中的 roi。 While in some cases results are on the point, some cases are not that good.虽然在某些情况下结果是正确的,但有些情况并不是那么好。 I'm using the code below.我正在使用下面的代码。

import cv2 as cv
import numpy as np

path  = "./images/{}.png".format

for num in range(1000):
    filename  = path(num)
    img       = cv.imread(filename, cv.IMREAD_GRAYSCALE)
    res       = cv.threshold(img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)[1]
    res       = np.hstack((img, res))
    cv.imwrite(filename.replace(".png", "_.png"), res)

While these results are acceptable虽然这些结果是可以接受的

在此处输入图像描述 在此处输入图像描述

These definitely need to be improved这些肯定需要改进

在此处输入图像描述 在此处输入图像描述

How can I improve my code?我怎样才能改进我的代码?

I suggest using the GRIP software to implement two image processing pipelines: One that performs thresholding to find the center point and another to find all the other circles.我建议使用GRIP 软件来实现两个图像处理管道:一个执行阈值处理以找到中心点,另一个执行查找所有其他圆圈。
These pipelines can then be generated to Python OpenCV code and you can import them into your code (which I have already done for you).然后可以将这些管道生成为 Python OpenCV 代码,您可以将它们导入到您的代码中(我已经为您完成了)。

Here is the center point pipeline: It performs a simle HSV threshold followed by a find blob command这是中心点管道:它执行简单的 HSV 阈值,然后执行查找 blob 命令
中心_管道


Here is the circle pipeline:这是圆形管道:
It performs a Laplacian transform (which is a one-sided Fourier Transform to detects the contrast changes which represent the edges of the circles), then Canny edge detection to find the edges of the circles, and then finds and filters the contours of the circles.它执行拉普拉斯变换(这是一个单边傅里叶变换来检测代表圆圈边缘的对比度变化),然后进行Canny边缘检测以找到圆圈的边缘,然后找到并过滤圆圈的轮廓. To find the number of circle, you can divide the number of contours by 2 (inner and outer circle associated with each Laplacian ring)要找到圆的数量,您可以将轮廓的数量除以 2(与每个拉普拉斯环相关联的内圆和外圆) 环形管道


Here is thelink to download the GRIP software这是下载 GRIP 软件的链接

Here is a link to all my files (including the auto-generated Python image processing pipeline)这是我所有文件的链接(包括自动生成的 Python 图像处理管道)

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

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