Python opencv optimal thresholding

人盡茶涼 提交于 2021-01-29 07:20:08

问题


I am trying to detect roi in my image dataset using Otsu thresholding. 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?


回答1:


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.
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).

Here is the center point pipeline: It performs a simle HSV threshold followed by a find blob command


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. To find the number of circle, you can divide the number of contours by 2 (inner and outer circle associated with each Laplacian ring)


Here is the link to download the GRIP software

Here is a link to all my files (including the auto-generated Python image processing pipeline)



来源:https://stackoverflow.com/questions/64272269/python-opencv-optimal-thresholding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!