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

 

最新文章

  1. WPF&#39;s Style BasedOn
  2. Python资源
  3. 安装包制作工具 SetupFactory使用1 详解
  4. Linux学习笔记(6)Linux常用命令之帮助命令与用户管理命令
  5. 【Go语言】集合与文件操作
  6. tomcat 设置集群
  7. Dubbo架构设计详解(转自shiyanjun.cn)
  8. javascript第三弹——数组
  9. Educational Codeforces Round 7 C. Not Equal on a Segment 并查集
  10. POJ2104 k-th number 划分树
  11. Unity-layermask的问题
  12. java数据结构之链表的实现
  13. 获取一个gridcontrol的数据行数
  14. Crazyflie笔记五: CRTP 实时通信协议(一)(转)
  15. CSS布局(六) 对齐方式
  16. Java异常实战——OutOfMemoryError
  17. Hadoop生态组件Hive,Sqoop安装及Sqoop从HDFS/hive抽取数据到关系型数据库Mysql
  18. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习6
  19. Pool:小对象缓存or复用
  20. java把一个list分割成多个list存入map中(实例)

热门文章

  1. Android系统Recovery工作原理之使用update.zip升级过程分析(七)---Recovery服务的核心install_package函数【转】
  2. Webstorm配置运行React Native
  3. WinForm c# 备份 还原 数据库(Yc那些事儿 转)
  4. bzoj1116 [POI2008]CLO——并查集找环
  5. A simple problem(并查集判环)
  6. Appium + python - online-install-apk
  7. 一、SQL系列之~使用SQL语言导出数据及实现定时导出数据任务
  8. RabbltMQ
  9. 【LuoguP2210 USACO】 Haywire
  10. Asp.net MVC4 Step by Step (2)-参数数据的传递