I am trying to write a code using c++ and opencv that would recognize which blue pixel comes first from left, right, up and bottom.
The picture I am using is this one:
And what I want to receive is a picture like this:
This is my code until now and I don't really understand why it doesn't work.
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);
}
The dots I receive at the end are totaly random.
I would really appreciate it if someone can help! :)