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

checkbox detection

Post 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.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: checkbox detection

Post 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.
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 »

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:
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post by cso »

Thanks for the replies!

I was also thinking, how can detect an actual rectangle? Like 4 edges and only white pixels "inside"?
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post by cso »

Any ideas?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: checkbox detection

Post by snibgo »

The methods mentioned above apply to empty checkboxes as well as ticked ones.
snibgo's IM pages: im.snibgo.com
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post by cso »

Yeah, I was just wondering if this alternative approach is possible.
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: checkbox detection

Post 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.
snibgo's IM pages: im.snibgo.com
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: checkbox detection

Post 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
snibgo's IM pages: im.snibgo.com
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: checkbox detection

Post by snibgo »

I don't know what you mean by "[peak black pixel sum]" etc.
snibgo's IM pages: im.snibgo.com
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 »

Have you tried the compare function that I suggested above?
cso
Posts: 28
Joined: 2017-06-09T19:10:21-07:00
Authentication code: 1151

Re: checkbox detection

Post 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%


 
Post Reply