python 区域找图是什么思路

Python029

python 区域找图是什么思路,第1张

可以参考如下代码:

import os    

from time import sleep    

from myopencv import Image    

import X    

import gdkutils    

class XAuto:    

 _screensize = None    

 def __init__(self, tmp_img='/tmp/tmp%d.png' % os.getpid(),    

   default_threshold=0.7, default_rect=None):    

   self.d = X.Display()    

   self.tmp_img = tmp_img    

   self.default_threshold = default_threshold    

   self.default_rect = default_rect    

 def find_and_click(self, *args, back=False, **kwargs):    

   pos = self.find(*args, **kwargs)    

   if pos:    

     if back:    

       self.click_and_back(pos)    

     else:    

       self.click(pos)    

   return pos    

 def find_and_moveto(self, *args, **kwargs):    

   pos = self.find(*args, **kwargs)    

   if pos:    

     self.moveto(pos)    

   return pos    

 def click(self, pos=None, button=X.LEFT_BUTTON):    

   d = self.d    

   if pos is not None:    

     d.motion(pos)    

   d.button(button)    

   d.flush()    

 def wait(self, seconds):    

   sleep(seconds)    

 def click_and_back(self, pos, button=X.LEFT_BUTTON):    

   d = self.d    

   old_pos = d.getpos()    

   d.motion(pos)    

   d.button(button)    

   d.motion(old_pos)    

   d.flush()    

 def moveto(self, pos):    

   d = self.d    

   d.motion(pos)    

   d.flush()    

 def key(self, keyname):    

   d = self.d    

   d.key(keyname)    

   d.flush()    

 def find(self, img, threshold=None, rect=None, repeat=1, interval=0.2):    

   if isinstance(img, str):    

     img = Image(img)    

   if rect is None:    

     rect = self.default_rect or (0, 0) + self.screensize    

   if threshold is None:    

     threshold = self.default_threshold    

   tmp_img = self.tmp_img    

   for _ in range(repeat):    

     gdkutils.screenshot(tmp_img, rect)    

     sc = Image(tmp_img)    

     (x, y), similarity = sc.match(img)    

     if similarity > threshold:    

       x += rect[0]    

       y += rect[1]    

       x += img.width // 2    

       y += img.height // 2    

       return x, y    

     sleep(interval)    

   return False    

 @property    

 def screensize(self):    

   return self._screensize or gdkutils.get_screen_size()    

 def __del__(self):    

   try:    

     os.unlink(self.tmp_img)    

   except OSError:    

     pass    

 def monitor_size(self, *args, **kwargs):    

   return gdkutils.get_monitor_size(*args, **kwargs)

from PIL import Imageimport os.pathimport globdef convertjpg(jpgfile,outdir,width=1280,height=720):img=Image.open(jpgfile) new_img=img.resize((width,height),Image.BILINEAR) new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))for jpgfile in glob.glob("D:/python/*.jpg"):convertjpg(jpgfile,"D:/newfile")

convertjpg调用时可以有四个参数,如convertjpg(jpgfile,"D:/newfile",800,600)

Image open了jpg用完后要不要close?

这个问题跟image registration很相似 ,其实就是假设图像A和图像B之间存在一个平移(以及旋转)关系 ,使得平移后A和B重合的部分差别最小。

具体到这个问题, 只需要将源图像A和目标图像B都设成输入图像, 然后计算图像差的时候 。算平移后A和B重合的部分就可以了, 这样可以算出来一个平移向量, 有了平移向量之后, 在输入图像上面任意一点开始 ,这个点和他自身加上平移向量得到的点所围成的矩形一定是重复单元。

至于要最小重复单元, 只需要找到最短的平移向量就可以了, 不过需要去掉平凡解(零向量)。

以前看过image registration的东西有个快速的方法可以做到这一点。

先将源图片和目标图片降采样几次, 得到图像金字塔 先对金字塔顶端图片上寻找最佳匹配。 然后将最佳匹配对应的平移旋转参数作为初始估计, 再在下一层级的图像上再次估计 直到最后在原图像上估计。