python cv.resize_python – 在open cv中调整图像大小

我对opencv和图像处理有点新意.我正在尝试使用我在

pyimagesearch.com上找到的代码.我将图像调整为500像素的高度,以执行边缘检测和查找轮廓.

r = 500.0 / image.shape[1]

dim = (500,int(image.shape[0] * r))

image = cv2.resize(image,dim,interpolation = cv2.INTER_AREA)

ratio = image.shape[0] /500.0

再次,我将处理后的图像与比率相乘(以便在原始图像上进行更改)

warped = four_point_transform(orig,screenCnt.reshape(4,2)*ratio)

r = 500.0 / warped.shape[1]

dim = (500,int(warped.shape[0] * r))

warped = cv2.resize(warped,dim,interpolation = cv2.INTER_AREA)

cv2.imshow("Perspective Transform",warped)

在此之后,我得到的结果有点像thisImage.只有部分图像可见,我无法看到图像的其余部分.请帮我.谢谢!

最佳答案 在我看来,你正在混合宽度和高度.

对于常规图像,image.shape []将按顺序为您提供高度,宽度和通道.

所以它应该看起来像:

newHeight = 500.0

oldHeight = image.shape[0]

oldWidth = image.shape[1]

r = newHeight / oldHeight

newWidth = int(oldWidth * r)

dim = (newHeight, newWidth)

image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)

aspectRatio = image.shape[1] / image.shape[0]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import cv2 # 创建混合高斯模型 fgbg = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=50, detectShadows=False) # 打开视频文件 cap = cv2.VideoCapture('t1.mp4') # 获取视频帧率、宽度和高度 fps = int(cap.get(cv2.CAP_PROP_FPS)) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 创建前景视频对象 fg_out = cv2.VideoWriter('foreground_video.avi', cv2.VideoWriter_fourcc(*'XVID'), fps, (width, height)) # 循环遍历视频帧 while True: ret, frame = cap.read() if not ret: break # 高斯模型背景减除法 fgmask = fgbg.apply(frame) # 缩放比例 scale_percent = 50 # 计算缩放后的新尺寸 width = int(frame.shape[1] * scale_percent / 100) height = int(frame.shape[0] * scale_percent / 100) dim = (width, height) # 缩放图像 frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA) fgmask = cv2.resize(fgmask, dim, interpolation=cv2.INTER_AREA) # 形态学开运算去除噪 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) opening = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel) # 寻找轮廓并计算周长 contours, hierarchy = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: perimeter = cv2.arcLength(cnt, True) if perimeter > 500: # 画出矩形框 x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.imshow('frame', frame) cv2.imshow('fgmask', fgmask) if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放对象 cap.release() fg_out.release() cv2.destroyAllWindows()改这个程序,消除视频抖动的影响,不要用光流补偿
05-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值