Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/66055809/constract-bounding-boxes-from-encoded-pixels-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...