0

I'm able to detect keypoints and descriptors

keypoints, des = surf.detectAndCompute(gray_image,None)

I'm able to draw keypoints

output_image = cv2.drawKeypoints(output_image, keypoints)

I'm able to locate keypoints from the image below that only correspond to text perfectly. I want to them copy the text onto a new image.

How can I draw only the objects the keypoints refer to?

source image

Here is the output of the keypoints keypoints only

Ultimately i want the keypoints only image to have the text instead of the keypoint markers

6
  • For that you may need to identify the object in the context as well. Then if the keypoint is within the detected object, you can highlight that particular object Commented Feb 15, 2017 at 16:22
  • How do you identify the object in the context? Commented Feb 15, 2017 at 16:26
  • Can you upload a sample image you are working with Commented Feb 15, 2017 at 16:31
  • is your question about segmentation of the object from the rest of image? , or just get the focus on the part where the key-points are.. Commented Feb 15, 2017 at 16:31
  • I'm able to extract keypoints referring to text. I just want to be able to create a new image consisting only of text as matched by the keypoints. Commented Feb 15, 2017 at 16:36

1 Answer 1

2

Do have a look at this approach. It is not so tidy but you can possibly refine it to suit your needs.

I obtained keypoints on the given image using cv2.ORB_create(), for which I obtained this:

img = cv2.imread(filename,0)
orb = cv2.ORB_create()
kp = orb.detect(img,None)
kp, des = orb.compute(img, kp)

img2 = cv2.drawKeypoints(img,kp,None,color=(0,255,0), flags=0)
cv2.imshow('keypoint_on_text.jpg', img2)

enter image description here

You can use surf for better detection of keypoints.

Then I obtained an image having a solid color(black in this case) having the same shape as that of the image. I drew these obtained keypoints on this black image.

mask = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
mask[:] = (0, 0, 0) 

fmask = cv2.drawKeypoints(mask,kp,None,color=(0,255,0), flags=0)
cv2.imshow('fmask.jpg', fmask)

enter image description here

Now I converted this to grayscale image and applied threshold to binarize it. I then found contours on this image and drew them with a bigger radius.

graymask = cv2.cvtColor(fmask,cv2.COLOR_BGR2GRAY)
ret, th = cv2.threshold(graymask, 50, 255, 0)
_, contours , _= cv2.findContours(th,2,1)
rep = cv2.drawContours(fmask, contours, -1, (0,255,0), 5)
cv2.imshow('contours.jpg',rep)

enter image description here

I converted this to greyscale, binarized it and masked it with the original image to finally obtain this:

repmask = cv2.cvtColor(rep,cv2.COLOR_BGR2GRAY)
ret, th1 = cv2.threshold(repmask, 50, 255, 0)
res = cv2.bitwise_and(img,img,mask = th1)
cv2.imshow('Only_Text.jpg',res)

enter image description here

As you can see, certain portions of the desired text is visible. If you use surf detection, you will be able to obtain more portion of text.

Sign up to request clarification or add additional context in comments.

8 Comments

If you require help with coding, do reach out. I hope this helps. There may be better ways out there. But this is what struck me first. :D
Can you provide how you did: "I converted this to greyscale, binarized it and masked it with the original image to finally obtain this"?
Check the solution again. I have added relevant code.
graymask = cv2.cvtColor(fmask,cv2.COLOR_BGR2GRAY) ret, th = cv2.threshold(graymask, 50, 255, 0) _, contours , _= cv2.findContours(th,2,1) isn't finding any contours, i'm using 2.4 so my syntax is contours, hierarchy= cv2.findContours(th,2,1) but still no luck
the error I get: repmask = cv2.cvtColor(rep, cv2.COLOR_BGR2GRAY) cv2.error: /tmp/opencv-20170215-44295-5rgz56/opencv-2.4.13.2/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.