OpenCV python裁剪图像
我创建了一个黑色的图像,然后在这个图像中画了一个红色的矩形。之后,我裁剪了这幅图像,并使用命令在裁剪后的图像中绘制了另一个矩形<代码>cv2.矩形(裁剪,(50,50),(150150),(0,0255),3) 为什么当我在最后显示第二个矩形时,它会出现在原始图像中?我希望看到的只是第一个矩形OpenCV python裁剪图像,python,image,opencv,Python,Image,Opencv,我创建了一个黑色的图像,然后在这个图像中画了一个红色的矩形。之后,我裁剪了这幅图像,并使用命令在裁剪后的图像中绘制了另一个矩形cv2.矩形(裁剪,(50,50),(150150),(0,0255),3) 为什么当我在最后显示第二个矩形时,它会出现在原始图像中?我希望看到的只是第一个矩形 import cv2 import numpy as np #create image image = np.zeros((400,400,3), np.uint8) #draw rectangle into
import cv2
import numpy as np
#create image
image = np.zeros((400,400,3), np.uint8)
#draw rectangle into original image
cv2.rectangle(image,(100,100),(300,300),(0,0,255),3)
#crop image
crop = image[100:300,100:300]
#draw rectangle into cropped image
cv2.rectangle(crop,(50,50),(150,150),(0,0,255),3)
cv2.imshow('Result', image)
cv2.waitKey()
cv2.destroyAllWindows()
crop=image[100:300100:300]
在原始图像上创建视图,而不是新对象。修改该视图将修改基础原始图像。有关更多详细信息,请参阅
您可以通过在裁剪时创建副本来解决此问题:
crop=image[100:300100:300].copy()
注意:
image[100:300100:300]
参数是y:y+h,x:x+w
不是x:x+w,y:y+h
crop=image[100:300100:300]
在原始图像上创建视图而不是新对象。修改该视图将修改基础原始图像。有关更多详细信息,请参阅
您可以通过在裁剪时创建副本来解决此问题:
crop=image[100:300100:300].copy()
注意:
image[100:300100:300]
参数是y:y+h,x:x+w
不是x:x+w,y:y+h
您可以使用
roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
为了获得这两点,您可以调用cv2.setMouseCallback(“image”,鼠标裁剪)
。
函数是这样的
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)
您可以从这里获得详细信息:您可以使用
roi = oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0][0]:refPoint[1][0]]
为了获得这两点,您可以调用cv2.setMouseCallback(“image”,鼠标裁剪)
。
函数是这样的
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.imwrite("Cropped.jpg", roi)
after cv2.imshow("Cropped", roi)
我希望这能有所帮助。如果要保存裁剪后的图像,只需添加以下代码:
cv2.imwrite("Cropped.jpg", roi)
after cv2.imshow("Cropped", roi)
我希望这有帮助