python显⽰图⽚并让⽤户框选_matplotlib选择图像的框区域,
然后放⼤
使⽤opencv从⽹络摄像头获取帧,然后进⾏绘图。
我在图的中⼼画⼀个矩形,然后得到选定的区域并显⽰在另⼀个绘图中(通过插值放⼤)import matplotlib.pyplot as plt
import matplotlib.animation as animation
import cv2
boxSize = 150
enlargeBy = 3
def getBoxCoordinates(cap, size):
width = (3)
height = (4)
x1 = int(width / 2) - int(size / 2)
y1 = int(height / 2) - int(size / 2)
x2 = int(width / 2) + int(size / 2)
y2 = int(height / 2) + int(size / 2)
return [(x1, y1), (x2, y2)]
def getBox(cap, boxSize, frame, enlargeBy):
matplotlib中subplot[(x1, y1), (x2, y2)] = getBoxCoordinates(cap, boxSize);
# Get pixels in box
box_img = frame[y1 + 1:y2, x1 + 1:x2] # +1 cuz it excludes initial pixel interval
size(box_img, None, fx=enlargeBy, fy=enlargeBy,
interpolation=cv2.INTER_LINEAR) # different interpolation methods
cap = cv2.VideoCapture(0);
ret, frame = ad()
figWidth = 20
figHeight = 8
fig = plt.figure(figsize=(figWidth, figHeight))
enlarged = getBox(cap, boxSize, frame, enlargeBy)
[(x1, y1), (x2, y2)] = getBoxCoordinates(cap, boxSize);
video_plot = plt.subplot2grid((figHeight, figWidth), (0, 0), colspan=4, rowspan=4)
video_plot.axis('off')
video_plot.set_title("Camera feed")
video = video_plot.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
box_plot = plt.subplot2grid((figHeight, figWidth), (0, 4), colspan=4, rowspan=4)
box_plot.axis('off')
box_plot.set_title("Box")
box = box_plot.imshow(cv2.cvtColor(enlarged, cv2.COLOR_BGR2RGB)) #frame just to start
def updatefig(i):
ret, frame = ad()
enlarged = getBox(cap, boxSize, frame, enlargeBy)
video.set_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
box.set_data(cv2.cvtColor(enlarged, cv2.COLOR_BGR2RGB))
return [video, box]
ani = animation.FuncAnimation(fig, updatefig, interval=20, frames=200, blit=True)
plt.tight_layout()
plt.show()
cv2.destroyAllWindows()
plt.show()
我遇到的奇怪问题是,我在框架上绘制的矩形没有正确显⽰:它只显⽰⼀个或⼏个边。
我注意到,当图形尺⼨发⽣变化时,这种情况会发⽣变化,例如上⾯的代码只显⽰底部和左侧,如果我更改:^{pr2}$
然后我看到了底部、右侧和顶部,但没有看到左侧。在
不知道到底是什么原因造成了这种情况,也不知道该怎么解决。在

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。