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. monitor weblogic server ,Very simple to use, weblogic监控、巡检、故障简单小工具

        1. 开发了一个简单的监视weblogic执行情况的小程序.各位朋友下载下来试试,不用登陆console就能够知道server的执行状况,包含了jvm.线程.jdbc.状态jms等:另一个更简 ...

  2. Python3.4 远程操控电脑(开关机)

    import poplib import sys import smtplib from email.mime.text import MIMEText import os from email.he ...

  3. 移动互联网App推广的十大难题

    常常有朋友来问."我做了一个App,请问怎么推广啊?"或者就是"我们公司开发了一个App.想短时间内获取巨大的量."还有的就是问"有没有什么好渠道三个 ...

  4. web认证方案

    web构建在http之上,而它又是无状态协议,如何控制用户访问服务器上的受限资源呢? 最原始你想法通过http基本认证,每次发请求时都向后台传递用户名密码信息,服务器每次收到请求后都先验证用户是否合法 ...

  5. 从map到hash

    https://zybuluo.com/ysner/note/1175387 前言 这两种技巧常用于记录和去重量少而分散的状态. 都体现了映射思想. \(map\) 我一般是数组开不下时拿这玩意判重. ...

  6. 分享的js代码,从w3c上拓下来的

    <!DOCTYPE html><html><head> <title></title> <script>window._bd_s ...

  7. HTML 打印 换页

    打印 HTML 无法强制换页其实是一件很令人困扰的事,要达到这个功能其实可以透过 CSS 的 Pagebreak 来处理. 强制分页有大概只有二种用的到: { page-break-after: al ...

  8. python 12:list(range(...)) (转化参数列表)

    numbers = list(range(1,11)) #把范围产生的数字串转化为列表储存 print(numbers) 运行结果应该是: [1,2,3,4,5,6,7,8,9,10]

  9. 【Oracle】 手工建库

    操作系统:OEL 5.6 数据库版本:Oracle11gR2  11.2.0.4.0 新建数据库名称:lgr 1 生成pfile和口令文件 1)生成pfile文件,在模板文件init.ora中提取 [ ...

  10. C语言笔记(一)

    笑话一枚:程序员 A:“哥们儿,最近手头紧,借点钱?”程序员 B:“成啊,要多少?”程序员 A:“一千行不?”程序员 B:“咱俩谁跟谁!给你凑个整,1024,拿去吧.” =============== ...