Mat src = imread("1.jpg"); //
HSV (or HSB) means Hue, Saturation, Value (Brightness), where:
Hue - color tone, i.e. color shade.
Saturation - saturation. The higher this parameter, the “cleaner” the color will be, and the lower, the closer it will be to gray.
Value (Brightness) - the value (brightness) of the color. The higher the value, the brighter the color will be (but not whiter). And the lower, the darker (0% - black)
// HSV Mat hsv = Mat(src.cols, src.rows, 8, 3); // vector<Mat> splitedHsv = vector<Mat>(); cvtColor(src, hsv, CV_BGR2HSV); split(hsv, splitedHsv);
const int GREEN_MIN = 21; const int GREEN_MAX = 110;
for (int y = 0; y < hsv.cols; y++) { for (int x = 0; x < hsv.rows; x++) { // HSV- int H = static_cast<int>(splitedHsv[0].at<uchar>(x, y)); // int S = static_cast<int>(splitedHsv[1].at<uchar>(x, y)); // int V = static_cast<int>(splitedHsv[2].at<uchar>(x, y)); // // , if ((V < 20) || (H < GREEN_MIN) || (H > GREEN_MAX)) { src.at<Vec3b>(x, y)[0] = 255; src.at<Vec3b>(x, y)[1] = 255; src.at<Vec3b>(x, y)[2] = 255; } } }
Dilation (morphological expansion) - a convolution of an image or a selected image area with some kernel. The kernel can have any shape and size. In this case, the only leading position in the kernel is allocated, which is combined with the current pixel in the calculation of convolution. In many cases, a square or a circle with a leading position in the center is chosen as the core. The kernel can be viewed as a pattern or mask. The use of dilatation is reduced to a pattern passing through the entire image and the application of the local maximum search operator to the image pixel intensities covered by the template. Such an operation causes the growth of bright areas in the image. In the figure, pixels are marked in gray, which will be white as a result of the use of dilatation.
Erosion (morphological contraction) is the reverse operation. The effect of erosion is similar to dilatation, the only difference is that the local minimum search operator is used; the pixels that turn black as a result of erosion are grayed out.
int an = 5; // . Mat element = getStructuringElement(MORPH_ELLIPSE, Size(an * 2 + 1, an * 2 + 1), Point(an, an)); dilate(src, tmp, element); erode(tmp, tmp, element);
Mat grayscaleMat; cvtColor(tmp, grayscaleMat, CV_BGR2GRAY); // Mat mask(grayscaleMat.size(), grayscaleMat.type()); Mat out(src.size(), src.type()); threshold(grayscaleMat, mask, 200, 255, THRESH_BINARY_INV); // out = Scalar::all(255); // src.copyTo(out, mask);
Source: https://habr.com/ru/post/332464/
All Articles