python 图片滑动窗口
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 图片滑动窗口的更多相关文章
- Python之滑动窗口
需求 对于一个数组array = ["n","v","l","f",...,"y","c& ...
- 『Python』图像金字塔、滑动窗口和非极大值抑制实现
图像金字塔 1.在从cv2.resize中,传入参数时先列后行的 2.使用了python中的生成器,调用时使用for i in pyramid即可 3.scaleFactor是缩放因子,需要保证缩放后 ...
- Python实现图片滑动式验证识别
1 abstract 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类 ...
- 【剑指Offer】滑动窗口的最大值 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 暴力求解 单调递减队列 日期 题目地址:https://www ...
- leetcode 239. 滑动窗口最大值(python)
1. 题目描述 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 示 ...
- 玩转Python图片处理 (OpenCV-Python )
OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效——由一系列 C 函数和少量 C++ 类 ...
- TCP 滑动窗口和 拥塞窗口
转http://coolshell.cn/articles/11609.html 滑动窗口 -- 表征发送端和接收端的接收能力 拥塞窗口-- 表征中间设备的传输能力 TCP滑动窗口 需要说明一下,如果 ...
- opencv 模板匹配与滑动窗口(单匹配) (多匹配)
1单匹配: 测试图片: code: #include <opencv\cv.h> #include <opencv\highgui.h> #include <open ...
- 面试之路(29)-TCP流量控制和拥塞控制-滑动窗口协议详解
拥塞: 拥塞发生的主要原因在于网络能够提供的资源不足以满足用户的需求,这些资源包括缓存空间.链路带宽容量和中间节点的处理能力.由于互联网的设计机制导致其缺乏"接纳控制"能力,因此在 ...
随机推荐
- crm使用soap启用和停用记录
function demo() { //操作记录的id var targetId = "a8a46444-ba10-e411-8a04-00155d002f02"; ...
- Xcode HeaderDoc 教程(3)
打开 MathAPI.h,将第一个 @param 标签的參数名由firstNumber 改动为 thirdNumber,然后编译. 有一个警告发生.甚至提出了改动建议.它不会影响不论什么事情,但有助于 ...
- luogu2161 [SHOI2009]会场预约
题目大意 随着时间的推移这里有几个任务对应着一段区间.每次要将任务安到时间线上时,要把时间线上已有的与该任务对应区间有交集的区间对应的任务删去.求每次删去的区间个数,以及整个时间线上有几个任务.时间线 ...
- 微信小程序初探(一、简单的数据请求)
微信小程序出来有一段时间了,之前没看好小程序(觉得小程序体验不咋好,内心对新事物有抵触心里,请原谅我的肤浅[捂脸][捂脸]),不过后来偶然之间玩过小程序的游戏(跳一跳.球球大作战.猜画小歌 等),顿悟 ...
- python lmdb demo 这接口和BDB一样恶心啊!
import lmdb lmdb_img_name = "test.lmdb" env = lmdb.open(lmdb_img_name, map_size=1e6) with ...
- 洛谷 P3959 NOIP2017 宝藏 —— 状压搜索
题目:https://www.luogu.org/problemnew/show/P3959 搜索: 不是记忆化,而是剪枝: 邻接矩阵存边即可,因为显然没有那么多边. 代码如下: #include&l ...
- 杂项:ESB接口
ylbtech-杂项:ESB接口 ESB全称为Enterprise Service Bus,即企业服务总线.它是传统中间件技术与XML.Web服务等技术结合的产物.ESB提供了网络中最基本的连接中枢, ...
- codeforces round #424 div2
A 暴力查询,分三段查就可以了 #include<bits/stdc++.h> using namespace std; ; int n, pos; int a[N]; int main( ...
- Oracle占用内存过高解决办法
1.cmd sqlplus system账户登录 2.show parameter sga; --显示内存分配情况 3.alter system set sga_max_size=200m scope ...
- 3d数学 7 矩阵
7.1 矩阵-数学定义 在线性代数中, 矩阵就是以行和列形式组织的矩形数字块.矩阵是向量的数组. 7.1.1 矩阵的维度和记法 矩阵的维度被定义为它包含了多少行和多少列.一个\(r \times c\ ...