Best way to detect if checkbox is ticked

2024/9/29 23:26:38

My work:

  1. Scan the paper
  2. Check horizontal and vertical line
  3. Detect checkbox
  4. How to know checkbox is ticked or not

At this point, I thought I could find it by using Hierarchical and Contours: Below is my work

for i in range (len( contours_region)):     #I already have X,Y,W,H of the checkbox through #print(i)                               #cv2.connectedComponentsWithStatsx = contours_region[i][0][1]            #when detecting checkboxx_1 = contours_region[i][2][1]y = contours_region[i][0][0]y_1 = contours_region[i][2][0]image_copy= image.copy()X,Y,W,H = contours_info[i]cv2.drawContours(image_copy, [numpy.array([[[X,Y]],[[X+W,Y]],[[X+W,Y+H]],[[X,Y+H]]])], 0, (0,0,255),2)gray = cv2.cvtColor(image_copy, cv2.COLOR_BGR2GRAY)ret,bw = cv2.threshold(gray,220,255,cv2.THRESH_BINARY_INV)contours,hierarchy = cv2.findContours(bw[x:x_1, y:y_1], cv2.RETR_CCOMP,1)print('-----Hierarchy-----')print(hierarchy)print('-----Number of Contours : '+ str(len(contours)))cv2.imshow('a', image_copy)cv2.waitKey(0)

enter image description here

I got this result (some high contours, some high hierarchy)

-----Hierarchy-----
[[[-1 -1  1 -1][ 2 -1 -1  0][ 3  1 -1  0][ 4  2 -1  0][ 5  3 -1  0][ 6  4 -1  0][ 7  5 -1  0][-1  6 -1  0]]]
-----Number of Contours : 8

Another result:

enter image description here

Low Contours, Low Hierarchy

-----Hierarchy-----
[[[-1 -1  1 -1][ 2 -1 -1  0][-1  1 -1  0]]]
-----Number of Contours : 3

However, it's not perfect some case where it's not ticked but still got a really high result

[[[-1 -1  1 -1][ 2 -1 -1  0][ 3  1 -1  0][ 4  2 -1  0][ 5  3 -1  0][-1  4 -1  0]]]
-----Number of Contours : 6

enter image description here


In general, After review the whole data, the gap is not convincing between ticked and not ticked. Around 30% of boxes, giving the wrong result. Therefore, really wish to have a better method.

Answer

I think erode function help you. Use erosion to make the ticks bigger then count the non zero pixels. Here You can find the basics:

import cv2 
import numpy as np
from google.colab.patches import cv2_imshow
img = cv2.imread("image.png");
cv2_imshow(img)
kernel = np.ones((3, 3), np.uint8) better_image = cv2.erode(img,kernel)
cv2_imshow(better_image)
https://en.xdnf.cn/q/71153.html

Related Q&A

ResultSet object has no attribute find_all

i always met one problem, when I scraping one web page.AttributeError: ResultSet object has no attribute find. Youre probably treating a list of items like a single item. Did you call find_all() when y…

How can I set path to load data from CSV file into PostgreSQL database in Docker container?

I would like to load data from CSV file into PostgreSQL database in Docker. I run:docker exec -ti my project_db_1 psql -U postgresThen I select my database:\c myDatabaseNow I try to load data from myfi…

Why is this simple Spark program not utlizing multiple cores?

So, Im running this simple program on a 16 core multicore system. I run it by issuing the following.spark-submit --master local[*] pi.pyAnd the code of that program is the following. #"""…

How to get python dictionaries into a pandas time series dataframe where key is date object

I have a python dictionaries where the key is a dateobject and the value is the timeseires.timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {t2: 400, t1: 1000},datetime.datetime(2013, 3, 17, 18, 2…

Changing the color of an image based on RGB value

Situation:You have an image with 1 main color and you need to convert it to another based on a given rgb value.Problem:There are a number of different, but similar shades of that color that also need …

Python NumPy - FFT and Inverse FFT?

Ive been working with FFT, and Im currently trying to get a sound waveform from a file with FFT, (modify it eventually), but then output that modified waveform back to a file. Ive gotten the FFT of the…

Tools to help developers reading class hierarchy faster

I mostly spend time on Python/Django and Objective-C/CocoaTouch and js/jQuery in the course of my daily work.My editor of choice is vim for Python/Django and js/jQuery and xcode for Objective-C/CocoaTo…

Python Last Iteration in For Loop [duplicate]

This question already has answers here:What is the pythonic way to detect the last element in a for loop?(34 answers)How do I read and write CSV files?(7 answers)Closed 9 months ago.Is there any simp…

Django 1.7 multisite User model

I want to serve a Django application that serves multiple web sites by single database but different user sets. Think like a blog application, it will be used by several domains with different themes, …

Does for key in dict in python always iterate in a fixed order?

Does the python codefor key in dict:..., where dict is a dict data type, always iterate in a fixed order with regrard to key? For example, suppose dict={"aaa":1,"bbb",2}, will the …