使用C ++和opencv查找最左,最右,最高和最向下的蓝色像素

时间:2019-07-11 07:16:40

标签: c++ opencv

我正在尝试使用c ++和opencv编写代码,以识别哪个蓝色像素首先出现在左,右,上和下。 我正在使用的图片是这张: blue lines on image

我想收到的是这样的图片:blue lines with extrema marked

这是我的代码,直到现在,我还不太明白为什么它不起作用。

    void findBluePixels(Mat& original, Point2f min_x, Point2f max_x, Point2f min_y, Point2f max_y) {
    //First I set every point to the beginning of the image
        min_x = Point(0, 0);
        max_x = Point(0, 0);
        min_y = Point(0, 0);
        max_y = Point(0, 0);
    //looking for the most left blue pixel (the blue pixel with the smallest x value)
    //we are going through every column and every row
        for (int col = 0; col < original.cols; col++) {
            for (int row = 0; row < original.rows; row++) {
    //we check for every pixel if the pixel is blue and if the column of the pixel is bigger or equal to the beginning of the image
    if (original.at<Vec3b>(row, col)[0] == 255 && 
    original.at<Vec3b>(row, col)[1] == 0 &&
 original.at<Vec3b>(row, col)[2] == 0 && col >= min_x.y) {
    //if yes we have new value for the min_x and we stop looking because we have found the most left blue pixel
        min_x = Point(row, col);
        break;
                }
            }
        }
    //looking for the most right blue pixel (the blue pixel with the biggest x value)
    //we are going through every column and every row
        for (int col = 0; col < original.cols; col++) {
            for (int row = 0; row < original.rows; row++) {
    //we check for every pixel if the pixel is blue and if the column of the pixel is bigger or equal to the beginning of the image
    if (original.at<Vec3b>(row, col)[0] == 255 && 
    original.at<Vec3b>(row, col)[1] == 0 && 
original.at<Vec3b>(row, col)[2] == 0 && col >= max_x.y) {
    //if yes we have new value for the max_x, but we don't break because we are looking for the biggest blue pixel
    //we continue to search until the end of the picture
    max_x = Point(row, col);
                }
            }
        }
    //looking for the highest blue pixel (the blue pixel with the smallest y value)
    //we are going through every row and every column
        for (int row = 0; row < original.rows; row++) {
            for (int col = 0; col < original.cols; col++) {
    //we check for every pixel if the pixel is blue and if the row of the pixel is bigger or equal to the beginning of the image
    if (original.at<Vec3b>(row, col)[0] == 255 && 
    original.at<Vec3b>(row, col)[1] == 0 &&
    original.at<Vec3b>(row, col)[2] == 0 && row >= min_y.x) {
    //if yes we have new value for the min_y and we stop looking because we have found the highest blue pixel
    min_y = Point(row, col);
    break;
                }
            }
        }
    //looking for the most down blue pixel (the blue pixel with the biggest y value)
    //we are going through every column and every row
        for (int row = 0; row < original.rows; row++) {
            for (int col = 0; col < original.cols; col++) {
    //we check for every pixel if the pixel is blue and if the row of the pixel is bigger or equal to the beginning of the image
    if (original.at<Vec3b>(row, col)[0] == 255 && 
    original.at<Vec3b>(row, col)[1] == 0 &&
    original.at<Vec3b>(row, col)[2] == 0 && row >= max_y.x) {
    //if yes we have new value for the max_y, but we don't break because we are looking for the biggest blue pixel
    //we continue to search until the end of the picture
    max_y = Point(row, col);
                }
            }
        }
    //Here I want to make green points on the pixels we have found
    circle(original, min_x, 3, Scalar(0, 255, 0), 8, LINE_AA);
    circle(original, max_x, 3, Scalar(0, 255, 0), 8, LINE_AA);
    circle(original, min_y, 3, Scalar(0, 255, 0), 8, LINE_AA);
    circle(original, max_y, 3, Scalar(0, 255, 0), 8, LINE_AA);
}

我最后收到的点是完全随机的。

如果有人可以提供帮助,我将不胜感激! :)

2 个答案:

答案 0 :(得分:3)

如@nivpeled所述,但在代码中。

void findBluePixels(Mat& original) {
    Point min_x = Point(original.cols, original.rows);
    Point min_y = Point(original.cols, original.rows);
    Point max_x = Point(-1, -1);
    Point max_y = Point(-1, -1);
    //looking for the most left blue pixel (the blue pixel with the smallest x value)
    //we are going through every column and every row

    const Vec3b blueColor(255, 0, 0);
    for (int row = 0; row < original.rows; row++) {
        for (int col = 0; col < original.cols; col++) {
            if (blueColor != original.at<Vec3b>(row, col))
                continue;

            if (min_x.x > col) {
                min_x = Point(col, row);
            }
            if (max_x.x < col) {
                max_x = Point(col, row);
            }

            if (min_y.y > row) {
                min_y = Point(col, row);
            }
            if (max_y.y < row) {
                max_y = Point(col, row);
            }
        }
    }
    //Here I want to make green points on the pixels we have found
    circle(original, min_x, 13, Scalar(0, 255, 0), 8, LINE_AA);
    circle(original, max_x, 13, Scalar(0, 255, 0), 8, LINE_AA);
    circle(original, min_y, 13, Scalar(0, 255, 0), 8, LINE_AA);
    circle(original, max_y, 13, Scalar(0, 255, 0), 8, LINE_AA);
}

result

加上一些建议:

•进行循环首先是行,然后是列。如果您顺序访问数据,计算机的运行速度将大大提高。示例访问顺序编号:

Faster ->
[ 1 2 3 ] 
[ 4 5 6 ] 
[ 7 8 9 ] 
Slower ->
[ 1 4 7 ] 
[ 2 5 8 ] 
[ 3 6 9 ]

答案 1 :(得分:1)

  1. 您只能在矩阵上循环一次,并在同一(内部)循环体中进行所有检查。

  2. 更喜欢使用名称而不是数字。例如if(original.at(row,col)[BLUE] == 255而不是if(original.at(row,col)[0] == 255

  3. 请仔细检查像素中的索引0确实具有蓝色值(不是RGB吗?)

  4. 逻辑问题:如果您要查找例如最小x,您应编写代​​码(col

    如果(... && col min_x =点(行,列); 打破; }

并按如下所示进行初始化:

min_x = Point(MAX_COL, 0);
max_x = Point(MIN_COL, 0);
min_y = Point(0, MAX_ROW);
max_y = Point(0, MIN_ROW);