简体   繁体   中英

Constract bounding boxes from encoded pixels in python

What is the efficient way to constract bounding boxes for an objeject given the encoded pixel of that object. I am trying to convert a task from segmentation to object detection in yolo. Any suggestions are welcomed.

If you have the points of the segmentation then you can find the extents of the points by iterating through and recording the lowest and highest values of [x,y]. This will give you the the top left and bottom right corners.

If you're trying to work backwards from a colored, segmented image then you can use opencv to threshold the colors and get boxes from the blobs like this:

在此处输入图像描述

import cv2
import numpy as np

# load image
img = cv2.imread("mask.png");
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);

# threshold
thresh = cv2.inRange(gray, 100, 255);

# contour
_, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE);

# draw rectangle
x,y,w,h = cv2.boundingRect(contours[0]);
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2);

# show
cv2.imshow("box", img);
cv2.imshow("Thresh", thresh);
cv2.waitKey(0);

# save
cv2.imwrite("boxed.png", img);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM