【问题标题】:Python cv2 90:90 aspect ratio rectangle to crop imagesPython cv2 90:90 长宽比矩形裁剪图像
【发布时间】:2019-12-06 07:53:18
【问题描述】:

我正在编写一个程序来裁剪图像,我的问题是我希望该矩形具有特定的纵横比 (90:90),是否可以制作矩形而不是鼠标,使用鼠标滚轮更改其大小和点击鼠标确认?

import cv2
import numpy as np

cropping = False

x_start, y_start, x_end, y_end = 0, 0, 0, 0

image = cv2.imread('example.jpg')
oriImage = image.copy()


def mouse_crop(event, x, y, flags, param):
    # grab references to the global variables
    global x_start, y_start, x_end, y_end, cropping

    # if the left mouse button was DOWN, start RECORDING
    # (x, y) coordinates and indicate that cropping is being
    if event == cv2.EVENT_LBUTTONDOWN:
        x_start, y_start, x_end, y_end = x, y, x, y
        cropping = True

    # Mouse is Moving
    elif event == cv2.EVENT_MOUSEMOVE:
        if cropping == True:
            x_end, y_end = x, y

    # if the left mouse button was released
    elif event == cv2.EVENT_LBUTTONUP:
        # record the ending (x, y) coordinates
        x_end, y_end = x, y
        cropping = False # cropping is finished

        refPoint = [(x_start, y_start), (x_end, y_end)]

        if len(refPoint) == 2: #when two points were found
            roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
            cv2.imshow("Cropped", roi)

cv2.namedWindow("image")
cv2.setMouseCallback("image", mouse_crop)

while True:

    i = image.copy()

    if not cropping:
        cv2.imshow("image", image)

    elif cropping:
        cv2.rectangle(i, (x_start, y_start), (x_end, y_end), (255, 0, 0), 2)
        cv2.imshow("image", i)

    cv2.waitKey(1)

# close all open windows
cv2.destroyAllWindows()

【问题讨论】:

    标签: python crop rectangles aspect-ratio


    【解决方案1】:

    这是您可以用来执行此操作的基本结构。在第一次单击时创建一个初始矩形,通过向上或向下滚动来调整矩形的大小,然后通过再次单击来完成裁剪。当矩形超出图像边界和“小于零”时,您需要添加一些检查来限制矩形。

    # if the left mouse button was released
    if event == cv2.EVENT_LBUTTONUP:
        # create rectangle
        if not cropping:
            x_start, y_start = x-20, y-20
            x_end, y_end = x+20, y+20
            cropping = True # cropping is finished
        # finish cropping
        else:
            refPoint = [(x_start, y_start), (x_end, y_end)]
    
            if len(refPoint) == 2: #when two points were found
                roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
                cv2.imshow("Cropped", roi)
    
    elif event == cv2.EVENT_MOUSEWHEEL:
        print(flags)
        # scroll up
        if flags > 0:
            x_start, y_start = x_start-2, y_start-2
            x_end, y_end = x_end+2, y_end+2
        # scroll down
        else:
            x_start, y_start = x_start+2, y_start+2
            x_end, y_end = x_end-2, y_end-2
    

    【讨论】:

      【解决方案2】:

      谢谢 :) 但我还有一个问题,我的代码现在看起来像这样:

      import cv2
      import numpy as np
      
      cropping = False
      
      x_start, y_start, x_end, y_end = 0, 0, 0, 0
      
      image = cv2.imread('example.jpg')
      oriImage = image.copy()
      
      
      def mouse_crop(event, x, y, flags, param):
          # grab references to the global variables
          global x_start, y_start, x_end, y_end, cropping
      
          # if the left mouse button was DOWN, start RECORDING
          # (x, y) coordinates and indicate that cropping is being
          if event == cv2.EVENT_MOUSEMOVE:
              x_start, y_start, x_end, y_end = x, y, x+96, y+96
              cropping = True
      
          # Mouse is Moving
       #   elif event == cv2.EVENT_MOUSEMOVE:
       #       if cropping == True:
       #           x_end, y_end = x, y
      
          # if the left mouse button was released
          elif event == cv2.EVENT_LBUTTONUP:
              # record the ending (x, y) coordinates
      #        x_end, y_end = x, y
              cropping = False # cropping is finished
      
              refPoint = [(x_start, y_start), (x_end, y_end)]
      
              if len(refPoint) == 2: #when two points were found
                  roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
                  cv2.imshow("Cropped", roi)
      
          elif event == cv2.EVENT_MOUSEWHEEL:
              print(flags)
              # scroll up
              if flags > 0:
                  x_start, y_start = x_start-15, y_start-15
                  x_end, y_end = x_end+15, y_end+15
              # scroll down
              else:
                  x_start, y_start = x_start+15, y_start+15
                  x_end, y_end = x_end-15, y_end-15
      
      cv2.namedWindow("image")
      cv2.setMouseCallback("image", mouse_crop)
      
      while True:
      
          i = image.copy()
      
          if not cropping:
              cv2.imshow("image", image)
      
          elif cropping:
              cv2.rectangle(i, (x_start, y_start), (x_end, y_end), (255, 0, 0), 2)
              cv2.imshow("image", i)
      
          cv2.waitKey(1)
      
      # close all open windows
      cv2.destroyAllWindows()
      
      

      我的问题是:矩形跟随我的鼠标,我可以用鼠标滚轮把它变大变小,但是如果我把它变大并且鼠标移动,它又变小了

      【讨论】:

      • 您只需要 LBUTTONUP 和 MOUSEWHEEL 事件。如果您删除 MOUSEMOVE 事件处理程序,它应该可以工作。
      • 如果我删除 MOUSEMOVE,则不再有矩形。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      相关资源
      最近更新 更多