[英]resizing image by mouse dragging using opencv python
I am new to python and computer vision programming. 我是python和计算机视觉编程的新手。 though I am trying to make an image resizing tool.
虽然我正在尝试制作图像大小调整工具。 for this purpose I am using python3.x and opencv2.
为此,我正在使用python3.x和opencv2。 so far now I am able to resize an image by following code, its just an example.
到目前为止,我现在可以通过以下代码来调整图像的大小,这只是一个示例。
my_img = cv2.imread('4.1.04.tiff')
resized_img = cv2.resize(my_img,None,fx=0.5, fy=0.5, interpolation =
cv2.INTER_CUBIC)
cv2.imshow('Image',resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Well, my problem is I dont know how to do resize an image by dragging mouse. 好吧,我的问题是我不知道如何通过拖动鼠标来调整图像大小。 please let me know if it is possible in opencv or not and can be done using other libraries in python.
请让我知道opencv中是否可行,并且可以使用python中的其他库来完成。 Thanks!
谢谢!
I partially done the code for resizing image but its not working perfectly, there is some main window size problem occurring. 我部分完成了用于调整图像大小的代码,但它无法正常工作,出现了一些主窗口大小问题。 If you re-run the code you will get it.
如果重新运行代码,您将得到它。 I can not see full window and tracker bar too
我也看不到全窗口和跟踪栏
import cv2
#import numpy as np
pro_img = None
a1 = 0.5 #for default size of image
def nothing(x):
pass
def BnC_control(alpha):
alpha = alpha/10
#print(type(alpha))
if alpha >= 0.1:
return alpha
else:
return 0.1
img = cv2.imread('watch.jpg')
#img = np.zeros((512,512,3),np.uint16)
cv2.namedWindow('Window1',cv2.WINDOW_AUTOSIZE)
cv2.createTrackbar('Alpha','Window1',1,10,nothing)
while(True):
#r = BnC_control(a)
#pro_img = cv2.resize(img,None,fx=0.3,fy=0.3)
a = cv2.getTrackbarPos('Alpha','Window1')
a1 = BnC_control(a)
#print(a1)
re_img = cv2.resize(img,None,fx=a1,fy=a1,interpolation = cv2.INTER_CUBIC)
cv2.imshow('Window1',re_img)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
Also when I change interpolation to INTER_AREA then it goes wrong..nothing happens and trackerbar gets disappear. 另外,当我将插值更改为INTER_AREA时,它会出错..什么也没有发生,并且Trackerbar消失了。
A simple code to show a trackbar used to resize the image. 显示用于调整图像大小的跟踪栏的简单代码。 For more detail, see this link .
有关更多详细信息,请参见此链接 。
import cv2
import numpy as np
# method to be called by trackbar event
def onChange(pos):
global img,dst,rows,cols
# get value from trackbar
value = cv2.getTrackbarPos("resize_trackbar", "Result")
# resize image
dst = cv2.resize(src=img, dsize=(cols+value,rows+value))
#Run Main
if __name__ == "__main__" :
img = cv2.imread("image.jpg", -1)
# get original image size
rows,cols = img.shape[:2]
# initialize dst image
dst = np.copy(img)
# create display window
cv2.namedWindow("Result", cv2.WINDOW_NORMAL)
# create trackbar
cv2.createTrackbar("resize_trackbar", "Result", 0, 50, onChange)
while True:
cv2.imshow("Result", dst)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.