python opencv簡易パネルを実現します。


python-opencvは簡単な画板を実現します。参考にしてください。具体的な内容は以下の通りです。

# -*- coding: utf-8 -*-
"""
Created on Sat May 19 17:34:54 2018

@author: xxx
"""

import cv2 as cv
import numpy as np


def nothing(x):
  pass

#          True
drawing = False
#    mode   True     。   'm'       
mode = True
ix, iy = -1, -1

#      
def draw_circle(event, x, y, flags, param):
  r = cv.getTrackbarPos('R', 'image')
  g = cv.getTrackbarPos('G', 'image')
  b = cv.getTrackbarPos('B', 'image')
  color = (b, g, r)

  global ix, iy, drawing, mode
  #               
  if event == cv.EVENT_LBUTTONDOWN:
    drawing = True
    ix, iy = x, y
#                   。event       , flag       
  elif event == cv.EVENT_MOUSEMOVE and flags == cv.EVENT_FLAG_LBUTTON:
    if drawing == True:
      if mode == True:
        cv.rectangle(img, (ix, iy), (x, y), color, -1)
      else:
        #     ,           ,3       
        cv.circle(img, (ix, iy), 3, color, -1)
        #               ,        
#        r = int(np.sqrt((x - ix)**2 + (y - iy)**2))
#        cv.circle(img, (x, y), r, (0, 0, 255), -1)
#             
  elif event == cv.EVENT_LBUTTONUP:
      drawing == False
#      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)

#        
img = np.zeros((512, 512, 3), np.uint8)
cv.namedWindow('image')

cv.createTrackbar('R', 'image', 0, 255, nothing)
cv.createTrackbar('G', 'image', 0, 255, nothing)
cv.createTrackbar('B', 'image', 0, 255, nothing)
cv.setMouseCallback('image', draw_circle)

while(1):
  cv.imshow('image', img)
  k = cv.waitKey(1)&0xFF
  if k == ord('m'):
    mode = not mode
  elif k==27:
    break


cv.destroyAllWindow()
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。