1

i'm trying to detect a card, but the problem is that sometimes the image is not good and has several backgrounds, like this:

Not well define edges

![Not well define edges][1]

Example background

![Example background][2]

I did this:

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

edg = cv2.Canny(gray, 10, 20)
contours,_ = cv2.findContours(edg.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image,contours,-1,[0,255,0],2)

cv2.imshow('image',image)
cv2.waitKey(0)

But sometimes he detects other stuff, and not the card. Anyone has ideias how to solve this? I've tried Object Detection with YOLO, but it's hard

tentativa123
  • 21
  • 1
  • 6

1 Answers1

1

first of all, note that, there are some condition like light condition and medium condition in take photo which if you can control them, the image processing section load will decreases. for example in your example image, you can put A4 white paper in the background to reduce small contours and so on(Of course it's impossible to change condition).

Well, i try on your test image with this code :

import cv2

rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (17, 17))

img = cv2.imread('edge.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


gradX = cv2.Sobel(gray, ddepth=cv2.CV_32F, dx=1, dy=0,
    ksize=-1)

morph1 = cv2.morphologyEx(gradX, cv2.MORPH_OPEN, rectKernel)
morph2 = cv2.morphologyEx(morph1, cv2.MORPH_CLOSE, sqKernel)


cv2.imshow("img",img)
cv2.imshow("gradx",gradX)
cv2.imshow("tophat",morph1)
cv2.imshow("tophat2",morph2)
cv2.waitKey()

here is the results:

Sobel morph1 morph2

You can use contours and remove unwanted contours using contour properties : Contour Properties

Peyman habibi
  • 680
  • 1
  • 6
  • 17