2016-03-30 14 views
4

PIL kullanarak bir görüntüdeki kenarları nasıl algılayacağımı anladım (görüntüler çoğunlukla siyah çizim işaretleri ile beyaz arka plan olacaktır). Görüntüyü kırpabilmek için bu kenarları kapsayan dikdörtgeni nasıl algılayabilirim?Python: Görüntüde Kenarların Doğrultusunu Tespit Etme ve Bir Kare İçine Kırpma?

Örneğin, böyle bir şey kırpmak istiyorum:

enter image description here

veya bu:

enter image description here

içine

enter image description here

içine: nasıl bir nesnenin etrafında oto merkezini bilmiyorum hariç

enter image description here

Ben PIL kırpma aşinayım.

Güncelleme:

from PIL import Image, ImageFilter 
image = Image.open("myImage.png") 
image = image.filter(ImageFilter.FIND_EDGES) 

nasıl bütün bu kenarları içeren rect alacağı: Ben aşağıdakileri yaparak kenarlarını saptamak için idare ettik

?

enter image description here

+0

Ooh ile örneğin yapabileceğini sözlerine oldukça zor olmalıdır. Bu 'anahtar özellik' nerede biliyor musunuz? Bu ilginç bir özellik olarak, – ForceBru

+0

Oh anahtar özelliği ile herhangi bir işareti (yukarıdaki örnekte tüm sayı 3'dür) kestirmeye çalışacağım, böylece tüm işaretler yeni kırpılmış görüntüye sığacak şekilde. – KingPolygon

cevap

2

Sen OpenCV

import cv2 

#Load the image in black and white (0 - b/w, 1 - color). 
img = cv2.imread('input.png', 0) 

#Get the height and width of the image. 
h, w = img.shape[:2] 

#Invert the image to be white on black for compatibility with findContours function. 
imgray = 255 - img 
#Binarize the image and call it thresh. 
ret, thresh = cv2.threshold(imgray, 127, 255, cv2.THRESH_BINARY) 

#Find all the contours in thresh. In your case the 3 and the additional strike 
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) 
#Calculate bounding rectangles for each contour. 
rects = [cv2.boundingRect(cnt) for cnt in contours] 

#Calculate the combined bounding rectangle points. 
top_x = min([x for (x, y, w, h) in rects]) 
top_y = min([y for (x, y, w, h) in rects]) 
bottom_x = max([x+w for (x, y, w, h) in rects]) 
bottom_y = max([y+h for (x, y, w, h) in rects]) 

#Draw the rectangle on the image 
out = cv2.rectangle(img, (top_x, top_y), (bottom_x, bottom_y), (0, 255, 0), 2) 
#Save it as out.jpg 
cv2.imwrite('out.jpg', img) 

Örnek çıktı enter image description here

+0

Henüz opencv yüklemedim (Sorun yaşıyorum) ama kodunuzu yorumlayabilseydiniz hoş olurdu :) Birazdan test etmeye çalışacağım. – KingPolygon

+0

openCV, genişlik ve yüksekliklerin dikdörtgeni veya sağ ve alt koordinatları, sınırlamaRect çağrısı ile döndürüyor mu? genişlik ve yükseklik ise ve koordinat değilse, yukarıdaki kod işe yaramaz - çok bozuk bir şekilde. Eğer koordinatlarsa, x, y, w, h yerine x1, y1, x2, y2 adlarını verebilirsiniz. – jsbueno

+0

@jsbueno 'boundingRect', sol üst köşe koordinatlarını döndürür ve genişlik ve yüksekliğe göre düzeltir. Neden çalışmıyor? – vitalii

İlgili konular