两种尺度的图像滑窗效果

2023-11-24 19:59

本文主要是介绍两种尺度的图像滑窗效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、简单示例

当给你一张随机大小的图片时,用固定的矩形框框住目标,有些目标可能很大,有些目标也可能很小,比如从下面的目标找出猫眼,如果采用固定大小的矩形框,会出现漏检的情况:
这里写图片描述
这里的固定框、固定大小图片代码为:

'''
Created on 2017年8月19日@author: XuTing
'''
# import the necessary packages
import helpers
import time
import cv2# load the image and define the window width and height
image = cv2.imread('../image/cat2.jpg')  
(winW, winH) = (200, 128)
i = 0# loop over the image pyramid
for resized in helpers.pyramid(image, scale=1.5,minSize=(winW, winH)):# loop over the sliding window for each layer of the pyramidfor (x, y, window) in helpers.sliding_window(resized, stepSize=32, windowSize=(winW, winH)):# if the window does not meet our desired window size, ignore itif 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 windowclone = resized.copy()cropImg_clone = resized.copy()cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)cropImg = cropImg_clone[y: (y + winH),x:(x + winW)]#H,Wcv2.imshow("Window", clone)cv2.imshow("cropImg", cropImg)cv2.waitKey(1)#write
#         WinName = "Layer {}".format(i + 1)
#         cv2.imwrite('./'+WinName+'.jpg',clone)
#         i += 1time.sleep(0.025)

helpers:

'''
Created on 2017年8月19日@author: XuTing
'''
# import the necessary packages
import imutils
from skimage.transform import pyramid_gaussian
import cv2def pyramid(image, scale=1.5, minSize=(30, 30)):# yield the original imageprint('(H:{},W:{})'.format(image.shape[0], image.shape[1]))
#     yield image# compute the new dimensions of the image and resize itw = int(image.shape[1] / scale)image = imutils.resize(image, width=w)print('resize=(H:{},W:{})'.format(image.shape[0], image.shape[1]))# if the resized image does not meet the supplied minimum# size, then stop constructing the pyramidif image.shape[0] < minSize[1] or image.shape[1] < minSize[0]:print("Out of size!")else:yield imagedef pyramid2(image, scale=1.5, minSize=(30, 30)):# yield the original imageyield image# keep looping over the pyramidwhile True:# compute the new dimensions of the image and resize itw = int(image.shape[1] / scale)image = imutils.resize(image, width=w)print('(H:{},W:{})'.format(image.shape[0], image.shape[1]))# if the resized image does not meet the supplied minimum# size, then stop constructing the pyramidif image.shape[0] < minSize[1] or image.shape[1] < minSize[0]:print("Out of size!")break# yield the next image in the pyramidyield image        
def sliding_window(image, stepSize, windowSize):# slide a window across the imagefor y in range(0, image.shape[0], stepSize):for x in range(0, image.shape[1], stepSize):# yield the current windowyield (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 loopif resized.shape[0] < 30 or resized.shape[1] < 30:break# show the resized imageWinName = "Layer {}".format(i + 1)cv2.imshow(WinName, resized)cv2.waitKey(0)resized = resized*255cv2.imwrite('./'+WinName+'.jpg',resized)

为此采用了两种策略:
1)基于多尺度图片的定位;
固定的滑动窗口大小,而图像的尺寸按照一定比例缩放,而不是压缩,类似于金字塔的形状。
2)基于多尺寸滑动窗口的定位;
固定的图片大小,而滑动窗口尺寸会按照一定比例缩小,当小于设定的最小尺寸时,程序结束。

2、基于多尺度图片的定位

参考多尺度图片滑动窗口输出 - Alex_XT的博客 - CSDN博客
http://blog.csdn.net/u011463646/article/details/77417049
其实现的效果为:
这里写图片描述
代码:

'''
Created on 2017年11月20日@author: XuTing
'''
# import the necessary packages
import helpers
import argparse
import time
import cv2
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
IMAGE_PATH=os.path.join(BASE_DIR,'image','cat.jpg')
print(IMAGE_PATH)
# load the image and define the window width and height
image = cv2.imread(IMAGE_PATH)  
(winW, winH) = (100, 64)
i = 0# loop over the image pyramid
for resized in helpers.pyramid2(image, scale=2):# loop over the sliding window for each layer of the pyramidfor (x, y, window) in helpers.sliding_window(resized, stepSize=32, windowSize=(winW, winH)):# if the window does not meet our desired window size, ignore itif 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 windowclone = resized.copy()cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)cv2.imshow("Window", clone)cv2.waitKey(100)#write
#         WinName = "Layer {}".format(i + 1)
#         cv2.imwrite('./'+WinName+'.jpg',clone)
#         i += 1
#         time.sleep(0.025)

3、基于多尺寸滑动窗口的定位

在固定的图片大小中,使用不同大小的滑动窗口来实现目标的定位与检验:
(H:768,W:1024)
resize=(H:511,W:682)
minSize=windowList[-1]= (25, 16)
(winW, winH)=(200,128)
(winW, winH)=(100,64)
(winW, winH)=(50,32)
(winW, winH)=(25,16)
这里写图片描述

代码下载:http://download.csdn.net/download/u011463646/10126421

'''
Created on 2017年11月20日@author: XuTing
'''
# import the necessary packages
import helpers
import time
import cv2# load the image and define the window width and height
image = cv2.imread('../image/cat2.jpg')  
windowList = [(200, 128),(100,64),(50,32),(25,16)]# 使用了元组
i = 0# loop over the image pyramid
for resized in helpers.pyramid(image, scale=1.5,minSize=windowList[-1]):print("minSize=windowList[-1]=",windowList[-1])# loop over the sliding window for each layer of the pyramidfor winSize in windowList:winW=winSize[0]winH=winSize[1]print("(winW, winH)=({},{})".format(winW,winH))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 itif 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 windowclone = resized.copy()cropImg_clone = resized.copy()cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)cropImg = cropImg_clone[y: (y + winH),x:(x + winW)]#H,Wcv2.imshow("Window", clone)cv2.imshow("cropImg", cropImg)cv2.waitKey(1)#write#WinName = "Layer {}".format(i + 1)#cv2.imwrite('./'+WinName+'.jpg',clone)#i += 1time.sleep(0.025)

这篇关于两种尺度的图像滑窗效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/422470

相关文章

k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)

《k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)》本文记录在K8s上运行的MySQL/MariaDB备份方案,通过工具容器执行mysqldump,结合定时任务实... 目录前言一、获取需要备份的数据库的信息二、备份步骤1.准备工作(X86)1.准备工作(arm)2.手

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

c/c++的opencv图像金字塔缩放实现

《c/c++的opencv图像金字塔缩放实现》本文主要介绍了c/c++的opencv图像金字塔缩放实现,通过对原始图像进行连续的下采样或上采样操作,生成一系列不同分辨率的图像,具有一定的参考价值,感兴... 目录图像金字塔简介图像下采样 (cv::pyrDown)图像上采样 (cv::pyrUp)C++ O

golang实现延迟队列(delay queue)的两种实现

《golang实现延迟队列(delayqueue)的两种实现》本文主要介绍了golang实现延迟队列(delayqueue)的两种实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录1 延迟队列:邮件提醒、订单自动取消2 实现2.1 simplChina编程e简单版:go自带的time

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

CentOS7增加Swap空间的两种方法

《CentOS7增加Swap空间的两种方法》当服务器物理内存不足时,增加Swap空间可以作为虚拟内存使用,帮助系统处理内存压力,本文给大家介绍了CentOS7增加Swap空间的两种方法:创建新的Swa... 目录在Centos 7上增加Swap空间的方法方法一:创建新的Swap文件(推荐)方法二:调整Sww