简体   繁体   English

如何在 opencv python 中获得低对比度图像的边缘

[英]How can I get the edges of low contrast image in opencv python

I'm trying to get the edges of this object from a TEM(microscope) image and the problem is that the contact is low especially in the upper edge, I tried several things thresholding, contrast equalization... but I wasn't able to get the upper edge.我试图从 TEM(显微镜)图像中获取这个 object 的边缘,问题是接触很低,特别是在上边缘,我尝试了几件事阈值,对比度均衡......但我无法获得上边缘。

NB: I'm trying to calculate the angle between the droplet and the tube I'm not sure if this is the best way to approach this problem.注意:我正在尝试计算液滴和管子之间的角度,我不确定这是否是解决这个问题的最佳方法。

The original image:原图:

在此处输入图像描述

The Canny Edge detection I get:我得到的 Canny Edge 检测:

在此处输入图像描述

the steps I got to get this result are:我得到这个结果的步骤是:

  1. Contrast enhancement对比度增强
  2. Thresholding阈值化
  3. Gauss filter高斯滤波器
  4. Canny Edge detection Canny 边缘检测

Code:代码:

clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=(grid_size, grid_size))
equ = clahe.apply(img)
val = filters.threshold_otsu(equ)
mask = img < val
# denoising part
mask = filters.gaussian(mask,sigma=sigmaG)
# edge detection
edge = feature.canny(mask,sigma=sigmaC)
edge = img_as_ubyte(edge)

We have this image and we want to detect the edges of the microphone:我们有这个图像,我们想要检测麦克风的边缘:

在此处输入图像描述

Basically, I converted the image to grayscale, added a Gaussian blur, and detected the edges using the canny edge detector.基本上,我将图像转换为灰度,添加了高斯模糊,并使用 canny 边缘检测器检测边缘。 One more important part is to fill in the gaps in the detected edges by dilating the edges and then eroding them.一个更重要的部分是通过扩大边缘然后侵蚀它们来填充检测到的边缘中的间隙。

All of the above is implemented in the process function;以上都是在process function中实现的; the draw_contours function basically utilizes the process function, and detects the greatest contour: draw_contours function 基本利用process function,检测最大轮廓:

import cv2
import numpy as np

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_blur = cv2.GaussianBlur(img_gray, (11, 11), 7)
    img_canny = cv2.Canny(img_blur, 0, 42)
    kernel = np.ones((19, 19))
    img_dilate = cv2.dilate(img_canny, kernel, iterations=4)
    img_erode = cv2.erode(img_dilate, kernel, iterations=4)
    return img_erode

def draw_contours(img):
    contours, hierarchies = cv2.findContours(process(img), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    cnt = max(contours, key=cv2.contourArea)
    peri = cv2.arcLength(cnt, True)
    approx = cv2.approxPolyDP(cnt, 0.004 * peri, True)
    cv2.drawContours(img, [approx], -1, (255, 255, 0), 2)

img = cv2.imread("image.jpg")
h, w, c = img.shape

img = cv2.resize(img, (w // 2, h // 2))
draw_contours(img)

cv2.imshow("Image", img)
cv2.waitKey(0)

Output: Output:

在此处输入图像描述

You can omit the drop by tweaking some values int the process function.您可以通过在process function 中调整一些值来省略掉线。 For example, the values例如,值

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_blur = cv2.GaussianBlur(img_gray, (11, 11), 10)
    img_canny = cv2.Canny(img_blur, 0, 38)
    kernel = np.ones((13, 13))
    img_dilate = cv2.dilate(img_canny, kernel, iterations=3)
    img_erode = cv2.erode(img_dilate, kernel, iterations=4)
    return img_erode

Output: Output:

在此处输入图像描述

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

相关问题 如何在python中勾勒低对比度对象? - How can I contour low-contrast objects in python? 使用python中的opencv在低对比度图像中检测regtangles以供tesseract读取 - Detect regtangles in a low contrast image using opencv in python for reading by tesseract 在 opencv python 中获取图像的亮度和对比度 - Get brightness and contrast of an image in opencv python 如何在 Python OpenCV 中增加图像的对比度 - How do I increase the contrast of an image in Python OpenCV 获取图像中每个通道的对比度 opencv python - Get contrast for each channel in an image opencv python 如何使用 opencv python 自动调整扫描图像的对比度和亮度 - How to auto adjust contrast and brightness of a scanned Image with opencv python 如何使用 OpenCV Python 在扫描图像中进行局部对比度增强 - How to do a localized Contrast Enhancement In a scanned Image Using OpenCV Python 如何使用 opencv python 调整亮度、对比度和振动度? - How do I adjust brightness, contrast and vibrance with opencv python? matplotlib中的低对比度图像(对比度拉伸)问题 - problems low contrast image(contrast stretching) in matplotlib Python:图像预处理 - 对低对比度图像进行阈值化和二值化以进行斑点检测 - Python: Image preprocessing - Thresholding and binarizing low contrast images for Blob Detection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM