Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw an external rectangle with python opencv

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use python opencv to draw an external rectangular frame". In daily operation, I believe that many people have doubts about how to use python opencv to draw an external rectangular frame. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful for you to answer the doubt of "how to use python opencv to draw an external rectangular frame". Next, please follow the editor to study!

A rectangular frame is attached to the painting, which can be drawn either as the largest or separately.

#-*-coding: utf-8-*-import cv2 image = cv2.imread ('Granger Unique 110w2) print (image.shape) print (image.shape [0]) # hprint (image.shape [1]) # w# image to grayscale image img = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY) # cv2.imwrite (' GlaxoSigma 110w2max maskyogtif4) img) # image to binary ret, thresh = cv2.threshold (img, 2,255) Cv2.THRESH_BINARY_INV) contours, hierarchy = cv2.findContours (thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) x1 = [] y1 = [] x2 = [] for c in contours: # find the boundary coordinates x, y, w, h = cv2.boundingRect (c) # calculate the outermost rectangular boundary print (x, y, w, h) # because it contains The largest frame of the image itself, so if is used to remove the value of the image itself. If x! = 0 and y! = 0 and w! = image.shape [1] and h! = image.shape [0]: # the coordinates of the upper left corner and the lower right corner # if you execute the frame inside, it will be drawn separately Cv2.rectangle (image, (x, y), (x + w, y + h), (0,255,0), 1) x1.append (x) y1.append (y) x2.append (x + w) y2.append (y + h) x11 = min (x1) y11 = min (y1) x22 = max (x2) y22 = max (y2) # all targets are drawn and can be included An external rectangle. # cv2.rectangle (image, (x11, y11), (x22, Y22), (0,0,255), 1) # cv2.imshow ("img", image) cv2.imwrite ('Granger 110w2png, image) cv2.waitKey (0) # function: cv2.findContours () function to find the outline of the detected object. # Parameter: # Parameter 1: find the outline of the image, the received parameters are binary image, that is, black and white (not grayscale image), so the read image should first be converted to grayscale, and then converted to binary image # Parameter 2: outline retrieval mode, there are four kinds. # cv2.RETR_EXTERNAL means only the outer contour is detected; the contour detected by # cv2.RETR_LIST does not establish a hierarchical relationship; and # cv2.RETR_CCOMP establishes two levels of contours, with the upper layer as the outer boundary and the inner layer as the inner hole boundary information. If there is a connected object in the inner hole, the boundary of the object is also at the top. # cv2.RETR_TREE creates the outline of a hierarchical tree structure. # # Parameter 3: contour approximation. # cv2.CHAIN_APPROX_NONE stores all contour points, and the pixel position difference between the two adjacent points does not exceed 1, that is, max (abs (x1-x2), abs (y2-y1)) = = compressed horizontal, vertical and diagonal elements of cv2.CHAIN_APPROX_SIMPLE, keeping only the end coordinates of that direction. For example, a rectangular outline only needs four points to save the outline information # cv2.CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS uses the teh-Chinl chain approximation algorithm # Note: opencv2 returns two values: contours:hierarchy. Opencv3 returns three values, which are img, countours, and hierarchy##. The # cv2.findContours () function returns two values, one is the outline itself, and the other is the attribute corresponding to each outline. At this point, the study on "how to draw an external rectangular frame with python opencv" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report