简体   繁体   English

Python-OpenCV 查找矩形和裁剪图像

[英]Python-OpenCV Find rectangle and crop image

I'm trying to make a game bot, and in the process, need to find the minimap, and crop it out so I can figure out where the player is from it.我正在尝试制作一个游戏机器人,在此过程中,需要找到小地图,并将其裁剪出来,以便我可以找出玩家所在的位置。

I was able to take a screenshot of the image, crop out approximately where the minimap is.我能够截取图像的屏幕截图,裁剪出小地图的大致位置。 However, I can't get the exact minimap bounding box from it.但是,我无法从中获得确切的小地图边界框。

My original gray image:我的原始灰色图像:

Blurred and Threshed Image:模糊和脱粒的图像: 在此处输入图片说明

What I wished to do:我想做什么: 在此处输入图片说明

As I'm very new to OpenCV and image processing, I'm not sure what steps I need to take to accomplish this task.由于我对 OpenCV 和图像处理非常陌生,因此我不确定需要采取哪些步骤来完成此任务。

What steps should I take to find the bounding box/coordinates of the minimap?我应该采取什么步骤来找到小地图的边界框/坐标?

EDIT: It seens I was unclear when I meant "knew the exact bounding box".编辑:当我的意思是“知道确切的边界框”时,我发现我不清楚。 I need to exactly extract the minimap area from the images.我需要从图像中准确提取小地图区域。 I do not have any coordinate info except the top left of the minimap, and the size of the minimap changes depending on the size of the map, so I can't hardcode coordinates.除了小地图的左上角,我没有任何坐标信息,小地图的大小会根据地图的大小而变化,因此我无法对坐标进行硬编码。

If you know the exact coordinates of the area of interest and if that area is pinned in the same location all the time you can use any of the drawing functions with the known x,y coordinates;如果您知道感兴趣区域的确切坐标,并且该区域始终固定在同一位置,则可以使用任何具有已知 x,y 坐标的 绘图函数 h,w dimensions.高、宽尺寸。

void cv::rectangle  (   InputOutputArray    img,
Point   pt1,
Point   pt2,
const Scalar &  color,
int     thickness = 1,
int     lineType = LINE_8,
int     shift = 0 
)

For finding the dynamic w,h: Starting with the known top left coordinates parse the image pixel by pixel and compare the color of the read pixel to the known one, first on x axes ascending until you find a pixel that doesn't match.为了找到动态 w,h:从已知的左上角坐标开始,逐个像素地解析图像并将读取像素的颜色与已知像素进行比较,首先在 x 轴上升序,直到找到不匹配的像素。 Second on y axes descending, and so on you can find the margins of the map.其次在 y 轴降序上,依此类推,您可以找到地图的边距。

Example of value of a pixel at coordinates 100 100 (python):坐标 100 100 (python) 处的像素值示例:

img = cv2.imread('myImage.png')

px = img[100,100]
print px

For parsing read this article for fast, optimized "for" pixel loops with OpenCV and Python如需解析,请阅读本文以了解使用 OpenCV 和 Python进行快速、优化的“for”像素循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM