2012-04-21 18 views
5

मैं एक प्रोग्राम प्रोग्रामिंग कर रहा हूं और मैं एक सर्कल को पारदर्शी सीमाओं (sprites) agnist के साथ छवियों को जोड़ना चाहता हूं।पारदर्शी आस-पास (कोलिजन डिटेक्शन के लिए) के साथ छवि के सीमा पिक्सेल ढूंढना

यह जानना आसान है कि मंडल पिक्सेल के साथ कोल्सियन की जांच करके छवि को ओवरलैप कर रहा है जो पारदर्शी नहीं है।

मुझे समस्या है बाउंस बनाने के लिए सामान्य कोण को जानना।

मुझे लाइब्रेरी (जावा) या एल्गोरिदम की आवश्यकता होगी जो छवि को दी गई है, यह छवि की सीमा पर मौजूद पिक्सल के साथ एक सरणी लौटाएगी ताकि मैं सतह के दो बिंदुओं के बीच ढलान पा सकूं।

क्या कोई लाइब्रेरी/एल्गोरिदम/कोड स्निपेट है जिसे मैं सीख सकता हूं?

आपको बहुत धन्यवाद

लॉउ।

मूल छवि से एक मुखौटा है, जहां सभी पारदर्शी पिक्सेल 0 हैं और सभी अपारदर्शी पिक्सेल बनाएं रहे 1

फिर, एक सरल बढ़त का पता लगाने के लिए अपने मुखौटा पर घटा कर करते हैं:

उत्तर

8

यहाँ एक सरल तरीका है प्रत्येक पिक्सेल (x,y), जो 0 या 1 होगा, पिक्सेल (x+1,y+1) से और पूर्ण मूल्य ले जाएगा।

यह आपको छवि के किनारे पर पिक्सल के लिए 1 और 0 हर जगह देगा।

नोट: यह विधि अनिवार्य रूप से छवि को 2 डी फ़ंक्शन के रूप में समझने और इसके ढाल की गणना करने के बराबर है। किनारों तीव्रता तीव्रता सतह के हिस्सों (जो बड़े ढाल मूल्यों के अनुरूप हैं) हैं। gradient-based edge detection पर कुछ और जानकारी यहां दी गई है।

Image Mask

फिर नीचे छवि बदलाव और एक पिक्सेल पर और घटाना:

Original Test Image

पहले सभी अपारदर्शी पिक्सेल मुखौटा:


यहाँ एक उदाहरण छवि है यह खुद से

यह नीचे दी गई छवि बनाता है। अब मूल्य 1 के साथ मैट्रिक्स इंडेक्स को पढ़ें।

यह एज पिक्सल की आपकी सरणी है।

Edge Mask

नोट: अगर आपकी छवियों आंतरिक पारदर्शी पिक्सेल होते हैं, इस तकनीक का भी आंतरिक किनारों, जो या तो आप के लिए एक समस्या नहीं हो सकता मिल जाएगा ...

3

यह वही है I'v समय के साथ लागू किया है: (detectionStrength 10 सबसे अच्छा है)

public static List<Pixel> getEdges(Image image, int detectionStrength) { 

    boolean[][] opaque = new boolean[image.getWidth(null)][image 
      .getHeight(null)]; 
    LinkedList<Pixel> edges = new LinkedList<Pixel>(); 
    int rgb; 

    /* 
    * convert to BufferedImage to get individual pixel colors 
    */ 
    BufferedImage bufferedImage; 
    if (image instanceof BufferedImage) 
     bufferedImage = (BufferedImage) image; 
    else { 
     bufferedImage = new BufferedImage(image.getWidth(null), 
       image.getHeight(null), BufferedImage.TYPE_INT_ARGB); 
     bufferedImage.createGraphics().drawImage(image, 0, 0, null); 
    } 

    for (int i = 0; i < opaque.length; i++) { 
     for (int j = 0; j < opaque[i].length; j++) { 
      rgb = bufferedImage.getRGB(i, j); 
      opaque[i][j] = (rgb >> 24 & 0xFF) > detectionStrength; // transparency 
     } 
    } 

    /* 
    * If a pixel is opaque, but is surrounded, with at least one 
    * transparent pixel, it is considered an edge. 
    */ 
    for (int x = 0; x < opaque.length; x++) { 
     for (int y = 0; y < opaque[x].length; y++) { 
      if ((x == 0) || (x == opaque.length - 1) || (y == 0) 
        || (y == opaque[x].length - 1)) { // border pixel 
       if (opaque[x][y]) // if opaque, it is automatically an edge, 
            // no matter its surrounding... 
        edges.add(new Pixel(x, y, new Color(bufferedImage 
          .getRGB(x, y)))); 

      } else { // not a border pixel 
       if (opaque[x][y] 
         && (!opaque[x - 1][y - 1] || !opaque[x][y - 1] 
           || !opaque[x + 1][y - 1] 
           || !opaque[x - 1][y] || !opaque[x + 1][y] 
           || !opaque[x - 1][y + 1] 
           || !opaque[x][y + 1] || !opaque[x + 1][y + 1])) 
        edges.add(new Pixel(x, y, new Color(bufferedImage 
          .getRGB(x, y)))); 
      } 
     } 
    } 

    return edges; 
} 

और पिक्सेल वर्ग (Point का सिर्फ एक बहुत ही सरल विस्तार):

public class Pixel extends Point implements Cloneable { 

    private static final long serialVersionUID = -9053911985748552077L; 

    public Color color; 

    public Pixel(int x, int y, Color c) { 
     super(x, y); 
     color = c; 
    } 

    public Pixel(Pixel other) { 
     super(other.x, other.y); 
     color = other.color; 
    } 

    public Color getColor() { 
     return color; 
    } 

    public void setColor(Color newColor) { 
     color = newColor; 
    } 

    public int hashCode() { 
     final int prime = 31; 
     int result = super.hashCode(); 
     result = prime * result + ((color == null) ? 0 : color.hashCode()); 
     return result; 
    } 

    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (!super.equals(obj)) 
      return false; 
     if (!(obj instanceof Pixel)) 
      return false; 
     Pixel other = (Pixel) obj; 
     if (color == null) { 
      if (other.color != null) 
       return false; 
     } else if (!color.equals(other.color)) 
      return false; 
     return true; 
    } 

    public Object clone() { 
     return new Pixel(x, y, color); 
    } 

    public String toString() { 
     return "Pixel [color=" + color + ", x=" + x + ", y=" + y + "]"; 
    } 
} 

एल्गोरिदम के साथ बनाई गई छवि, होगी:

StackOverflow logo

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