In our last example, we drew filled rectangle. You modify the code to draw an unfilled rectangle.

2024-01-13 11:59

本文主要是介绍In our last example, we drew filled rectangle. You modify the code to draw an unfilled rectangle.,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 一、last example
  • 二、modify the code to draw an unfilled rectangle.
    • 1.修改方法
      • 第一步
      • 第二步


前言

题目地址为:https://docs.opencv.org/4.x/db/d5b/tutorial_py_mouse_handling.html
题目内容:In our last example, we drew filled rectangle. You modify the code to draw an unfilled rectangle.

一、last example

完整代码如下:

import numpy as np
import cv2 as cv
drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1
# mouse callback function
def draw_circle(event,x,y,flags,param):global ix,iy,drawing,modeif event == cv.EVENT_LBUTTONDOWN:drawing = Trueix,iy = x,yelif event == cv.EVENT_MOUSEMOVE:if drawing == True:if mode == True:cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)else:cv.circle(img,(x,y),5,(0,0,255),-1)elif event == cv.EVENT_LBUTTONUP:drawing = Falseif mode == True:cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)else:cv.circle(img,(x,y),5,(0,0,255),-1)img = np.zeros((512,512,3), np.uint8)
cv.namedWindow('image')
cv.setMouseCallback('image',draw_circle)
while(1):cv.imshow('image',img)k = cv.waitKey(1) & 0xFFif k == ord('m'):mode = not modeelif k == 27:break
cv.destroyAllWindows()

效果图:
效果图

二、modify the code to draw an unfilled rectangle.

1.修改方法

第一步

首先将cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)cv.circle(img,(x,y),5,(0,0,255),-1)中最后一个参数修改,-1代表填充满,如果最后一个参数修改为正整数例如将最后一个参数修改为5,则代表的意思是绘制矩形时的线宽度为5

第二步

注释掉绑定鼠标移动时绘制矩形的事件。就是注释掉这一部分:

# elif event == cv.EVENT_MOUSEMOVE:#     if drawing == True:#         if mode == True:#             cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)#         else:#             cv.circle(img,(x,y),5,(0,0,255),-1)

修改后的完整代码如下:

import numpy as np
import cv2 as cv
drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1
# mouse callback function
def draw_circle(event,x,y,flags,param):global ix,iy,drawing,modeif event == cv.EVENT_LBUTTONDOWN:drawing = Trueix,iy = x,y# elif event == cv.EVENT_MOUSEMOVE:#     if drawing == True:#         if mode == True:#             cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)#         else:#             cv.circle(img,(x,y),5,(0,0,255),-1)elif event == cv.EVENT_LBUTTONUP:drawing = Falseif mode == True:cv.rectangle(img,(ix,iy),(x,y),(0,255,0),5) else:cv.circle(img,(x,y),5,(0,0,255),5)img = np.zeros((512,512,3), np.uint8)
cv.namedWindow('image')
cv.setMouseCallback('image',draw_circle)
while(1):cv.imshow('image',img)k = cv.waitKey(1) & 0xFFif k == ord('m'):mode = not modeelif k == 27:break
cv.destroyAllWindows()

效果图:
效果图

这篇关于In our last example, we drew filled rectangle. You modify the code to draw an unfilled rectangle.的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/601390

相关文章

Debugging Lua Project created in Cocos Code IDE creates “Waiting for debugger to connect” in Win-7

转自 I Installed Cocos Code IDE and created a new Lua Project. When Debugging the Project(F11) the game window pops up and gives me the message waiting for debugger to connect and then freezes. Also a

LLVM入门2:如何基于自己的代码生成IR-LLVM IR code generation实例介绍

概述 本节将通过一个简单的例子来介绍如何生成llvm IR,以Kaleidoscope IR中的例子为例,我们基于LLVM接口构建一个简单的编译器,实现简单的语句解析并转化为LLVM IR,生成对应的LLVM IR部分,代码如下,文件名为toy.cpp,先给出代码,后面会详细介绍每一步分代码: #include "llvm/ADT/APFloat.h"#include "llvm/ADT/S

Linux内置的审计跟踪工具:last命令

如果你是一个服务器管理员,你或许知道你要保护你的服务器的话,不仅是从外部,还要从内部保护。Linux有一个内置工具来看到最后登陆服务器的用户,可以帮助你保护服务器。   这个命令是last。它对于追踪非常有用。让我们来看一下last可以为你做些什么。   last命令的功能是什么   last显示的是自/var/log/wtmp文件创建起所有登录(和登出)的用户。这个文件是二进制

VS Code 调试go程序的相关配置说明

用 VS code 调试Go程序需要在.vscode/launch.json文件中增加如下配置:  // launch.json{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information,

code: 400, msg: Required request body is missing 错误解决

引起这个错误的原因是,请求参数按照get方式给。 应该给json字符串才对 补充: 1. @RequestBody String resource 加@RequestBody必须给json字符串,否则会报错400,记如标题错误。 不加这个的进行请求的话,其实post和get就没有什么区别了。 2. List<String> indexCodes=(List<String>)json.

iOS项目发布提交出现invalid code signing entitlements错误。

1、进入开发者账号,选择App IDs,找到自己项目对应的AppId,点击进去编辑, 2、看下错误提示出现  --Specifically, value "CVYZ6723728.*" for key "com.apple.developer.ubiquity-container-identifiers" in XX is not supported.-- 这样的错误提示 将ubiquity

Matlab draw a sector 画一个扇形

Matlab draw a sector 画一个扇形 Matlab的函数代码: function [ sector ] = Draw_a_sector( map, center,StartR, EndR, StartAngle, EndAngle )%% Get indexs(row,column)size_map=size(map);for i = 1:size_map(2)index

解决服务器VS Code中Jupyter突然崩溃的问题

问题 本来在服务器Anaconda的Python环境里装其他的包,装完了想在Jupyter里写代码验证一下有没有装好,一运行发现Jupyter崩溃了!?报错如下所示 Failed to start the Kernel. ImportError: /home/hujh/anaconda3/envs/mia/lib/python3.12/lib-dynload/_sqlite3.cpython-

数据标注:批量转换json文件,出现AttributeError: module ‘labelme.utils‘ has no attribute ‘draw_label‘错误

labelme版本更换为3.11.2 "D:\Anaconda3\Lib\site-packages\labelme\utils\draw.py"缺失?: import ioimport os.path as ospimport numpy as npimport PIL.Imageimport PIL.ImageDrawimport PIL.ImageFontdef label_co

Behind the Code:与 Rakic 和 Todorovic 对话 OriginTrail 如何实现 AI 去中心化

原文:https://www.youtube.com/watch?v=ZMuLyLCtE3s&list=PLtyd7v_I7PGnko80O0LCwQQsvhwAMu9cv&index=12 作者:The Kusamarian 编译:OneBlock+ 随着人工智能技术的飞速发展,一系列前所未有的挑战随之而来:模型的衰退与互联网的潜在威胁愈发明显。AI 的增长曲线可能因训练过程中的瓶颈而趋于平