Tooth uncle tutorial is easy to understand

Results show

Look for the cross button in the upper right

origin

A lot of people were wondering how do I find this cross in the upper right corner, and then someone asked me, and I looked into it, okay

The environment

Phone: Mi 11 Pro

Android version: 11

Autojs version: 9.0.10

Train of thought

  • Cut out a screenshot of the cross button in the upper right corner
  • Extract the contours of large and small images
  • Contrast profile feature
  • Displays the contour that best matches

What are the contour features

  • Degree of rectangularity: Degree of rectangularity reflects the degree to which an object fills its enclosing rectangle, that is, the proportion of the area of the contour to that of the enclosing rectangle
  • Aspect ratio: the ratio of the width to the height of the smallest enclosing rectangle
  • MatchShapes compares the similarity of two silhouettes

The above three types are used in this paper to compare whether the two contours are similar.

There are other contour features, please Baidu OpenCV contour features, or Halcon regional features

Here’s what you’ll learn

  • Outline enclosing rectangle
  • Outline minimum enclosing rectangle
  • Extract image contour
  • Draw a picture outline
  • Draws text to the specified position
  • Calculate the contour centroid
  • The rectangle degrees
  • Calculated contour area
  • Gaussian, binarization
  • Release opencV resources

Profile analysis

insets

A larger version

The image above compares the similarity of the two shapes according to matchShapes,

The similarity threshold is set to 9,

Anything less than 9,

The number in the image is how similar the outline is to the cross button,

The screenshot of the cross button in the upper right was taken at 1080 by 1920 resolution,

A larger version is 1152 * 2376,

The similarity of the fork button in the large picture is 7.168, not the minimum (the smaller the value is, the more similar it is).

The word “man” was the lowest,6.700,

So I’m going to add the other features, the rectangularity and the aspect ratio, and when I add them, none of the other contours match the features,

All that’s left is the cross button in the upper right corner

Contour search applies to scenarios

Transparent buttons, and simple graphics

Improve the accuracy and speed of contour search

Specify the search area, crop the excess image area, and then use the outline to find the image

The code on

1. Import the classes
runtime.images.initOpenCvIfNeeded();
importClass(org.opencv.core.MatOfByte);
importClass(org.opencv.core.Scalar);
importClass(org.opencv.core.Point);
importClass(org.opencv.core.CvType);
importClass(java.util.List);
importClass(java.util.ArrayList);
importClass(java.util.LinkedList);
importClass(org.opencv.imgproc.Imgproc);
importClass(org.opencv.imgcodecs.Imgcodecs);
importClass(org.opencv.core.Core);
importClass(org.opencv.core.Mat);
importClass(org.opencv.core.MatOfDMatch);
importClass(org.opencv.core.MatOfKeyPoint);
importClass(org.opencv.core.MatOfRect);
importClass(org.opencv.core.Size);
importClass(org.opencv.features2d.DescriptorMatcher);
importClass(org.opencv.features2d.Features2d);
importClass(org.opencv.core.MatOfPoint2f);
Copy the code
2. Read the picture
var bigImgPath = "/storage/emulated/0/ script /autojs search for similar profile image/big picture _ Close ad.jpg";
var smallImgPath = "/storage/emulated/0/ script /autojs Search for similar contoured images/small picture _ Close ad.png";
var bigImg = $images.read(bigImgPath);
var smallImg = $images.read(smallImgPath);
let bigImgMat = bigImg.getMat(); / / a larger version
let smallImgMat = smallImg.getMat(); / / a larger version
Copy the code
3. Extract contours
contour_info(bigImgMat, bigImgContours);
contour_info(smallImgMat, smallImgContours);
Copy the code
4. Calculate the rectangularity and aspect ratio
/ / rectangle degrees
let smallImgRectangularity = getRectangularity(smallImgContour);
/ / aspect ratio
let smallImgAspectRatio = getAspectRatio(smallImgContour);
Copy the code
5. Set the upper and lower limits of rectangularity and the same width and height ratio
let rectangularityUnit = 0.1;
let rectangularityLowerLimit =
  smallImgRectangularity - rectangularityUnit < 0 ? 0 : smallImgRectangularity - rectangularityUnit;
let rectangularityUpperLimit =
  smallImgRectangularity + rectangularityUnit > 1 ? 1 : smallImgRectangularity + rectangularityUnit;
Copy the code
6. Compare the contour features when traversing the contour of the large image
if (dist < distLimit) {
  / / rectangle degrees
  let itemRectangularity = getRectangularity(item);
  if (1 || (itemRectangularity > rectangularityLowerLimit && itemRectangularity < rectangularityUpperLimit)) {
      / / aspect ratio
      let itemAspectRatio = getAspectRatio(item);
    if (1| | (itemAspectRatio > aspectRatioLowerLimit && itemAspectRatio < aspectRatioUpperLimit)) {the outline of similar information list. Push ({dist: dist,
        area: area,
        itemRectangularity: itemRectangularity,
        itemAspectRatio: itemAspectRatio,
        contour: item,
        center: {
          x: centerX,
          y: centerY,
        },
        wh: {
          w: rotateRect.width,
          h: rotateRect.height, }, }); }}}Copy the code
7. Draw Outlines and similarities
// CV ::Scalar(V1, v2, V3, V4), the first three parameters will set BGR successively, which is opposite to RGB, and the fourth parameter will set the transparency of the pictureImgproc.drawContours(bigImgMat, sorted outline list, -1, Scalar(0.0.255.255), thickness, lineType);
Imgproc.putText(bigImgMat, content, ptTextBottomLeftPosition, fontFace, fontScale, Scalar(0.0.255.255), thickness);
Copy the code
8. View the picture of MAT
let tempFilePath = files.join(files.getSdcardPath(), "Script"."Autojs finds images with similar contours. PNG");
Imgcodecs.imwrite(tempFilePath, bigImgMat);
app.viewFile(tempFilePath);
bigImgMat.release();
Copy the code

Quotes.

Thinking is the most important, other Baidu, Bing, StackOverflow, Android documents, autoJS documents, and finally in the group to ask — fang Shu tutorial

The statement

This tutorial is intended for learning purposes only and is not intended for any other use