问题描述
我想在我的项目数据集中删除图像的模糊背景,并且我已经在 here 中使用 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,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,iterations = 5);
crop_mask = cv2.dilate(crop_mask,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
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)