OpenCV:检测清晰描绘的对象边界失败

slhcrj9b  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(141)

我有以下图片:


的数据
我想检测图像中放在table上的文档的边缘。我尝试了以下代码(前面的方法已被注解掉)。

  1. def detectDocEdge(imagePath):
  2. image = loadImage(imagePath)
  3. gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  4. # Approach 1
  5. # equalized_image = cv2.equalizeHist(gray_image)
  6. # blurred = cv2.GaussianBlur(equalized_image, (15, 15), 0) # TODO: May not be required
  7. # edges = cv2.Canny(blurred, 5, 150, apertureSize=3)
  8. # kernel = np.ones((15, 15), np.uint8)
  9. # dilated_edges = cv2.dilate(edges, kernel, iterations=1)
  10. # Approach 2
  11. # blurred = cv2.GaussianBlur(gray_image, (5, 5), 0)
  12. # thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
  13. # kernel = np.ones((5, 5), np.uint8)
  14. # dilated_edges = cv2.dilate(thresh, kernel, iterations=1)
  15. # Approach 3
  16. # Apply morphological operations
  17. # kernel = np.ones((3, 3), np.uint8)
  18. # closed_image = cv2.morphologyEx(thresholded_image, cv2.MORPH_CLOSE, kernel)
  19. # Approach 4
  20. blurred = cv2.GaussianBlur(gray_image, (5, 5), 0)
  21. edges = cv2.Canny(blurred, 50, 150)
  22. dilated = cv2.dilate(edges, None, iterations=5)
  23. eroded = cv2.erode(dilated, None, iterations=5)
  24. contours, hierarchy = cv2.findContours(eroded, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  25. contour_img = image.copy()
  26. cv2.drawContours(contour_img, contours, -1, (0, 255, 0), 2)
  27. showImage(contour_img)
  28. max_contour = None
  29. max_measure = 0 # This could be area or combination of area and perimeter
  30. max_area = 0
  31. for contour in contours:
  32. area = cv2.contourArea(contour)
  33. perimeter = cv2.arcLength(contour, True)
  34. # Here we use a combination of area and perimeter to select the contour
  35. measure = area + perimeter # You can also try different combinations
  36. # if measure > max_measure:
  37. # max_measure = measure
  38. # max_contour = contour
  39. if area > max_area:
  40. max_area = area
  41. max_contour = contour
  42. contour_img = image.copy()
  43. cv2.drawContours(contour_img, [max_contour], -1, (0, 255, 0), 2)
  44. showImage(contour_img)

字符串
这些方法似乎都不起作用,我总是得到以下优势:

你能建议一个修复?我需要尽可能通用的代码,以便它可以在尽可能多的条件下检测文档边缘。

xwbd5t1u

xwbd5t1u1#

我可以通过缩小图像和放大轮廓来获得文档周围的轮廓。

  1. import cv2
  2. import numpy as np
  3. def detectDocEdge(imagePath):
  4. image = cv2.imread(imagePath, cv2.IMREAD_COLOR)
  5. scale_factor = .25
  6. small_image = cv2.resize(image, None, fx=scale_factor, fy=scale_factor)
  7. gray = cv2.cvtColor(small_image, cv2.COLOR_BGR2GRAY)
  8. edges = cv2.Canny(gray, 50, 150)
  9. kernel = np.ones((5, 5), np.uint8)
  10. edges = cv2.dilate(edges, kernel, iterations=1)
  11. contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  12. contour = sorted(contours, key=cv2.contourArea, reverse=True)[0]
  13. contour[:, :, 0] = contour[:, :, 0] / scale_factor
  14. contour[:, :, 1] = contour[:, :, 1] / scale_factor
  15. contour_img = image.copy()
  16. cv2.drawContours(contour_img, [contour], -1, (0, 255, 0), 10)
  17. cv2.namedWindow('Image', cv2.WINDOW_NORMAL)
  18. cv2.imshow('Image', contour_img)
  19. cv2.waitKey(0)
  20. cv2.destroyAllWindows()

字符串


的数据

展开查看全部

相关问题