[英]How can I get the edges of low contrast image in opencv python
我試圖從 TEM(顯微鏡)圖像中獲取這個 object 的邊緣,問題是接觸很低,特別是在上邊緣,我嘗試了幾件事閾值,對比度均衡......但我無法獲得上邊緣。
注意:我正在嘗試計算液滴和管子之間的角度,我不確定這是否是解決這個問題的最佳方法。
原圖:
我得到的 Canny Edge 檢測:
我得到這個結果的步驟是:
代碼:
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)
我們有這個圖像,我們想要檢測麥克風的邊緣:
基本上,我將圖像轉換為灰度,添加了高斯模糊,並使用 canny 邊緣檢測器檢測邊緣。 一個更重要的部分是通過擴大邊緣然后侵蝕它們來填充檢測到的邊緣中的間隙。
以上都是在process
function中實現的; 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:
您可以通過在process
function 中調整一些值來省略掉線。 例如,值
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:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.