Detect image validity with C++?

I am trying to do a c++ program that detects a validity of an image. Let say there is a square. If the 'X' is inside the square, means the image is valid.
If the 'X' is too big and exceed the boundaries of the square, tne image is invalid.

But as a beginner in C++ and opencv, I don't know how to begin. Any help is appreciated.

well, this could be really easy or really, really hard lol. need more info from you.

I mean, you could be asking if you trace the diagonal of a square and check pixel values, if they form an X in some specific color (lets say black?) and that the X does not go out of bounds. this is a simple iterative loop over the pixel data, and very trivial.

or you could be checking multiple colors, distorted X shapes that are not purely on the diagonal, or other things that can be summed up in "a relatively easy but not for a novice pattern recognition problem". here you could build an OCR engine and look for the letter X in the area of interest, for example, or you could make a number of crude attacks on the problem that will get you the right answer most of the time, for example you can derez the square portion of the image down to a very small baseline image and check that, for example if you derez down to 9 pixels and see if that matches one or two patterns ... it will work for probably 90% of input, you could try something like that to see if it works for your data.

My program will about validating an election ticket. If the black X is too big and outside the square, it will be invalid. But what opencv C++ concept to use?
i haven't used opencv but that really isnt the question yet.
the question is how to solve the problem. What tools you invoke to do that come later.

so election x boxes are probably drawn by hand. How x like do they have to be to qualify, so long as they are in the box? Would you accept a fully filled in box, with no white left? A / half x? The letter Y ? etc? the scoring criteria here are *everything*.

once you understand the scoring criteria, ... well, the old way that I did this kind of thing, you would take a bunch of real actual samples of data (or, make some up) and train the engine to recognize what is allowed, and what is not allowed, extensively. Then you run it on some data it has never seen before and see how well it did. The more data you use in training and testing, the better your confidence in the result. So it looks like (from a 2 second read online) opencv supports training of this nature. However it also supports other ideas.. if you can think of a better approach, go for it. Perhaps all you need is its recognition algorithm to take a look to see if there is an X there at all or not, and if there is, a dumb loop to see that its inside the box would put the problem to rest?

problems of this nature can sometimes get better answers if you do some preprocessing. It may be that the library does this for you already, but, for example, converting the image (lets say its true color) to black and white only (literally a one bit per pixel image) may train far, far better in the algorithms than the true color image. It may be looking at a lot of irrelevant data for its decision process in the full image, things like lighting or variation in paper shade etc may have a weird effect. /shrug just some ideas.
Last edited on
Lol I would not trust machine learning to validate a ballot, I think the noisy behavior of such a process would be horrible for any sane election process (or SAT exam, we don't need machine learning to grade exams). But maybe it has been done. Most ballots are electronic these days, with a paper copy being made just for redundancy.

But anyway, you bring up a good points with asking what is the actual pass/fail criteria -- determining the criteria here affects your entire process. Some other factors to consider: Can the ballot be rotated? How thick can the black lines be? Should a slight smudge discount a ballot? Depending on how realistic you want to be, it gets a lot more complicated. Is the (black?) color of the box itself distinguishable from the black of the X?

Not many people here have experience with the OpenCV API, I would probably try to find a more dedicated forum for that. If your problem is the actual logic in your program, we can certainly try to help iron out the details, but discussing the actual OpenCV functions to use would probably be better on another forum.

Again, I don't know OpenCV, but I searched "opencv detect squares" and found this https://github.com/opencv/opencv/blob/master/samples/cpp/squares.cpp

What I would do is first detect the ballot square (I'd imagine you have a known size to look for), and then see if there is significant black marking outside of the square. But there's certainly tons of edge cases here, so maybe provide examples of input/output if you want more-concrete answers.
Last edited on
Lol I would not trust machine learning to validate a ballot

I should have mentioned that. 100% that.

AI is cool. Its a lot of fun. But unless the problem is idiotically trivial (meaning you can code it with a couple of if - else statements without AI) or done to death by a million bored programmers (consider chess algorithms) you will almost always end up with a black box that gives the right answer 85, maybe 90, maybe even 95% of the time and just can't get its act together for the last few cases.
That doesn't seem like much, but people would be rioting in the streets if there was a 5% error in a close election (consider if this had been the issue with, say, gore in florida or something like that). They would be madder than all get out with even a 1% error, for a situation like that. You don't want to be THAT programmer or the company that did the work, no way. Please tell us that this is just a school assignment, or at the very least that you are working in a place where the actual election results are not too important, like a nice dictatorship or something?



Is machine learning even relevant for this? It's not like you have a corpus of data that you have to classify, or something like that. You have an image that you have to compare to a few known images (first option selected, second option selected, etc.) to find which it's more similar to. The worst case is where the ballot is not sufficiently like any of the reference images, in which case the fallback can be a human.
Topic archived. No new replies allowed.