Tooth uncle tutorial is easy to understand

“Someone in the group asked

The original image

After filling in the color

Train of thought

Extract blue line –> Extract red line –> Extract coordinates –> Fill color

Extraction of blue

  1. Using a PS color picker or other software’s straw, suck in a few more blue dots and break down the RGB component of the color into the method
let lowColor = colors.rgb(lowRGB.red, lowRGB.green, lowRGB.blue);
let highColor = colors.rgb(highRGB.red, highRGB.green, highRGB.blue);
let newImg = images.inRange(img, lowColor, highColor);
Copy the code

The extracted blue area

  1. And you can see that there are some breaks in the middle, so let’s expand it a little bit
Imgproc.morphologyEx( newImg.mat, mat, Imgproc.MORPH_DILATE, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, Size(3, 3)));Copy the code

  1. Draw the outline of the picture. Fill in the outline with green
Imgproc.findContours(mat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, Point());
Imgproc.drawContours(img.mat, contours, -1, Scalar(0, 255, 0, 255), -1, 8);
Copy the code

  1. Gets the smallest enclosing rectangle of the contour
let box = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(0).toArray()));
for (let j = 0; j < 4; j++) {
  Imgproc.line(img.mat, rect[j], rect[(j + 1) % 4], Scalar(0, 255, 0, 255), 1, 8);
}
Copy the code

  1. Take the coordinates of the two ends of the rectangle and take the first point on one end and the third point on the other end and draw the line between the two points
Imgproc.line(img.mat, rect[0], rect[2], Scalar(0, 255, 0, 255), 1, 8);
Copy the code

  1. Get the coordinates of the blue line in the same way

  1. Four points are obtained, and then draw the quadrilateral with OpencV
Imgproc.fillPoly(polyMat, pts, Scalar(0, 255, 0, 255), 1);
Core.addWeighted(img.mat, alpha, polyMat, beta, gamma, matAddWeighted);
Copy the code

The test environment

Phone: Mi 11 Pro

Android version: 12

Autojs version: 9.1.10

Baidu, Bing, StackOverflow, Github, Android docs, AutoJS docs, and finally just ask in the group

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

Wechat official account tooth Uncle tutorial