How can you recognize the sign of infinity (namely it) in a photo or video?

I would be grateful if you bring the code to the answer.

UPD: Explain how, for example, using contour analysis to understand whether there is an infinity sign in a photo. An example below (in the center there can be a complete connection, a person can be seen - he can hold a sheet, the color is not important).

Outlines can be contiguous, a person can be seen - he can hold a sheet, color is not important

  • five
    My daughter asks how to spell the number 8 - the father answers: and you turn the infinity sign 90 degrees - and you will get what you need. - nick_n_a
  • Those. how to recognize everything else, you know? I think you need to train cascades to search for this sign. - Gino Pane
  • Gino Pane, and so usually everything is recognized - are learning cascades on the appropriate sample? - Timur Musharapov
  • 2
    @nick_n_a is not 90 degrees, but pi in half. - Nick Volynkin
  • not enough details. What can be at the entrance? (who cuts the image so that one character remains? how much garbage can there be in the picture (colors, other characters)? How many font options do you want to support the styles? Is 8 an infinity sign (who sets the correct orientation of the picture?) etc. - jfs

3 answers 3

OpenCV provides many tools for pattern recognition, including through contour analysis. In the third version of the framework, almost everything related to contour comparison was allocated to a separate module with the telling name Shape Distance and Matching .

The author of the question did not specify with which figures (sets of figures) the infinity sign will be compared, and noted only that only the fact of the candidate’s similarity to the desired object is of interest.

Obviously, by itself, the concept of "infinity" will not say anything to the machine, and therefore will have to offer it a certain image in the form of a reference. Let this image look like this ( model1.jpg file):

Meanwhile, in order to make sure that the method disclosed further allows us to distinguish other figures, we will add several more different images to the reference model ( model2.jpg , model3.jpg and model4.jpg files, respectively):

In fact, all images are the same size and are shown in a reduced form only to save space on the page.

We start by selecting the object whose image was provided by the author of the question:

cv::Mat tst_src_mat = cv::imread("test.jpg", cv::IMREAD_GRAYSCALE); if(tst_src_mat.empty()) return; cv::Mat tst_bin_mat; cv::threshold(tst_src_mat, tst_bin_mat, 0, 255 , cv::THRESH_OTSU | cv::THRESH_BINARY_INV); 

After binarization, it turns out that in addition to the object of interest in the image in the lower left corner there is also an artifact:

It is possible to get rid of it through a simple and somewhat philosophical approach — to ignore, leaving only that outline in sight, which, on the contrary, is of interest. This can be done, for example, by focusing on the maximum area and the corresponding index in the vector:

 std::vector<std::vector<cv::Point> > tst_cs; cv::findContours(tst_bin_mat.clone(), tst_cs , cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); int ci = -1; double max_area = 0.; for(int i = 0, n = tst_cs.size(); i < n; ++i) { const double area = cv::contourArea(tst_cs.at(i)); if(area > max_area) {max_area = area; ci = i;} } if(ci == -1) return; 

This is followed by a nuance that implies that any contours being compared should have the same number of points in its composition. Of course, various figures usually will not follow such a rule, which means they will have to help them “on the handbrake” by simply adding the required number of points, but only with those coordinates that are already in the figure:

 const int num_pts = 300; for(int i = tst_cs.at(ci).size()-1, d = 0; i < num_pts; ++i) tst_cs[ci].push_back(tst_cs[ci][d++]); 

The constant num_pts is an arbitrary number. In principle, it depends on the complexity of the work. Ideally, it should be equal to the maximum number of points at the very uh-uh ... multi-point contour of the compared.

Perhaps it is time to move on to the previously reviewed benchmark and images that are very different from the test pieces:

 // Герой настоящей статьи, который собственно // и будет производить сравнение контуров. cv::Ptr<cv::ShapeContextDistanceExtractor> sc = cv::createShapeContextDistanceExtractor(); for(int i = 0; i < 4; ++i) { std::string fname = std::string("model") + std::to_string(i+1) + std::string(".jpg"); cv::Mat src_mat = cv::imread(fname, cv::IMREAD_GRAYSCALE); if(src_mat.empty()) return -1; cv::Mat bin_mat; cv::threshold(src_mat, bin_mat, 0, 255 , cv::THRESH_OTSU | cv::THRESH_BINARY_INV); std::vector<std::vector<cv::Point> > cs; cv::findContours(bin_mat.clone(), cs , cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); for(int i = cs.at(0).size()-1, d = 0; i < num_pts; ++i) cs[0].push_back(cs[0][d++]); std::cout << fname << " - " << sc->computeDistance(tst_cs[ci], cs[0]) << std::endl; } 

The result of the code will be the following values:

model1.jpg - 2.84489

model2.jpg - 112.033

model3.jpg - 32.8308

model4.jpg - 524.659

The image model1.jpg contains the infinity sign, and accordingly has the smallest distance differences with the test figure. Images with the sign "dogs" ( model2.jpg ) and "sickle with a hammer" ( model4.jpg ) in view of the obvious differences complete the list of dissimilarity. But who would have thought that the “Batman” ( model3.jpg ) will get second place here. And yet, if you look closely, it is quite possible to catch the features common to infinity.

  • A wonderful and very detailed answer - thank you very much, alexis031182! - Timur Musharapov
  • alexis031182, and how can you find this particular figure in the photo, where there are other objects that are inherent in the photos? Then use the Haar cascade or is there a better way? - Timur Musharapov
  • @TimurMusharapov, it depends on the input conditions. In response, this condition was the size of the object of interest, but it was possible not to screen out the artifact, and to compare it with the standard in the same way. That is, if a set of contours is detected in the frame, which cannot be eliminated by any obvious sign, we analyze them in the general order as usual candidates. - alexis031182

This answer is outdated and corresponds to the question in its original version .


How are children taught the alphabet? Show pictures in the primer, pronounce the corresponding sounds. How then do children begin to recognize the characters of the letters when they are not found in a letter? Obviously, at this point in time, they have already expended enough resources to memorize almost all possible features of the shape of these very symbols.

How do chimpanzees detect bananas? Probably more so because she was shown it at the time, singling out as an independent and very tasty object.

Darling, I will lead you to the very edge of the universe! I will give you this star, with incorruptible light she will light up our way to infinity ...

What did Favorite hope for when she accepted the proposal to move to infinity? Most likely, the associations that had been laid earlier allowed her to understand that this would be at least a pleasant journey.

And what about the car? And the car does not have the experience that they get a child, chimpanzees and Darling. You will have to work with the machine so that it can begin to distinguish the infinity drawn on paper in the form of a sign, the infinity of the banana gluttony and the infinity of the path under the incorruptible light.

Having appreciated the scale of the tragedy, how much has been done and how much more needs to be done, the developers (some on Seventh Day) begin to realize that it is necessary to urgently narrow the field of activity, limiting the creative potential to local changes.

Someone chooses a set of images with a specific outline of the infinity symbol on a certain background in order to be able to work with contours. Someone chooses the video, but with the same conditions specifying the sample. In any case, the creator no longer limits himself to encompassing the immense, and even if he asks his neighbors, picking their worlds, for help, he specifies the question as much as possible by offering them a couple of examples of their own vision of the subject. And everyone is happy.

  • Interesting answer. And how can you work with the contours? - Timur Musharapov
  • As already said, in the presence of certain conditions, such as a uniform background and an object that contrasts with it. If these conditions are met, then apply a contour analysis, which has several advantages over other methods: speed of work, variability to scale and often - to the rotation of the figure. But since this direction is extremely demanding on the input conditions, then considering it as a solution without relying on a specific image simply does not make sense. - alexis031182
  • The sign of infinity on the usual album sheet. Tell me how to proceed? - Timur Musharapov
  • If in a nutshell, then: frame capture, blur blur() and findContours() . In general, you need to add this newly indicated nuance to the question and attach an example of the image. As part of the recognition, you always need a maximum of specifics. Otherwise there will be only lengthy answers, like mine. - alexis031182
  • Updated, thanks for the advice. - Timur Musharapov

Basically, you can train the Haar cascade for these purposes. Moreover, if the symbol is always on a white background, there should be no problems. Here is a link to the documentation on the training of the cascade, here is an article on Habré.