[英]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:我得到这个结果的步骤是:
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.