用C++和OpenCV查找最左边、最右边、最高、最蓝的像素 我试图用C++和OpenCV编写代码,识别哪一个蓝色像素从左、右、上、下都是第一个。 我正在使用的图片是:
我想收到的是这样一张照片: 这是我的代码,直到现在,我真的不明白为什么它不工作用C++和OpenCV查找最左边、最右边、最高、最蓝的像素 我试图用C++和OpenCV编写代码,识别哪一个蓝色像素从左、右、上、下都是第一个。 我正在使用的图片是: ,c++,opencv,C++,Opencv,我想收到的是这样一张照片: 这是我的代码,直到现在,我真的不明白为什么它不工作 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 =
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);
}
我最后收到的点完全是随机的
如果有人能帮忙,我会非常感激的!:
您只能在矩阵上循环一次,并在同一内部for循环体中执行所有检查
喜欢使用名字而不是数字。e、 g.如果是original.atrow,则列[BLUE]==255,而不是如果是original.atrow,则列[0]==255
请仔细检查像素中的索引0是否确实包含蓝色值,不是RGB吗
逻辑问题:如果你想找到例如最小x,你应该编码colmin_x = Point(MAX_COL, 0);
max_x = Point(MIN_COL, 0);
min_y = Point(0, MAX_ROW);
max_y = Point(0, MIN_ROW);
正如@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);
}
不知道,但是你应该提取一个函数bool is_blue const Pixel&使代码更可读和简洁。对不起,我觉得你的逻辑完全是随机的。不管怎么说,这就是调试器的作用,使用调试器可以准确地找到程序偏离预期的时间和地点。您没有在帖子中指定坐标系,这使得很难确定。但是Cal>=Max x.y比较一个列,我认为它是一个x坐标,带有y坐标。那没有意义。与所有其他比较一样,您似乎混淆了x和y。您的符号有问题。。如果x从左到右,y从上到下或相反,w/e、col对应于x值,而row对应于y值。如果这不是您想要的符号,则最左边和最右边的定义是错误的。您的min_x和max_x代码是相同的。一团糟>=非常感谢您提供的解决方案和提示!我会使蓝色成为常量,甚至是静态常量。
Faster ->
[ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]
Slower ->
[ 1 4 7 ]
[ 2 5 8 ]
[ 3 6 9 ]