2015-03-27 7 views
7

मैं यह छवि लेकिन छवि के सभी भाग नहीं अस्पष्ट बना देगी छवि को धुंधला करने केजावा कलंक छवि

int radius = 11; 
    int size = radius * 2 + 1; 
    float weight = 1.0f/(size * size); 
    float[] data = new float[size * size]; 

    for (int i = 0; i < data.length; i++) { 
     data[i] = weight; 
    } 

    Kernel kernel = new Kernel(size, size, data); 
    ConvolveOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); 
    //tbi is BufferedImage 
    BufferedImage i = op.filter(tbi, null); 

कोशिश कर रहा हूँ।

enter image description here

मैं कहाँ याद आ रही है, ताकि यह पूरा छवि अस्पष्ट बना देगी। किसी भी रास्ते के बिना।

+0

यदि आप वास्तविक मूल्यों के साथ 'int आकार = त्रिज्या * 2 + 1;' को प्रतिस्थापित करते हैं और देखें कि क्या यह मान बदलना धुंधला बॉक्स बदलता है? – npinti

+2

वर्कअराउंड के लिए [इस आलेख] (http://www.informit.com/articles/article.aspx?p=1013851&seqNum=5) में 'एज पर कार्य करना' अनुभाग देखें। कर्नेल के आकार के संबंध में आपको अपनी छवि का आकार बढ़ाने की जरूरत है। – halex

उत्तर

2

है ऐसा इसलिए है क्योंकि आप इस पंक्ति में ConvolveOp.EDGE_NO_OP उपयोग कर रहे हैं:

ConvolveOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); 

API documentation का कहना है: स्रोत छवि के किनारे पर

पिक्सल के बिना गंतव्य में इसी पिक्सल कॉपी कर रहे हैं संशोधन।

EDGE_ZERO_FILL का प्रयास करें - जो आपको काला सीमाएं देगा।

आप धुंधला होने के बाद किनारों को काटने की कोशिश भी कर सकते हैं।

किनारों को क्यों नहीं कर सकता है इसका कारण यह है कि एल्गोरिदम कैसे काम करता है।

+0

'EDGE_ZERO_FILL' के साथ धुंधले भाग के चारों ओर एक काला सीमा होगी, जो 'EDGE_NO_OP' के समान ही रहेगी। – halex

+0

हां और मैं नहीं चाहता कि काला सीमा भी –

7

मानक जावा ConvolveOp में केवल दो विकल्प EDGE_ZERO_FILL और EDGE_NO_OP हैं। आप जो चाहते हैं वह जेए समकक्ष (ConvolveDescriptor) से विकल्प है, जो EDGE_REFLECT (या EDGE_WRAP है, यदि आप दोहराना पैटर्न चाहते हैं)।

यदि आप जेएई का उपयोग नहीं करना चाहते हैं, तो आप अपनी छवि को एक बड़ी छवि में कॉपी करके, किनारों को खींचने या लपेटकर, स्वयं को लागू कर सकते हैं, फिर से खोलें, फिर किनारों को काट लें (तकनीक के समान टिप्पणी अनुभाग में @ हेलक्स द्वारा पोस्ट किए गए the article के "एज पर कार्य" अनुभाग में वर्णित है, लेकिन उस लेख के अनुसार, आप किनारों को पारदर्शी छोड़ सकते हैं)।

सादगी के लिए, आप केवल my implementation called ConvolveWithEdgeOp का उपयोग कर सकते हैं जो उपर्युक्त (बीएसडी लाइसेंस) करता है।

कोड क्या आप मूल रूप से किया था के समान होगा:

// ...kernel setup as before... 
Kernel kernel = new Kernel(size, size, data); 
BufferedImageOp op = new ConvolveWithEdgeOp(kernel, ConvolveOp.EDGE_REFLECT, null); 

BufferedImage blurred = op.filter(original, null); 

फिल्टर किसी अन्य BufferedImageOp की तरह काम करना चाहिए, और किसी भी BufferedImage साथ काम करना चाहिए।

+0

हम केवल धुंधला हिस्सा प्राप्त कर सकते हैं और उपरोक्त कोड –

+0

@PrashantThorat से अन्य bufferedImage में तांबे प्राप्त कर सकते हैं मुझे खेद है, मैं आपके प्रश्न को समझ नहीं पा रहा हूं। मेरा कोड किसी भी 'BufferedImage' के साथ काम करना चाहिए। – haraldK

0

आप एक पिक्सेल को धुंधला नहीं कर सकते हैं। यह स्पष्ट लगता है, लेकिन जब आप इसके बारे में सोचते हैं, तो न्यूनतम क्या होता है? एक पिक्सेल धुंधला करने के लिए, आपको पड़ोसी पिक्सेल की आवश्यकता है।

समस्या यह है कि किनारों और कोनों पर, पिक्सेल के बहुत कम पड़ोसियों हैं - धुंध एल्गोरिथ का उपयोग करने के लिए बहुत कम पिक्सल हैं। इसमें छवि के बाहर "पिक्सल" नहीं है जिसके साथ धुंधला होना है, इसलिए यह केवल उन लोगों को छोड़ देगा।

समाधान या तो तस्वीर को किसी भी तरह विस्तारित करने के लिए है (क्या आपके पास एक बड़ी स्रोत छवि उपलब्ध है?), या जब आप पूरा कर लें तो गैर-धुंधले बिट्स को काट लें। दोनों अनिवार्य रूप से वही हैं।

+1

डाउनवोट के साथ क्या है? मैं बिल्कुल समझता हूं कि समस्या क्या है। – Tomas

0

मुझे यह एक बार मिला जब मैं दस्तावेज़ों के लिए धुंधली क्षमता की तलाश में था जब ConvolveOp क्लास ने इसे काट नहीं दिया था (कारणों में आप चल रहे हैं)। यह एक गॉसियन ब्लर करता है जो सबसे प्राकृतिक धुंध इम्हो है ... उम्मीद है कि यह आपकी मदद करेगा। मैं इस वेबपेज से यह लिया गया: Java Image Processing ...

/* 
** Copyright 2005 Huxtable.com. All rights reserved. 
*/ 

package com.jhlabs.image; 

import java.awt.image.*; 

/** 
* A filter which applies Gaussian blur to an image. This is a subclass of ConvolveFilter 
* which simply creates a kernel with a Gaussian distribution for blurring. 
* @author Jerry Huxtable 
*/ 
public class GaussianFilter extends ConvolveFilter { 

    static final long serialVersionUID = 5377089073023183684L; 

    protected float radius; 
    protected Kernel kernel; 

    /** 
    * Construct a Gaussian filter 
    */ 
    public GaussianFilter() { 
     this(2); 
    } 

    /** 
    * Construct a Gaussian filter 
    * @param radius blur radius in pixels 
    */ 
    public GaussianFilter(float radius) { 
     setRadius(radius); 
    } 

    /** 
    * Set the radius of the kernel, and hence the amount of blur. The bigger the radius, the longer this filter will take. 
    * @param radius the radius of the blur in pixels. 
    */ 
    public void setRadius(float radius) { 
     this.radius = radius; 
     kernel = makeKernel(radius); 
    } 

    /** 
    * Get the radius of the kernel. 
    * @return the radius 
    */ 
    public float getRadius() { 
     return radius; 
    } 

    public BufferedImage filter(BufferedImage src, BufferedImage dst) { 
     int width = src.getWidth(); 
     int height = src.getHeight(); 

     if (dst == null) 
      dst = createCompatibleDestImage(src, null); 

     int[] inPixels = new int[width*height]; 
     int[] outPixels = new int[width*height]; 
     src.getRGB(0, 0, width, height, inPixels, 0, width); 

     convolveAndTranspose(kernel, inPixels, outPixels, width, height, alpha, CLAMP_EDGES); 
     convolveAndTranspose(kernel, outPixels, inPixels, height, width, alpha, CLAMP_EDGES); 

     dst.setRGB(0, 0, width, height, inPixels, 0, width); 
     return dst; 
    } 

    public static void convolveAndTranspose(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) { 
     float[] matrix = kernel.getKernelData(null); 
     int cols = kernel.getWidth(); 
     int cols2 = cols/2; 

     for (int y = 0; y < height; y++) { 
      int index = y; 
      int ioffset = y*width; 
      for (int x = 0; x < width; x++) { 
       float r = 0, g = 0, b = 0, a = 0; 
       int moffset = cols2; 
       for (int col = -cols2; col <= cols2; col++) { 
        float f = matrix[moffset+col]; 

        if (f != 0) { 
         int ix = x+col; 
         if (ix < 0) { 
          if (edgeAction == CLAMP_EDGES) 
           ix = 0; 
          else if (edgeAction == WRAP_EDGES) 
           ix = (x+width) % width; 
         } else if (ix >= width) { 
          if (edgeAction == CLAMP_EDGES) 
           ix = width-1; 
          else if (edgeAction == WRAP_EDGES) 
           ix = (x+width) % width; 
         } 
         int rgb = inPixels[ioffset+ix]; 
         a += f * ((rgb >> 24) & 0xff); 
         r += f * ((rgb >> 16) & 0xff); 
         g += f * ((rgb >> 8) & 0xff); 
         b += f * (rgb & 0xff); 
        } 
       } 
       int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff; 
       int ir = PixelUtils.clamp((int)(r+0.5)); 
       int ig = PixelUtils.clamp((int)(g+0.5)); 
       int ib = PixelUtils.clamp((int)(b+0.5)); 
       outPixels[index] = (ia << 24) | (ir << 16) | (ig << 8) | ib; 
       index += height; 
      } 
     } 
    } 

    /** 
    * Make a Gaussian blur kernel. 
    */ 
    public static Kernel makeKernel(float radius) { 
     int r = (int)Math.ceil(radius); 
     int rows = r*2+1; 
     float[] matrix = new float[rows]; 
     float sigma = radius/3; 
     float sigma22 = 2*sigma*sigma; 
     float sigmaPi2 = 2*ImageMath.PI*sigma; 
     float sqrtSigmaPi2 = (float)Math.sqrt(sigmaPi2); 
     float radius2 = radius*radius; 
     float total = 0; 
     int index = 0; 
     for (int row = -r; row <= r; row++) { 
      float distance = row*row; 
      if (distance > radius2) 
       matrix[index] = 0; 
      else 
       matrix[index] = (float)Math.exp(-(distance)/sigma22)/sqrtSigmaPi2; 
      total += matrix[index]; 
      index++; 
     } 
     for (int i = 0; i < rows; i++) 
      matrix[i] /= total; 

     return new Kernel(rows, 1, matrix); 
    } 

    public String toString() { 
     return "Blur/Gaussian Blur..."; 
    } 
} 
0

आप अक्सर अपने आवेदन में चित्रों के साथ सौदा है, तो आप ImageJ एपीआई का उपयोग पर विचार करना चाहते हो सकता है: यह काफी छवि प्रसंस्करण कार्यों का एक बहुत पर कार्यक्षमता पैक, धुंधला सहित।

उनके गाऊसी धुंधला फिल्टर चित्र के किनारे करने के लिए सही अस्पष्ट बना देगी, निम्नलिखित धारणा

{...} यह मानता है कि बाहर के छवि पिक्सल के बराबर मान बनाकर निकटतम किनारे पिक्सेल {...}

भले ही आप ImageJ एपीआई का उपयोग करने के लिए अपने कोड को बदलने के लिए नहीं करना चाहते, तो आप अभी भी ऊपर धारणा अपनी समस्या का समाधान करने के लिए उपयोगी मिल सकता है। http://rsb.info.nih.gov/ij/developer/api/ij/plugin/filter/GaussianBlur.html

1

आप opencv पुस्तकालय का उपयोग कर सकते पूरा चित्र का धुंधलापन बनाने के लिए:

अधिक जानकारी के लिए, API दस्तावेज़ों में GaussianBlur फिल्टर की जाँच करें।

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