Page 1 of 2

checkbox detection

Posted: 2017-08-02T23:30:43-07:00
by cso
How can I detect if the checkbox in an image is checked or not?

Example with checkbox checked:
http://imgur.com/a/3VPuC

Example with checkbox not checked:
http://imgur.com/DwQvB4X

Note: I'm not embedding the images because the one with the checkbox checked appears weird.

Re: checkbox detection

Posted: 2017-08-03T07:18:09-07:00
by GeeMack
cso wrote: 2017-08-02T23:30:43-07:00Best I can suggest is crop out the box that was shaded and use compare with subimage-search to locate the best match. If the match score is low enough for the given metric such as rmse, then you have a good match.
First, you need to provide the version of IM you're using and let us know which platform or OS you're running it on and some more details about your needs.

If your checkbox will always be at a known location in the image, if the checked box is always black (or transparent like your example image), and if the unchecked box is always white inside, you can just test a pixel in the box with a command like this...

Code: Select all

convert image.png -format %[fx:p{12,16}] info:
That will return a 1 if the box is unchecked, and a 0 if the box is checked. Substitute the "12,16" there for the coordinates of a known pixel you want to test.

There may be far better ways to do it, but we'd have to know a lot more about your situation.

Re: checkbox detection

Posted: 2017-08-03T09:53:44-07:00
by fmw42
Best I can suggest is crop out the box that was shaded and use compare with subimage-search to locate the best match. If the match score is low enough for the given metric such as rmse, then you have a good match.

See
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/

Code: Select all

compare -metric rmse -subimage-search largeimage smallrectangle null:

Re: checkbox detection

Posted: 2017-09-18T15:42:39-07:00
by cso
Thanks for the replies!

I was also thinking, how can detect an actual rectangle? Like 4 edges and only white pixels "inside"?

Re: checkbox detection

Posted: 2017-09-21T16:10:43-07:00
by cso
Any ideas?

Re: checkbox detection

Posted: 2017-09-21T17:22:58-07:00
by snibgo
The methods mentioned above apply to empty checkboxes as well as ticked ones.

Re: checkbox detection

Posted: 2017-09-22T11:42:43-07:00
by cso
Yeah, I was just wondering if this alternative approach is possible.

Re: checkbox detection

Posted: 2017-09-28T06:06:59-07:00
by cso
I had an idea, check it with histogram distribution? The checkbox with a check won't have 2 sudden drops in the histogram.
I mean:
- the edges of the rectangle have "max black pixels"
- the "inside" of the checked rectangle has "less than max black pixels"
- the "inside" of the unchecked rectangle (empty) has few black pixels (only the edges of the rectangle)

How can I do this?

Re: checkbox detection

Posted: 2017-09-28T07:51:53-07:00
by snibgo
Do you know where the checkbox is? And how thick the borders are? If you do, then crop the image to just the inside of the box. Find the mean lightness of that area. If this is light, then there is no check. If it is somewhat dark, then there is a check.

If you don't know where the checkbox is, or how thick the borders are, the problem is harder, and the first step is to solve those problems.

Re: checkbox detection

Posted: 2017-09-28T08:09:07-07:00
by cso
I know where it is but not exact to the pixel level. There might be some x, y translation.
How do I find the mean lightness?

Re: checkbox detection

Posted: 2017-09-28T09:17:02-07:00
by snibgo
"-format %[fx:mean]" gives the mean value, on a scale of 0.0 to 1.0. For example:

Code: Select all

f:\web\im>%IM%convert rose: -format %[fx:mean] info:

0.412341

Re: checkbox detection

Posted: 2017-09-29T09:07:53-07:00
by cso
Tried it and while it seems to work ok most of the time, sometimes there is a small shift (translation i X or Y) in the crop of the checkbox which is problematic for accuracy.

The "[peak black pixel sum] ... [low black pixel sum] ... [peak black pixel sum]" would be better I think. Don't know how to implement this though, any ideas?

Re: checkbox detection

Posted: 2017-09-29T09:21:08-07:00
by snibgo
I don't know what you mean by "[peak black pixel sum]" etc.

Re: checkbox detection

Posted: 2017-09-29T09:39:23-07:00
by fmw42
Have you tried the compare function that I suggested above?

Re: checkbox detection

Posted: 2017-09-29T09:43:00-07:00
by cso
I mean count (or calculate the sum of) the black black pixels on each Y.
That way it will be something like:

Code: Select all

 0%
90%    +--------+
 2%    |        |
 2%    |        |
 2%    |        |
 2%    |        |
90%    +--------+
 0%