r/computervision • • Mar 14 '22

Help: Project ideas for improving pen tracking algorithm?

202 Upvotes

38 comments sorted by

15

u/Mega_Mandarin Mar 14 '22

I have video footage showing 4000 Chinese characters being written, and I would like to precisely track the tip of the pen as it moves about on the page. The posted video shows what I've been able to achieve so far, but it's still too jittery. The steps my code takes for each frame are:

  1. Apply gaussian blur and canny transform.

  2. Detect areas showing blue ink on the page using HSV threshold values.

  3. Subtract these blue ink areas from the canny transform results (red pixels in the video show what's left).

  4. Apply a Hough transform and determine the highest-scoring pair of lines that have a plausible orientation and internal angle (green lines in video).

  5. Given the coordinates of all pixels (results of step 3) contained in a box surrounding the intersection of these lines, determine the 20th percentile x and y coordinates (yellow dot in video).

Each frame is analyzed separately, so I should be able to improve the results by looking at multiple frames at once. I wonder though if there are any alternative strategies that might work better? Would any of the object tracking algorithms out there be precise enough? Would it be worth the time and effort to train a CNN? My main problem right now is motion blur that reduces the edges detected by the canny transform.

This is my first CV project (I'm kicking myself for not asking the calligrapher to wrap the pen in colored tape), so any suggestions would be greatly appreciated.

3

u/Sweetpants88 Mar 14 '22

On step 2, why did you choose HSV threshold values?

1

u/Mega_Mandarin Mar 14 '22

Is there some other technique I could have used to detect the blue ink areas?

→ More replies (2)

51

u/thetrombonist Mar 14 '22

Great results so far, I’d look into trying to apply a Kalman filter for determining the position of the dot, to help remove the jittering

9

u/Mega_Mandarin Mar 14 '22

Thank you for the suggestion. Just knowing what to google is helpful!

23

u/FrogBearSalamander Mar 14 '22

There are plenty of options depending on your goals.

First, it sounds like you built a heuristic pen tip detector using standard computer vision techniques. The results already look pretty good! But it's not clear how you're evaluating the results beyond looking at the video and deciding that it's not good enough.

I recommend labeling as much data as you can. That's needed if you end up using machine learning, but it's also needed for quantitative evaluation. One of the most effective ways to (somewhat) quickly label your data is to use your existing tracker to get started. Then skim the results and manually fix the worst errors. Use this to update your tracker (makes the most sense in an ML setting) and iterate until you're happy with the results. You can see this as "active learning" where you act as the oracle deciding which examples get labeled next.

You can also build tools to help you identify bad frames, e.g. model the dynamics (needed for a Kalman filter anyway) and sort frames based on the gap between the estimated location and the predicted one.

You want the tracks to be consistent temporally. If you need an online system (i.e. output a point for each frame in order and never change it), use a Kalman filter or particle filter, etc. It's better if you don't need an online system and can process the whole video before reporting a track. Then you can use "future" information to improve previous points. This is called "kalman smoothing" and you and use the same idea with a particular filter.

Even without a full ML-based detector, you can use labeled frames to constrain the temporal smoothing. That means that every time you label a frame, the surrounding frames also improve a bit, which speeds up the process (i.e. you don't need to manually label every frame).

Focusing on the computer vision side, I'd look at shadow detection since it appears that many of the big errors are due to thinking the pen's shadow is the side of the pen. Google for the latest methods, but a simple method is to build a model of the background and look for darkening in HSV space (or a similar colorspace that treats luma separate from chroma). Then ignore any "edges" between paper and shadowed paper.

8

u/thecodingnerd256 Mar 14 '22 edited Mar 14 '22

That is an absolutely stellar reply. You said what i considered to be most important when i had a quick look and that is trying to mitigate the effect of the shadows. The current cv method is pretty close with some filtering as eveyone is saying. The key here is to get the input data slightly cleaner before using it.

One slightly different way of looking at this is to not track the pen at all. The OP has shown a clear ability to track the ink and remove it from consideration. I would argue that you could improve the pens location detection by tracking where new ink appears in the image. Both data sets together should be pretty robust even without the shadow removal.

EDIT

Also thinking about it a bit more you are using the Hough to match to an assumed angle which remains the same. Why dont you use the angle from previous frames to try and lock the lines to the motion of the pen. As the pen moves and changes orientation the angle will change and you need to accept a range of angles to accomodate that. The shadow will always increase that angle and may even be acceptable but if you change that target angle for Hough transform to match your previously tracked target then you are more likely to keep track of the correct location.

4

u/Mega_Mandarin Mar 14 '22

I would argue that you could improve the pens location detection by tracking where new ink appears in the image.

I originally tried using something like this to create animations for the characters by masking over the . There will be a minority of frames where the tip of the pen occludes the fresh ink, but I hadn't thought of using that as a source of data in the pen tracking code. That's a great idea.

Why dont you use the angle from previous frames to try and lock the lines to the motion of the pen.

Yeah, my code looks at each frame individually right now, but it can definitely be improved by looking backward and forward in time. I don't need to process the videos online.

3

u/Mega_Mandarin Mar 14 '22

Thank you for such a comprehensive reply. You've certainly given me a lot to think about!

The barrier to using a supervised learning approach with labeled data is there there is so much data to label! I have ~15 hours of footage. A certain amount of manual labeling and error correction is inevitable though because whatever process I come up with won't be 100% reliable.

I have scanned images of the paper after all the characters were written. I think I will develop an objective function that compares predicted pen tip points with areas where I know ink was applied to the page. I don't need to process the videos online, so I can pose the entire video for each Chinese character as an optimization problem. I will look up kalman smoothing to make sure I don't end up reinventing the wheel.

Shadow detection is a great idea. I will look into that.

→ More replies (1)

7

u/johnnySix Mar 14 '22

Applying a pattern tracker for the tip of the pen could help here. I know I’m visual effects they do tracking that is pixel precise. So that could be used as the next step per frame

2

u/Mega_Mandarin Mar 14 '22

pattern tracker

like a cascade classifier?

4

u/johnnySix Mar 14 '22

More like template matching. You tell it what the tip of the pen looks like as a pixel pattern, and the algo lines your point up to that location

3

u/gireeshwaran Mar 14 '22

What about using particle filter for tracking the dot ?

1

u/Mega_Mandarin Mar 14 '22

particle filter

I will look into that, thank you.

3

u/LokiJesus Mar 14 '22

Can you control the pen type and lighting? If you could purchase a pen wit a red tip and matte finish (non specular reflective) and used a lighter ink or something blue, you could really separate the image onto the red and blue channels of the image before processing. It is always worth it to precondition your video before digitizing if you can.

2

u/Mega_Mandarin Mar 14 '22

What you see is what I have, unfortunately; everything has already been filmed. I should have asked the calligrapher to wrap the pen in a darker-colored tape.

3

u/4xle Mar 14 '22 edited Mar 14 '22

If you can identify a part of the pen with template matching before characters start being drawn, you might be able to use optical flow tracking to track the motion of several points on the pen, including the tip? Especially if you can mask out the blue ink from the process. Combined with your current process and a Kalman filter, you should be able to get a fairly stable tracking point as long as the pen doesn't twist, I think.

Deep learning feels like overkill for this, but the other thing you could do is mask the frames looking for changes in blue ink, using growing blue ink lines to inform the path of the tip. It would only help with actually tracing the stroke in post, and not intermediate lifts, but combined with your current process I think you'd be able to check/stabilize the results from tracking the pen tip fairly well.

2

u/Mega_Mandarin Mar 14 '22

I will look into optical flow tracking, thank you. I've found that the angular orientation of the pen doesn't change that much during the writing process.

Tracking the growth of blue ink lines as an additional data source is a great idea.

1

u/VU22 Mar 14 '22

The first problem I see is the lines also created for shades as the edge of pen, if the pen is white you might need to add extra morphological transformation, thresholding, and also a interpolation for lines if the frame rate is high enough. Other than that it seems cool, but I would try these methods just to see if they make it any better.

2

u/Mega_Mandarin Mar 14 '22

Thanks for the ideas. I think some interpolation will be inevitable, but the videos were filmed at 30 fps so that should be ok.

1

u/[deleted] Mar 14 '22

I have no idea what you are doing but its incredible

1

u/Mega_Mandarin Mar 14 '22

Thanks, but there really isn't anything super-sophisticated going on here. This is my first CV project; I just followed some tutorials to learn the basics and googled to fill in the gaps.

1

u/[deleted] Mar 15 '22 edited Mar 15 '22

I’m surprised there isn’t more discussion around the architecture. What model are you using?

I would recommend modeling pose and not the outline/mask of the pen

This is because you are likely to have higher uncertainty around edge pixels, which is what is causing your point to jitter. With pose, you would likely reduce jitter as you are compressing the underlying task for the network to learn. It is also much easier to label pose on frames of your video, as you are just connecting different ROI xy coordinates on the pen frames. You can also easily convert masks from prelabeled data.

I also would make the pen tip ROI the pixel just above/below/left/right of the pen tip depending on the pen stroke, as this will decouple the architecture from trying to figure out the exact pixel of the pen tip, which should further reduce jitter. It will instead model the ink location which is what you want.

Correct me if I’m wrong, it just seems like you are using masks from your example.

1

u/Mega_Mandarin Mar 15 '22

I am just using masks (as well as hough and canny transforms). I described the processing steps in a comment above.

→ More replies (1)

1

u/[deleted] Mar 15 '22

I'd say maybe paint the tip of the pen a different color to get rid of any reflectivity that could be messing with the camera a bit

1

u/Sate18SXD Mar 25 '22

I am new to classical CV. How did you get the two green lines?

1

u/Mega_Mandarin Mar 25 '22

I added a comment above describing the steps my code takes

1

u/PacManFan123 Mar 14 '22

You can also create a moving average of the vectors over time to refine the position

1

u/Neilblaze Apr 17 '22

Interesting! Curious, what are those two green-colored lines that's intersecting at pen's tip?

0

u/[deleted] Mar 14 '22

[deleted]

1

u/Mega_Mandarin Mar 14 '22

Everything has already been filmed, unfortunately.