SoFunction
Updated on 2024-11-15

Complete code for python opencv to draw an external rectangular box

Draw external rectangular boxes, either as one largest or separately.

# -*- coding: utf-8 -*-
 
import cv2
 
image = ('G:/110w2/mask_tif4/')
 
print()
print([0]) # h
print([1]) # w
# Image to grayscale
img = (image, cv2.COLOR_BGR2GRAY)
#('G:/110w2/mask_tif4/', img)
# image to binary map
ret, thresh = (img, 2, 255, cv2.THRESH_BINARY_INV)
contours, hierarchy = (thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
x1 = []
y1 = []
x2 = []
y2 = []
for c in contours:
    # Find the boundary coordinates
    x, y, w, h = (c)  # Calculate the outermost rectangular boundary of the point set
    print(x, y, w, h)
    # Because this contains, the largest box of the image itself, the if is used to exclude the value of the image itself.
    if x != 0 and y != 0 and w != [1] and h != [0]:
        # Upper left and lower right coordinates
        # If you execute this frame inside, it's to draw it separately #
        (image, (x, y), (x + w, y + h), (0, 255, 0), 1)
        (x)
        (y)
        (x + w)
        (y + h)
x11 = min(x1)
y11 = min(y1)
x22 = max(x2)
y22 = max(y2)
 
# This draws all the targets that can be included, an outer rectangular box.
#(image, (x11, y11), (x22, y22), (0, 0, 255), 1)
 
#("img", image)
('G:/110w2/mask_tif4/0_001.png', image)
(0)
 
 
# Function: () function to find the outline of the detected object.
# Parameters.
# Parameter 1: the image to find the contour, the received parameter is a binary map, i.e., black and white (not grayscale), so the read image has to be converted to grayscale first, and then to a binary map
# Parameter 2: Retrieval mode of the profile, there are four types.
# cv2.RETR_EXTERNAL indicates that only the outer contour is detected; # cv2.RETR_EXTERNAL indicates that only the outer contour is detected.
# cv2.RETR_LIST Detected contours do not establish hierarchical relationships; # cv2.RETR_LIST Detected contours do not establish hierarchical relationships.
# cv2.RETR_CCOMP Creates two levels of contours, with the top level being the outer boundary and the inner level being the boundary information for the bore. If there is another connected object inside the bore, the boundary of this object is also on the top layer.
# cv2.RETR_TREE Create an outline of a hierarchical tree structure.
#
# Parameter 3: Approximation of contours.
# cv2.CHAIN_APPROX_NONE Store all contour points where the pixel position difference between two neighboring points is no more than 1, i.e. max(abs(x1-x2), abs(y2-y1)) == 1
# cv2.CHAIN_APPROX_SIMPLE Compresses elements in the horizontal, vertical, and diagonal directions, retaining only the endpoint coordinates in that direction, e.g., a rectangular outline needs only 4 points to save the outline information
# cv2.CHAIN_APPROX_TC89_L1, CV_CHAIN_APPROX_TC89_KCOS using teh-Chinl chain approximation algorithm
# Note: opencv2 returns two values: contours: hierarchy. opencv3 returns three values: img, countours, hierarchy.
#
# Return Value
#()The function returns two values,One is the silhouette itself,There is also a property that corresponds to each profile。

to this article on python opencv draw external rectangular box of the complete code is introduced to this article, more related python opencv rectangular box content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!