gpt4 book ai didi

python - 使用 Canny edge 创建 mask - 已更新

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:53 24 4
gpt4 key购买 nike

我想在提供的图像中使用查找卷心菜。我已经有一个使用颜色阈值处理的前一个问题的实现,但是它需要我手动输入 HSV 或 RGB 值,我需要一种自适应的阈值处理方式,并考虑使用 canny edge 来查找边缘然后创建一个蒙版。

下面是 color thesholding 的实现,这是 canny 所需的输出。

Sample image

Masked

Using find contours

# Import the necessary packages
import numpy as np
import argparse
import cv2
import glob

def auto_canny(image, sigma=0.33):
# compute the median of the single channel pixel intensities
v = np.median(image)

# apply automatic Canny edge detection using the computed median
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(image, lower, upper)

# return the edged image
return edged

# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
cv2.imshow("Image", image)


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)

auto = auto_canny(blurred)

cv2.imshow("Image", auto)
cv2.imwrite("newimage1.jpg", auto)



(_, cnts, _) = cv2.findContours(auto.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

if len(cnts) > 0:
# sort the contours and find the largest one -- we
# will assume this contour correspondes to the area
# of my phone
cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
cv2.drawContours(image, [cnt], -1, (0, 255, 0), 2)


cv2.imshow("Tracking", image)
cv2.imwrite("newimage2.jpg", image)
cv2.waitKey(0)

cv2.waitKey(0)

结果:

Canny Output Outcome

我的想法是使用 canny 找到边缘,然后使用 findcontours 获得最大的轮廓并创建一个应该是卷心菜的蒙版。但是,这似乎不起作用,因为 canny 输出的结果有很多优势。

我认为我应该在应用 canny 边缘检测之前做一些预处理,但我不太确定应用什么技术进行预处理。

编辑:

通读了一些建议并尝试了那些我知道该怎么做的建议,首先我将其转换为 HSV 并将图像拆分为相应的 H、s 和 v。实现了 2 种方法,结果如下,任何建议如何改进?

# Import the necessary packages
import numpy as np
import argparse
import cv2
import glob

def auto_canny(image, sigma=0.33):
# compute the median of the single channel pixel intensities
v = np.median(image)

# apply automatic Canny edge detection using the computed median
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(image, lower, upper)

# return the edged image
return edged

# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
cv2.imshow("Image", image)
newImage = image.copy()

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
blurred = cv2.GaussianBlur(hsv, (3, 3), 0)

#cv2.imshow("HSV image", blurred)

#now to seperate and only extract hue image
h,s,v = cv2.split(blurred)

cv2.imshow("H", h)
#cv2.imshow("S", s)
#cv2.imshow("V", v)

thresh = cv2.adaptiveThreshold(h, 255,
cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 4)
cv2.imshow("adaptive1", thresh)
cv2.imwrite("adaptive1.jpg", thresh)

(_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

auto = auto_canny(h)
cv2.imshow("canny", auto)
cv2.imwrite("canny1.jpg", auto)


(_, cnts2, _) = cv2.findContours(auto.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

if len(cnts) > 0:
# sort the contours and find the largest one -- we
# will assume this contour correspondes to the area
# of my phone
cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
cv2.drawContours(image, [cnt], -1, (0, 255, 0), 2)

cv2.imshow("adaptive2", image)
cv2.imwrite("adaptive2.jpg", image)

if len(cnts2) > 0:
# sort the contours and find the largest one -- we
# will assume this contour correspondes to the area
# of my phone
cnt = sorted(cnts2, key = cv2.contourArea, reverse = True)[0]
cv2.drawContours(newImage, [cnt], -1, (0, 255, 0), 2)

cv2.imshow("canny2", newImage)
cv2.imwrite("canny2.jpg", newImage)

cv2.waitKey(0)

自适应:

adaptive

contour

精明:

canny

contour

最佳答案

您还可以在色彩空间中设置图像的 3d 直方图,如果您知道目标是场景中的主要对象,则可以在算法上围绕原色空间设置边界(聚类),然后使用它分割。这可能是我想要的。

关于python - 使用 Canny edge 创建 mask - 已更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46042960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com