METHOD #1: No smooth, just scaling.

def pyramid(image, scale=1.5, minSize=(30, 30)):
# yield the original image
yield image # keep looping over the pyramid
while True:
# compute the new dimensions of the image and resize it
w = int(image.shape[1] / scale)
image = imutils.resize(image, width=w) # if the resized image does not meet the supplied minimum
# size, then stop constructing the pyramid
if image.shape[0] < minSize[1] or image.shape[1] < minSize[0]:
break # yield the next image in the pyramid
yield image

METHOD #2: Resizing + Gaussian smoothing.

# import the necessary packages
import helpers
from skimage.transform import pyramid_gaussian
import argparse
import cv2 # construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", '--image', required=True, help="Path to the image")
ap.add_argument("-s", "--scale", type=float, default=1.5, help="scale factor size")
args = vars(ap.parse_args()) # load the image
image = cv2.imread(args["image"]) # METHOD #1: No smooth, just scaling.
# loop over the image pyramid
for (i, resized) in enumerate(helpers.pyramid(image, scale=args["scale"])):
# show the resized image
cv2.imshow("Layer {}".format(i + 1), resized)
cv2.waitKey(0) # close all windows
cv2.destroyAllWindows() # METHOD #2: Resizing + Gaussian smoothing.
for (i, resized) in enumerate(pyramid_gaussian(image, downscale=2)):
# if the image is too small, break from the loop
if resized.shape[0] < 30 or resized.shape[1] < 30:
break # show the resized image
cv2.imshow("Layer {}".format(i + 1), resized)
cv2.waitKey(0) #Run cmd python pyramid.py --image image/cat.jpg --scale 1.5

参考

【1】Image Pyramids with python and OpenCV - PyImageSearch
http://www.pyimagesearch.com/2015/03/16/image-pyramids-with-python-and-opencv/
【2】jrosebr1/imutils: A series of convenience functions to make basic
image processing operations such as translation, rotation, resizing,
skeletonization, and displaying Matplotlib images easier with opencv and
Python.
https://github.com/jrosebr1/imutils
【3】Histogram of Oriented Gradients and Object Detection - PyImageSearch
http://www.pyimagesearch.com/2014/11/10/histogram-oriented-gradients-object-detection/
【4】Module: transform — skimage v0.14dev docs
http://scikit-image.org/docs/dev/api/skimage.transform.html#pyramid-gaussian

上边我们介绍了图片不压缩的情况下,重新resize到不同大小,这样做的目的是为这一节做准备,即利用滑动窗口圈住图片的文字信息内容等,例如车牌的获取。

# import the necessary packages
import helpers
import argparse
import time
import cv2 # load the image and define the window width and height
image = cv2.imread('./image/cat.jpg')
(winW, winH) = (200, 128) # loop over the image pyramid
for resized in helpers.pyramid(image, scale=1.5):
# loop over the sliding window for each layer of the pyramid
for (x, y, window) in helpers.sliding_window(resized, stepSize=32, windowSize=(winW, winH)):
# if the window does not meet our desired window size, ignore it
if window.shape[0] != winH or window.shape[1] != winW:
continue # THIS IS WHERE YOU WOULD PROCESS YOUR WINDOW, SUCH AS APPLYING A
# MACHINE LEARNING CLASSIFIER TO CLASSIFY THE CONTENTS OF THE
# WINDOW # since we do not have a classifier, we'll just draw the window
clone = resized.copy()
cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
cv2.imshow("Window", clone)
cv2.waitKey(1)
# time.sleep(0.025)

helpers:

'''
Created on 2017年8月19日 @author: XuTing
'''
# import the necessary packages
import imutils
from skimage.transform import pyramid_gaussian
import cv2 def pyramid(image, scale=1.5, minSize=(30, 30)):
# yield the original image
yield image # keep looping over the pyramid
while True:
# compute the new dimensions of the image and resize it
w = int(image.shape[1] / scale)
image = imutils.resize(image, width=w) # if the resized image does not meet the supplied minimum
# size, then stop constructing the pyramid
if image.shape[0] < minSize[1] or image.shape[1] < minSize[0]:
break # yield the next image in the pyramid
yield image def sliding_window(image, stepSize, windowSize):
# slide a window across the image
for y in range(0, image.shape[0], stepSize):
for x in range(0, image.shape[1], stepSize):
# yield the current window
yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]]) if __name__ == '__main__':
image = cv2.imread('./image/cat2.jpg')
# METHOD #2: Resizing + Gaussian smoothing.
for (i, resized) in enumerate(pyramid_gaussian(image, downscale=2)):
# if the image is too small, break from the loop
if resized.shape[0] < 30 or resized.shape[1] < 30:
break
# show the resized image
WinName = "Layer {}".format(i + 1)
cv2.imshow(WinName, resized)
cv2.waitKey(10)
resized = resized*255
cv2.imwrite('./'+WinName+'.jpg',resized)

效果







参考

【1】Sliding Windows for Object Detection with Python and OpenCV - PyImageSearch
http://www.pyimagesearch.com/2015/03/23/sliding-windows-for-object-detection-with-python-and-opencv/?replytocom=322532
【2】My imutils package: A series of OpenCV convenience functions - PyImageSearch
http://www.pyimagesearch.com/2015/02/02/just-open-sourced-personal-imutils-package-series-opencv-convenience-functions/
【3】《SVM物体分类和定位检测》 - Hans的成长记录 - CSDN博客
http://blog.csdn.net/renhanchi/article/category/7007663

 

python 图片滑动窗口的更多相关文章

  1. Python之滑动窗口

    需求 对于一个数组array = ["n","v","l","f",...,"y","c& ...

  2. 『Python』图像金字塔、滑动窗口和非极大值抑制实现

    图像金字塔 1.在从cv2.resize中,传入参数时先列后行的 2.使用了python中的生成器,调用时使用for i in pyramid即可 3.scaleFactor是缩放因子,需要保证缩放后 ...

  3. Python实现图片滑动式验证识别

    1 abstract 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类 ...

  4. 【剑指Offer】滑动窗口的最大值 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 暴力求解 单调递减队列 日期 题目地址:https://www ...

  5. leetcode 239. 滑动窗口最大值(python)

    1. 题目描述 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 示 ...

  6. 玩转Python图片处理 (OpenCV-Python )

    OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效——由一系列 C 函数和少量 C++ 类 ...

  7. TCP 滑动窗口和 拥塞窗口

    转http://coolshell.cn/articles/11609.html 滑动窗口 -- 表征发送端和接收端的接收能力 拥塞窗口-- 表征中间设备的传输能力 TCP滑动窗口 需要说明一下,如果 ...

  8. opencv 模板匹配与滑动窗口(单匹配) (多匹配)

    1单匹配: 测试图片:   code: #include <opencv\cv.h> #include <opencv\highgui.h> #include <open ...

  9. 面试之路(29)-TCP流量控制和拥塞控制-滑动窗口协议详解

    拥塞: 拥塞发生的主要原因在于网络能够提供的资源不足以满足用户的需求,这些资源包括缓存空间.链路带宽容量和中间节点的处理能力.由于互联网的设计机制导致其缺乏"接纳控制"能力,因此在 ...

随机推荐

  1. Test for Job (poj 3249 记忆化搜索)

    Language: Default Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9733   A ...

  2. 第十七周自由练习项目——acm 学生最高最低成绩

    /* *程序的版权和版本号声明部分: *Copyright(c)2014,烟台大学计算机学院学生 *All rights reserved. *文件名:acm 学生最高与最低成绩 *作者:刘中林 *完 ...

  3. Entity Framework Utility .ttinclude File

    https://msdn.microsoft.com/en-us/library/ff477603(v=vs.100).aspx This topic provides an overview of ...

  4. uva1084

    状压dp+凸包 并没有看出来凸包的性质 首先答案一定在凸包上,然后每个凸包的角加起来是一个圆,那么就相当于凸包周长加一个圆了.然后预处理,再状压dp计算即可. #include<bits/std ...

  5. ECharts-热力图实例

    1.引入echarts.js 2.页面js代码 //用ajax获取所需要的json数据 $.get("../../../mall/queryPageWtSrPost.do", { ...

  6. Balloons(DFS)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2248 题意:(1)求图中四连块(有公共边的方块 ...

  7. 服务器通信REST、gRPC,Swagger/OpenAPI

    服务间的通信方式是在采用微服务架构时需要做出一个最基本的决策.默认的选项是通过 HTTP 发送 JSON,也就是所谓的 REST API.我们也是从 REST 开始的,但最近我们决定改用 gRPC. ...

  8. [NOI2015,LuoguP2146]软件包管理器------树剖

    ***题目链接戳我*** 又是在树上瞎搞滴题目.... 我们如果以安装的软件为1,未安装的软件为0,那么软件改变的数量即树上权值总和的数量,涉及到区间修改,区间查询,考虑树剖 分析完毕,似乎没啥好说的 ...

  9. WPF TextBox 仅允许输入数字

    因为在 IValueConverter 实现中,当文本不能转换为目标类型时返回 DependencyProperty.UnsetValue ,Validation.GetHasError 返回 tru ...

  10. 自学Python五 爬虫基础练习之SmartQQ协议

    BAT站在中国互联网的顶端,引导着中国互联网的发展走向...既受到了多数程序员的关注,也在被我们所惦记着... 关于SmartQQ的协议来自HexBlog,根据他的博客我自己也一步一步的去分析,去尝试 ...