Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ draw_mask_bbox The Rect r maybe out of bounds #1404

Closed
a819411321 opened this issue Nov 13, 2023 · 7 comments
Closed

C++ draw_mask_bbox The Rect r maybe out of bounds #1404

a819411321 opened this issue Nov 13, 2023 · 7 comments

Comments

@a819411321
Copy link

Uploading image.png…

@a819411321
Copy link
Author

for (size_t i = 0; i < dets.size(); i++) {
cv::Mat img_mask = scale_mask(masks[i], img);
auto color = colors[(int)dets[i].class_id % colors.size()];
auto bgr = cv::Scalar(color & 0xFF, color >> 8 & 0xFF, color >> 16 & 0xFF);
cv::Rect r = get_rect(img, dets[i].bbox);
for (int x = r.x; x < r.x + r.width; x++) {
for (int y = r.y; y < r.y + r.height; y++) {
//need add
if (x >= img.cols||y >= img.rows) {
continue;
}
float val = img_mask.at(y, x);
if (val <= 0.5) continue;
img.atcv::Vec3b(y, x)[0] = img.atcv::Vec3b(y, x)[0] / 2 + bgr[0] / 2;
img.atcv::Vec3b(y, x)[1] = img.atcv::Vec3b(y, x)[1] / 2 + bgr[1] / 2;
img.atcv::Vec3b(y, x)[2] = img.atcv::Vec3b(y, x)[2] / 2 + bgr[2] / 2;
}
}
cv::rectangle(img, r, bgr, 2);
// Get the size of the text
cv::Size textSize = cv::getTextSize(labels_map[(int)dets[i].class_id] + " " + to_string_with_precision(dets[i].conf), cv::FONT_HERSHEY_PLAIN, 1.2, 2, NULL);
// Set the top left corner of the rectangle
cv::Point topLeft(r.x, r.y - textSize.height);
// Set the bottom right corner of the rectangle
cv::Point bottomRight(r.x + textSize.width, r.y + textSize.height);
// Set the thickness of the rectangle lines
int lineThickness = 2;
// Draw the rectangle on the image
cv::rectangle(img, topLeft, bottomRight, bgr, -1);
cv::waitKey(0);
cv::putText(img, labels_map[(int)dets[i].class_id] + " " + to_string_with_precision(dets[i].conf), cv::Point(r.x, r.y + 4), cv::FONT_HERSHEY_PLAIN, 1.2, cv::Scalar::all(0xFF), 2);
}
}

@wang-xinyu
Copy link
Owner

What does this mean? Did you find a bug? Have you fix it?

@wxd9199
Copy link

wxd9199 commented Nov 15, 2023

多谢!加了这个解决了崩溃的问题
//need add
if (x >= img.cols||y >= img.rows) {
continue;
}

@a819411321
Copy link
Author

What does this mean? Did you find a bug? Have you fix it?

是的 运行官方的yolov5s-seg模型和推理bus.jpg图的时候,这会报错,查看错误,发现是因为像素的地址超过了图像的范围。

@wang-xinyu
Copy link
Owner

@a819411321 Can you help raise a PR to fix this?

@oulton
Copy link

oulton commented May 20, 2024

多谢!加了这个解决了崩溃的问题 //need add if (x >= img.cols||y >= img.rows) { continue; }

cv::Rect r = get_rect(img, dets[i].bbox);
this r out of bounds!!!!!

modify get_rect

int x = round(l);
int y = round(t);
int w = round(r - l);
int h = round(b - t);
if(x < 0) x = 0;
if(y < 0) y = 0;
if(x + w > img.cols) w = img.cols - x;
if(y + h > img.rows) h = img.rows - y;
return cv::Rect(x, y, w, h);
// return cv::Rect(round(l), round(t), round(r - l), round(b - t));

@oulton
Copy link

oulton commented May 20, 2024

多谢!加了这个解决了崩溃的问题 //need add if (x >= img.cols||y >= img.rows) { continue; }

x ,y will still be less than 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants