<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
話不多說,直接上程式碼
import copy import cv2 import numpy as np WIN_NAME = 'draw_rect' class Rect(object): def __init__(self): self.tl = (0, 0) self.br = (0, 0) def regularize(self): """ make sure tl = TopLeft point, br = BottomRight point """ pt1 = (min(self.tl[0], self.br[0]), min(self.tl[1], self.br[1])) pt2 = (max(self.tl[0], self.br[0]), max(self.tl[1], self.br[1])) self.tl = pt1 self.br = pt2 class DrawRects(object): def __init__(self, image, color, thickness=1): self.original_image = image self.image_for_show = image.copy() self.color = color self.thickness = thickness self.rects = [] self.current_rect = Rect() self.left_button_down = False @staticmethod def __clip(value, low, high): """ clip value between low and high Parameters ---------- value: a number value to be clipped low: a number low limit high: a number high limit Returns ------- output: a number clipped value """ output = max(value, low) output = min(output, high) return output def shrink_point(self, x, y): """ shrink point (x, y) to inside image_for_show Parameters ---------- x, y: int, int coordinate of a point Returns ------- x_shrink, y_shrink: int, int shrinked coordinate """ height, width = self.image_for_show.shape[0:2] x_shrink = self.__clip(x, 0, width) y_shrink = self.__clip(y, 0, height) return (x_shrink, y_shrink) def append(self): """ add a rect to rects list """ self.rects.append(copy.deepcopy(self.current_rect)) def pop(self): """ pop a rect from rects list """ rect = Rect() if self.rects: rect = self.rects.pop() return rect def reset_image(self): """ reset image_for_show using original image """ self.image_for_show = self.original_image.copy() def draw(self): """ draw rects on image_for_show """ for rect in self.rects: cv2.rectangle(self.image_for_show, rect.tl, rect.br, color=self.color, thickness=self.thickness) def draw_current_rect(self): """ draw current rect on image_for_show """ cv2.rectangle(self.image_for_show, self.current_rect.tl, self.current_rect.br, color=self.color, thickness=self.thickness) def onmouse_draw_rect(event, x, y, flags, draw_rects): if event == cv2.EVENT_LBUTTONDOWN: # pick first point of rect print('pt1: x = %d, y = %d' % (x, y)) draw_rects.left_button_down = True draw_rects.current_rect.tl = (x, y) if draw_rects.left_button_down and event == cv2.EVENT_MOUSEMOVE: # pick second point of rect and draw current rect draw_rects.current_rect.br = draw_rects.shrink_point(x, y) draw_rects.reset_image() draw_rects.draw() draw_rects.draw_current_rect() if event == cv2.EVENT_LBUTTONUP: # finish drawing current rect and append it to rects list draw_rects.left_button_down = False draw_rects.current_rect.br = draw_rects.shrink_point(x, y) print('pt2: x = %d, y = %d' % (draw_rects.current_rect.br[0], draw_rects.current_rect.br[1])) draw_rects.current_rect.regularize() draw_rects.append() if (not draw_rects.left_button_down) and event == cv2.EVENT_RBUTTONDOWN: # pop the last rect in rects list draw_rects.pop() draw_rects.reset_image() draw_rects.draw() if __name__ == '__main__': #image = np.zeros((256, 256, 3), np.uint8) image = cv2.imread("111.jpg") draw_rects = DrawRects(image, (0, 255, 0), 2) cv2.namedWindow(WIN_NAME, 0) cv2.setMouseCallback(WIN_NAME, onmouse_draw_rect, draw_rects) while True: cv2.imshow(WIN_NAME, draw_rects.image_for_show) key = cv2.waitKey(30) if key == 27: # ESC break cv2.destroyAllWindows()
執行效果
補充
當然Python+OpenCV不僅能做到在影象上繪製任意大小矩形,還能實現滑鼠點選影象時會顯示其座標值
下面是實現程式碼
import cv2 import numpy as np img = cv2.imread("111.jpg") # print img.shape def on_EVENT_LBUTTONDOWN(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: xy = "%d,%d" % (x, y) print xy cv2.circle(img, (x, y), 1, (255, 0, 0), thickness=-1) cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), thickness=1) cv2.imshow("image", img) cv2.namedWindow("image",cv2.WINDOW_KEEPRATIO) cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN) cv2.imshow("image", img) while (True): try: cv2.waitKey(100) except Exception: cv2.destroyWindow("image") break cv2.waitKey(0) cv2.destroyAllWindow()
執行結果:
以上就是Python+OpenCV實現在影象上繪製矩形的詳細內容,更多關於Python OpenCV的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45