checkbox detection

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post by cso »

@Fred: Wouldn't that be slower?

edit: also less reliable because of the X, Y shift.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: checkbox detection

Post by fmw42 »

It might be slower, but there are ways to speed it up. It will be more accurate with respect to x,y shifts because it finds the best shift that makes the match score better. You can limit the search to a region around your approximately known locations. You can do the search at multiple resolutions to speed it up. You can use my faster FFT based scripts such as normcrosscorr, if on a Unix-like system.

Perhaps you should post an example image of what you are trying to search for the check boxes.
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post by cso »

I already crop separate images of each checkbox. Here are some examples:

Image
Image
Image
Image

Do you think subimage search would work better/faster than the "counting black pixels on every Y" approach?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: checkbox detection

Post by fmw42 »

cso wrote: 2017-09-29T10:24:09-07:00
Do you think subimage search would work better/faster than the "counting black pixels on every Y" approach?
I have no idea, since I do not know what you are trying to match to. Can you provide an image that shows an image that you want to test?

Counting black does not seem to me to be an accurate method, depending upon what you are testing?
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post by cso »

I want to match this image Image in one of these 4 boxes:

Image
Image
Image
Image
Last edited by cso on 2017-09-29T11:49:56-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: checkbox detection

Post by fmw42 »

Your image does not show up! However I am able to get to it. I do not understand why you want to match those templates against a fully black square? If you want to find all squares that are black, use a black template.

Perhaps I do not understand what your goal is? Please explain the project in more detail and the reason behind the project.

Are you trying to detect if a student has filled in a checkbox? Or are you just trying to detect the location of checkboxes whether filled or not?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: checkbox detection

Post by fmw42 »

OK you have now changed the image to an empty check box. Are you trying to find the location of an empty check box or any checkbox that is filled, empty or partially filled? Why not just find the black outline using a template that is transparent in the center and outside where it is white. You can do a masked compare to detect the black and not care what is inside it.
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post by cso »

Sorry, edited the post, you're too fast (thanks!) and I was sloppy! I don't have fully black squares anymore, I only ticked check boxes and non ticked ones (empty).
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post by cso »

I'm not trying to find the location of the boxes. I already know the location of the boxes (sometimes there is a small deviation of some pixels which is the X/Y shift you see in the group of the 4 images) and crop them up in individual images.

What I want to know is whether the cropped image of the checkbox is empty or ticked/filled.
Why not just find the black outline using a template that is transparent in the center and outside where it is white. You can do a masked compare to detect the black and not care what is inside it.
I don't understand, can you please illustrate?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: checkbox detection

Post by fmw42 »

I would suggest you use compare to align the boxes. Then you can count the number of black pixels or get the average gray level value.
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post by cso »

@Fred: I tried, it doesn't work well.
I read your solution in this thread: https://www.imagemagick.org/discourse-s ... hp?t=31295
I have to hand-tune the threshold to make it work.

I also read this, maybe I should use morphology?
http://www.imagemagick.org/Usage/compare/#sub-image

Looks complicated though.
Other ideas?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: checkbox detection

Post by fmw42 »

You have not tried using masked compare. See viewtopic.php?f=4&t=31053
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post by cso »

Ok maybe cropping the individual boxes is not a good idea after all.
To reset the problem, this is my larger image and I want to classify all boxes in this image as ticked or non-ticked.

Image

Fred, what would you use?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: checkbox detection

Post by fmw42 »

I would use the last two as separate reference images to match all the others using a compare -subimage-search. If one or the other match better than some threshold, you know it is not checked if the bottom one and checked if the second from the bottom one. But you should pad the other with a little more white so that the reference images have room to move to the best location.
Post Reply