简体   繁体   中英

How to get image from bounding box

How would I extract the bounding boxes from this image?

Here's my code

tf.disable_v2_behavior()
import cv2
import pathlib

model_path = "/Users/ebayb/Desktop/ndir/Model"

# path to the folder containing images
image_path = "/Users/ebayb/Desktop/top-cube/testing/2"

session = tf.Session(graph=tf.Graph())

tf.saved_model.loader.load(session, ['serve'], model_path)

# extract the coordinates of the rectange that's to be drawn on the image
def draw_boxes(height, width, box, img):
  # starting coordinates of the box
  ymin = int(max(1, (box[0] * height)))
  xmin = int(max(1, (box[1] * width)))
  # last coordinates of the box
  ymax = int(min(height, (box[2] * height)))
  xmax = int(min(width, (box[3] * width)))
  coordinates_list=[ymin, ymax, xmin, xmax]
  print(f"cor: {coordinates_list}")
  # draw a rectange using the coordinates
  cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (10, 255, 0), 2)
  

for file in pathlib.Path(image_path).iterdir():
  # get the current image path
  current_image_path = r"{}".format(file.resolve())
  
  
  img_bytes = open(current_image_path, 'rb').read()
  
  
  result = session.run(['detection_boxes:0', 'detection_scores:0'], feed_dict={'encoded_image_string_tensor:0': [img_bytes]})
  
  boxes = result[0][0]
  scores = result[1][0]
  
  print("For file {}".format(file.stem))

 
  img = cv2.imread(current_image_path)
  
  imH, imW, _ = img.shape
               
  for i in range(len(scores)):
    # only consider a detected object if it's probability is above 25%
    if scores[i] > 0.258:
      print("The box {} has probability {}".format(boxes[i], scores[i]))
      draw_boxes(imH, imW, boxes[i], img)
      
      
      
  
  

  cv2.imwrite("bound.jpg", img)

And I'm wondering how would I get the bounding box from it? I've tried using ROI from some other answers on other threads but that doesn't seem to work and it'll just get other images

(Example of ROI doing it) https://gyazo.com/5478ff4715311c0422d1dfbad7f03d18

Here's the code for the ROI:

import numpy as np

# Load image, grayscale, Otsu's threshold 
image = cv2.imread('test.jpg')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 100, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find contours, obtain bounding box, extract and save ROI
ROI_number = 0
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
    ROI = original[y:y+h, x:x+w]
    cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
    ROI_number += 1

cv2.imshow('image', image)
cv2.waitKey()

Here is the test image I'm using在此处输入图片说明

You are already drawing rectangles in the first code. If you want the regions with rectangles then modify the first code to crop original image with xmin, ymin, xmax, ymax values.

For each rectangle drawn fulfilling scores[i] > 0.258 , use above values to crop part of original image in draw_boxes function and save. No need to use contours then.

Use Non-max Suppression for overlapping bounding boxes if any.

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