image-segmentation - 在 Canny 边缘检测中应用自适应阈值
问题描述
我想在我的项目数据集中删除图像的模糊背景,我已经在这里使用 Canny 边缘检测获得了一个非常好的解决方案。我想对 Canny 的双阈值要求应用自适应阈值。我很感激这方面的任何帮助。
imageNames = glob.glob(r"C:\Users\Bikir\Pictures\rTest\*.jpg")
count=0
for i in imageNames:
img = Image.open(i)
img = np.array(img)
# grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# canny - I want this two values (0 and 150) to be adaptive in this case
canned = cv2.Canny(gray, 0, 150)
# dilate to close holes in lines
kernel = np.ones((3,3),np.uint8)
mask = cv2.dilate(canned, kernel, iterations = 1);
# find contours
# Opencv 3.4, if using a different major version (4.0 or 2.0), remove the first underscore
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE);
# find the biggest contour
biggest_cntr = None;
biggest_area = 0;
for contour in contours:
area = cv2.contourArea(contour);
if area > biggest_area:
biggest_area = area;
biggest_cntr = contour;
# draw contours
crop_mask = np.zeros_like(mask);
cv2.drawContours(crop_mask, [biggest_cntr], -1, (255), -1);
# opening + median blur to smooth jaggies
crop_mask = cv2.erode(crop_mask, kernel, iterations = 5);
crop_mask = cv2.dilate(crop_mask, kernel, iterations = 5);
crop_mask = cv2.medianBlur(crop_mask, 21);
# crop image
crop = np.zeros_like(img);
crop[crop_mask == 255] = img[crop_mask == 255];
img = im.fromarray(crop)
img.save(r"C:\Users\Bikir\Pictures\removed\\"+str(count)+".jpg")
count+=1
解决方案
推荐阅读
- java - 如何从 openapi 3获取 ResponseType
- php - 如何检查文件名和路径与其完整命名空间不匹配的 PHP 类(区分大小写)
- javascript - JSON 响应输出不会在单击事件时更新
- clojure - Clojure:指定几个约束;被迫“爆发”成谓词。有更好的办法吗?
- python-3.x - 不知道如何解决这个循环导入
- ios - main.jsbundle 文件显示在我的 iOS 项目中,但仍然抛出“No bundle url present”
- string - 在字符串表中搜索子字符串?
- php - Eclipse 2019-06 PHP 自动完成(内容辅助)不起作用
- typescript - 没有使用 io-ts 进行未定义验证的数组
- mysql - 加入一个表,右表是来自左表的列值的连接名称