2013-07-10 6 views
6

मैं पीआईएल पुस्तकालय का उपयोग कर बड़ी छवि से उप-छवि खोजना चाहता हूं। मैं उन निर्देशांकों को भी जानना चाहता हूं जहां यह पाया जाता है?पीआईएल पुस्तकालय का उपयोग कर सबमिज कैसे प्राप्त करें?

+0

क्या आप अधिक विशिष्ट हो सकते हैं? वैसे भी - अगर आप चेहरे का पता लगाना चाहते हैं और इसी तरह - पीआईएल के बारे में भूल जाओ (यह इस तरह के काम के लिए डिज़ाइन नहीं किया गया है) और ओपनसीवी की तलाश करें। –

+0

क्या आप थोड़ा और स्पष्ट हो सकते हैं? आपके द्वारा पहले से किए गए कोड के कुछ पंक्तियां दें, आपकी छवि का कौन सा डेटा प्रारूप इत्यादि है। – usethedeathstar

+0

मैंने अभी तक कोडिंग शुरू नहीं किया है। मैं बड़ी छवि से उपनिवेश चाहता हूँ। पूर्व के लिए हमारे पास किसी भी खिलाड़ी का स्क्रीन शॉट है। हमारे पास तलाश स्क्रीन है। अब मैं पीआईएल का उपयोग करके तलाशने का स्थान ढूंढना चाहता हूं। – Sagar

उत्तर

5
import cv2 
import numpy as np 
image = cv2.imread("Large.png") 
template = cv2.imread("small.png") 
result = cv2.matchTemplate(image,template,cv2.TM_CCOEFF_NORMED) 
print np.unravel_index(result.argmax(),result.shape) 

यह ठीक काम करता है और मेरे लिए कुशल तरीके से।

0

ऐसा लगता है कि आप वस्तु का पता लगाने निष्पादित करना चाहते हैं, शायद के माध्यम से मिलान टेम्पलेट। यह एक छोटी सी समस्या नहीं है जब तक कि आप एक सटीक पिक्सेल-बाय-पिक्सेल मैच की तलाश नहीं कर रहे हैं, और पीआईएल इस तरह की चीज करने के लिए नहीं है।

जनवरी सही है कि आपको OpenCV आज़माएं। यह अच्छी पायथन बाइंडिंग के साथ एक मजबूत कंप्यूटर दृष्टि पुस्तकालय है।

यहाँ जिनका मिलान इस क्षेत्र के आसपास एक आयत ड्रॉ पायथन में एक अच्छा कम उदाहरण है: https://github.com/jungilhan/Tutorial/blob/master/OpenCV/templateMatching.py

2

मैं इसे केवल पीआईएल का उपयोग करने में कामयाब रहा।

कुछ चेतावनियां:

  1. यह एक पिक्सेल सही खोज है। यह बस आरजीबी पिक्सल से मेल खाता है।
  2. सादगी के लिए मैं अल्फा/पारदर्शिता चैनल हटा देता हूं। मैं केवल आरजीबी पिक्सल की तलाश में हूं।
  3. यह कोड मेमोरी से बड़ी छवि को रखते हुए, संपूर्ण उपमहाद्वीप पिक्सेल सरणी को स्मृति में लोड करता है। मेरे सिस्टम पर पाइथन ने 1920x1200 स्क्रीनशॉट के माध्यम से खोज रहे एक छोटे 40x30 सबमिटेज के लिए ~ 26 एमआईबी मेमोरी पदचिह्न बनाए रखा।
  4. यह सरल उदाहरण बहुत ही कुशल नहीं है, लेकिन बढ़ती दक्षता जटिलता को जोड़ देगा। यहां मैं चीजों को सीधे आगे और समझने में आसान रख रहा हूं।
  5. यह उदाहरण विंडोज और ओएसएक्स पर काम करता है। लिनक्स पर परीक्षण नहीं किया गया। यह केवल प्राथमिक प्रदर्शन का एक स्क्रीनशॉट लेता है (बहु मॉनीटर सेटअप के लिए)।

कोड यह रहा:

import os 
from itertools import izip 

from PIL import Image, ImageGrab 


def iter_rows(pil_image): 
    """Yield tuple of pixels for each row in the image. 

    From: 
    http://stackoverflow.com/a/1625023/1198943 

    :param PIL.Image.Image pil_image: Image to read from. 

    :return: Yields rows. 
    :rtype: tuple 
    """ 
    iterator = izip(*(iter(pil_image.getdata()),) * pil_image.width) 
    for row in iterator: 
     yield row 


def find_subimage(large_image, subimg_path): 
    """Find subimg coords in large_image. Strip transparency for simplicity. 

    :param PIL.Image.Image large_image: Screen shot to search through. 
    :param str subimg_path: Path to subimage file. 

    :return: X and Y coordinates of top-left corner of subimage. 
    :rtype: tuple 
    """ 
    # Load subimage into memory. 
    with Image.open(subimg_path) as rgba, rgba.convert(mode='RGB') as subimg: 
     si_pixels = list(subimg.getdata()) 
     si_width = subimg.width 
     si_height = subimg.height 
    si_first_row = tuple(si_pixels[:si_width]) 
    si_first_row_set = set(si_first_row) # To speed up the search. 
    si_first_pixel = si_first_row[0] 

    # Look for first row in large_image, then crop and compare pixel arrays. 
    for y_pos, row in enumerate(iter_rows(large_image)): 
     if si_first_row_set - set(row): 
      continue # Some pixels not found. 
     for x_pos in range(large_image.width - si_width + 1): 
      if row[x_pos] != si_first_pixel: 
       continue # Pixel does not match. 
      if row[x_pos:x_pos + si_width] != si_first_row: 
       continue # First row does not match. 
      box = x_pos, y_pos, x_pos + si_width, y_pos + si_height 
      with large_image.crop(box) as cropped: 
       if list(cropped.getdata()) == si_pixels: 
        # We found our match! 
        return x_pos, y_pos 


def find(subimg_path): 
    """Take a screenshot and find the subimage within it. 

    :param str subimg_path: Path to subimage file. 
    """ 
    assert os.path.isfile(subimg_path) 

    # Take screenshot. 
    with ImageGrab.grab() as rgba, rgba.convert(mode='RGB') as screenshot: 
     print find_subimage(screenshot, subimg_path) 

गति:

$ python -m timeit -n1 -s "from tests.screenshot import find" "find('subimg.png')" 
(429, 361) 
(465, 388) 
(536, 426) 
1 loops, best of 3: 316 msec per loop 

उपरोक्त आदेश मैं तिरछे subimage युक्त के रूप में timeit चल रहा था खिड़की ले जाया चल रहा है।

संबंधित मुद्दे