Original image. Click to enlarge.
Image with Smart Blur applied. Notice that outlines are
preserved, even where the oranges overlap.One of my favorite Photoshop effects is Smart Blur, which provides a seemingly effortless way to smooth out JPEG artifacts, remove blemishes from skin in photographs of people, etc. Its utility lies in the fact that despite the considerable blurriness it imparts to many regions of an image, it preserves outlines and fine details (the more important parts of an image, usually). Thus it gives the effect of magically blurring only those parts of the image that you
want to be blurred.
The key to how Smart Blur works is that it preferentially blurs parts of an image that are sparse in detail (rich in low-frequency information) while leaving untouched the parts of the image that are comparatively rich in detail (rich in high-frequency information). Abrupt transitions in tone are ignored; areas of subtle change are smoothed (and thus made even more subtle).
The algorithm is quite straightforward:
1. March through the image pixel by pixel.
2. For each pixel, analyze an adjacent region (say, the adjoining 5 pixel by 5 pixel square).
3. Calculate some metric of pixel variance for that region.
4. Compare the variance to some predetermined threshold value.
5. If the variance exceeds the threshold, do nothing.
6. If the variance is less than the threshold, apply blurring to the source pixel. But vary the amount of blurring according to the variance: low variance, more blurring (high variance, less blurring).
In the implementation presented below, I start by cloning the current image and massively blurring the entire (cloned) image. Then I march through the pixels of the original image and begin doing the region-by-region analysis. When I need to apply blurring, I derive the new pixel by linear interpolation between original and cloned-image pixels.
So the first thing we need is a routine for linear interpolation between two values; and a corresponding routine for linear interpolation between two
pixel values.
Linear interpolation is easy:
public double lerp( double a, double b, double amt) {
return a + amt * ( b - a );
}
Linear interpolation between pixels is tedious-looking but straightforward:
int lerpPixel( int oldpixel, int newpixel, double amt ) {
int oldRed = ( oldpixel >> 16 ) & 255;
int newRed = ( newpixel >> 16 ) & 255;
int red = (int) lerp( (double)oldRed, (double)newRed, amt ) & 255;
int oldGreen = ( oldpixel >> 8 ) & 255;
int newGreen = ( newpixel >> 8 ) & 255;
int green = (int) lerp( (double)oldGreen, (double)newGreen, amt ) & 255;
int oldBlue = oldpixel & 255;
int newBlue = newpixel & 255;
int blue = (int) lerp( (double)oldBlue, (double)newBlue, amt ) & 255;
return ( red << 16 ) | ( green << 8 ) | blue;
}
Another essential routine that we need is a routine for analyzing the pixel variance in a region. For this, I use a root-mean-square error:
public double rmsError( int [] pixels ) {
double ave = 0;
for ( int i = 0; i < pixels.length; i++ )
ave += ( pixels[ i ] >> 8 ) & 255;
ave /= pixels.length;
double diff = 0;
double accumulator = 0;
for ( int i = 0; i < pixels.length; i++ ) {
diff = ( ( pixels[ i ] >> 8 ) & 255 ) - ave;
diff *= diff;
accumulator += diff;
}
double rms = accumulator / pixels.length;
rms = Math.sqrt( rms );
return rms;
}
Before we transform the image, we should have code that opens an image and displays it in a JFrame. The following code does that. It takes the image whose path is supplied in a command-line argument, opens it, and displays it in a JComponent inside a JFrame:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class ImageWindow {
// This inner class is our canvas.
// We draw the image on it.
class ImagePanel extends JComponent {
BufferedImage theImage = null;
ImagePanel( BufferedImage image ) {
super();
theImage = image;
}
public BufferedImage getImage( ) {
return theImage;
}
public void setImage( BufferedImage image ) {
theImage = image;
this.updatePanel();
}
public void updatePanel() {
invalidate();
getParent().doLayout();
repaint();
}
public void paintComponent( Graphics g ) {
int w = theImage.getWidth( );
int h = theImage.getHeight( );
g.drawImage( theImage, 0,0, w,h, this );
}
} // end ImagePanel inner class
// Constructor
public ImageWindow( String [] args ) {
// open image
BufferedImage image = openImageFile( args[0] );
// create a panel for it
ImagePanel theImagePanel = new ImagePanel( image );
// display the panel in a JFrame
createWindowForPanel( theImagePanel, args[0] );
// filter the image
filterImage( theImagePanel );
}
public void filterImage( ImagePanel panel ) {
SmartBlurFilter filter = new SmartBlurFilter( );
BufferedImage newImage = filter.filter( panel.getImage( ) );
panel.setImage( newImage );
}
public void createWindowForPanel( ImagePanel theImagePanel, String name ) {
BufferedImage image = theImagePanel.getImage();
JFrame mainFrame = new JFrame();
mainFrame.setTitle( name );
mainFrame.setBounds(50,80,image.getWidth( )+10, image.getHeight( )+10);
mainFrame.setDefaultCloseOperation(3);
mainFrame.getContentPane().add( theImagePanel );
mainFrame.setVisible(true);
}
BufferedImage openImageFile( String fname ) {
BufferedImage img = null;
try {
File f = new File( fname );
if ( f.exists( ) )
img = ImageIO.read(f);
}
catch (Exception e) {
e.printStackTrace();
}
return img;
}
public static void main( String[] args ) {
new ImageWindow( args );
}
}
Note the method
filterImage(), where we instantiate a
SmartBlurFilter. Without further ado, here's the full code for
SmartBlurFilter:
import java.awt.image.Kernel;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.Graphics;
public class SmartBlurFilter {
double SENSITIVITY = 10;
int REGION_SIZE = 5;
float [] kernelArray = {
1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1
};
Kernel kernel = new Kernel( 9,9, normalizeKernel( kernelArray ) );
float [] normalizeKernel( float [] ar ) {
int n = 0;
for (int i = 0; i < ar.length; i++)
n += ar[i];
for (int i = 0; i < ar.length; i++)
ar[i] /= n;
return ar;
}
public double lerp( double a,double b, double amt) {
return a + amt * ( b - a );
}
public double getLerpAmount( double a, double cutoff ) {
if ( a > cutoff )
return 1.0;
return a / cutoff;
}
public double rmsError( int [] pixels ) {
double ave = 0;
for ( int i = 0; i < pixels.length; i++ )
ave += ( pixels[ i ] >> 8 ) & 255;
ave /= pixels.length;
double diff = 0;
double accumulator = 0;
for ( int i = 0; i < pixels.length; i++ ) {
diff = ( ( pixels[ i ] >> 8 ) & 255 ) - ave;
diff *= diff;
accumulator += diff;
}
double rms = accumulator / pixels.length;
rms = Math.sqrt( rms );
return rms;
}
int [] getSample( BufferedImage image, int x, int y, int size ) {
int [] pixels = {};
try {
BufferedImage subimage = image.getSubimage( x,y, size, size );
pixels = subimage.getRGB( 0,0,size,size,null,0,size );
}
catch( Exception e ) {
// will arrive here if we requested
// pixels outside the image bounds
}
return pixels;
}
int lerpPixel( int oldpixel, int newpixel, double amt ) {
int oldRed = ( oldpixel >> 16 ) & 255;
int newRed = ( newpixel >> 16 ) & 255;
int red = (int) lerp( (double)oldRed, (double)newRed, amt ) & 255;
int oldGreen = ( oldpixel >> 8 ) & 255;
int newGreen = ( newpixel >> 8 ) & 255;
int green = (int) lerp( (double)oldGreen, (double)newGreen, amt ) & 255;
int oldBlue = oldpixel & 255;
int newBlue = newpixel & 255;
int blue = (int) lerp( (double)oldBlue, (double)newBlue, amt ) & 255;
return ( red << 16 ) | ( green << 8 ) | blue;
}
int [] blurImage( BufferedImage image,
int [] orig, int [] blur, double sensitivity ) {
int newPixel = 0;
double amt = 0;
int size = REGION_SIZE;
for ( int i = 0; i < orig.length; i++ ) {
int w = image.getWidth();
int [] pix = getSample( image, i % w, i / w, size );
if ( pix.length == 0 )
continue;
amt = getLerpAmount ( rmsError( pix ), sensitivity );
newPixel = lerpPixel( blur[ i ], orig[ i ], amt );
orig[ i ] = newPixel;
}
return orig;
}
public BufferedImage filter( BufferedImage image ) {
ConvolveOp convolver = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
null);
// clone image into target
BufferedImage target = new BufferedImage(image.getWidth(), image
.getHeight(), image.getType());
Graphics g = target.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
int w = target.getWidth();
int h = target.getHeight();
// get source pixels
int [] pixels = image.getRGB(0, 0, w, h, null, 0, w);
// blur the cloned image
target = convolver.filter(target, image);
// get the blurred pixels
int [] blurryPixels = target.getRGB(0, 0, w, h, null, 0, w);
// go thru the image and interpolate values
pixels = blurImage(image, pixels, blurryPixels, SENSITIVITY);
// replace original pixels with new ones
image.setRGB(0, 0, w, h, pixels, 0, w);
return image;
}
}
Despite all the intensive image analysis, the routine is fairly fast: On my machine, it takes about one second to process a 640x480 image. That's slower than Photoshop by a factor of five, or more, but still not bad (given that it's "only Java").
Ideas for further development:
- Substitute a directional blur for the non-directional blur.
- Substitute a Sobel kernel for the blur kernel.
- Try other sorts of kernels as well.
I built a similar effect with ActionScript.
ReplyDeleteInstead, to improve performance, I found the edges of the image based on a threshold, selected the inverse. Blurred the selection using the BlurFilter and reapplied it to the image. I then added some noise to hide the "waxy" look smart blur gives off. It's not as accurate as the smart blur, but it performs really well.
You should consider combining the two methods. Use a basic blur for relatively smooth areas, and a smart blur for pixels that reside along the the edges. You will find that by reducing the amount of pixels to smart blur you will ultimately increase performance considerably!
Very much useful article. Kindly keep blogging
DeleteJava Training in Chennai
Java Online Training India
I just have this energy level that I've never had before. The only thing that's changed is I've started wearing my Power Balance bracelet and nike running shoes
ReplyDeletePower Balance wholesale
Silly Bands
Reebok Easytone
The best way to implement smart blur filter is to just reuse the class as given: Processing offers bridges between PImage and BufferedImage.Since the class uses some Java image processing facilities, a direct port to pure Processing (using pixels[] and such) is a bit more work.
ReplyDeletedraw barcode in java
Our Pay for custom written college papers services makes your college life bearable and also receive the best assignment writing service fulfilling by enabling you to pass your exams and also in other research and essay writing tasks.
ReplyDeleteThank you so much dear for this amazing information. Everyone needs best for their business to compete in this fast-growing world. OGEN Infosystem provides best services for website designing in Delhi.
ReplyDeleteWebsite Designing Company in Delhi
This is really a nice and informative, containing all information and also has a great impact on discussion-board on https://essaysworld.net/write-my-discussion-board-post
ReplyDeleteHi there! If you happen to look writing help with the high quality then I recommend you to use esay writing service because there you can find argumentative essay topics and many othe things.
ReplyDeletewebroot geek squad experts do deliver reliable home-based services for any pre-installed devices and gadgets Initiate a discussion session with the Trend micro geek squad team to quickly resolve your accessory related queries Professional tech executives on board to help in resolving issues related to account management, payment and other types of information up gradation Get the privilege to check with the current status of the troubleshooting process that is handled by the Geek Support experts. Geek squad appointment Highly convenient repair returns guaranteed with the tech support Looking for online multi-device support?
ReplyDeletegeek squad tech support is one of the leading company for each office and enterprise shoppers. geek squad chat offer you services from the expertise and experienced professionals. Our specialists promise you to deliver fast, economical and efficient facilities for all of your digital and technology-based issues. geek squad chat with agent See sample of current Geek Squad Protection Terms and Conditions for specific coverage and exclusions. Power surges can happen at any time, causing damage to your equipment. geek squad chat Most manufacturers’ warranties don’t cover power surges, but with Geek Squad Protection, you don’t have to worry. geek squad tech support
ReplyDeleteGeek Squad Tech Support provides the best technical help and guidance. Get in touch with extensively trained tech support team for all sort of queries and issues regarding your devices. You can contact Geek Squad round the clock according to your convenience.
ReplyDeleteGet highly accurate, technology fixes for your electronic gadgets at Geek Squad Tech Support. Reach our experts at Geek Squad Tech Support to remove all complexities with your appliances.
ReplyDelete
ReplyDeleteFix technical breakdown of all your electronics and appliances at Geek Squad Support. Reach the certified experts at Geek Squad Support for fixing any kind of technical bug with your devices. Best of services and assistance assured at support.
Geek Squad Tech Support is a team of experts that provides tech support services for various devices at home or office. For any tech help contact our experts at Geek squad tech support number.
ReplyDelete:)
ReplyDeleteglobalemployees
ReplyDeletehire Laravel developers
Web Designers for hire
Best Buy Appointment |
ReplyDeleteGeek Squad Appointment Scheduling |
Best Buy Geek Squad Appointment Schedule |
BestBuy.com Appointments |
Geek Squad Appointments At Best Buy |
Make An Appointment With The Geek Squad |
Schedule Geek Squad Appointment |le
Geek Squad Chat is a well-known tech service provider company in the world. Our experts offer support for all types of technical problems faced in computers, laptops and smartphones. They even provide solutions for home appliances, TV, smart wearable and home theatre. We help our customers in setup, installation, repair, uninstallation, replacement, damage and other issues. Our dedicated support team is always available at your service and assures you for the quick resolutions. Book geek squad chat today and get your device fixed without wasting much time.
ReplyDeletegeek squad chat |
geek squad chat with an agent |
geek squad chat |
geek squad chat with an agent |
geek squad chat |
I am virtually impressed about the data you provide for your article. I have to say am enormously crushed by your whole story. It’s not easy to get such exceptional statistics these days. I sit up for staying right here for a long term.
ReplyDeleteoffice.com/setup
mcafee.com/activate
ReplyDeleteشركة نقل اثاث بجدة
شركة نقل عفش بالرياض
شركة نقل عفش بالمدينة المنورة
شركة نقل عفش بالدمام
شركة نقل عفش بالدمام
شركة نقل اثاث بجدة
شركة نقل عفش بالرياض
ReplyDeleteشركات نقل وتنظيف
شركات نقل عفش بالطائف
شركات نقل عفش بالمدينة المنورة
شركات نقل عفش بمكة
شركات نقل عفش
شركة تنظيف خزانات بالمدينة المنورة
شركات تنظيف بمكة
https://community.ebay.com/t5/Shipping-Returns/%D8%B4%D8%B1%D9%83%D8%A7%D8%AA-%D9%86%D9%82%D9%84-%D8%B9%D9%81%D8%B4-%D9%88%D8%A7%D8%AB%D8%A7%D8%AB-%D8%A8%D8%A7%D9%84%D9%85%D9%85%D9%84%D9%83%D8%A9-%D8%A7%D9%84%D9%85%D8%AA%D8%AD%D8%AF%D8%A9/qaq-p/26768794#M143546
ReplyDeletehttp://jumperads.unblog.fr/2017/03/22/%d8%a7%d9%81%d8%b6%d9%84-%d8%b4%d8%b1%d9%83%d8%a9-%d9%86%d9%82%d9%84-%d8%b9%d9%81%d8%b4-%d8%a7%d9%84%d9%82%d8%b5%d9%8a%d9%85/
http://jumperads.unblog.fr/2017/03/20/%d8%a7%d9%87%d9%85-%d8%b4%d8%b1%d9%83%d8%a7%d8%aa-%d9%86%d9%82%d9%84-%d8%a7%d9%84%d8%b9%d9%81%d8%b4-%d8%a8%d8%ae%d9%85%d9%8a%d8%b3-%d9%85%d8%b4%d9%8a%d8%b7/
http://jumperads.unblog.fr/2017/03/20/%d8%b4%d8%b1%d9%83%d8%a9-%d8%a7%d9%84%d9%85%d8%aa%d8%ad%d8%af%d8%a9-%d9%85%d9%86-%d8%a7%d9%87%d9%85-%d8%b4%d8%b1%d9%83%d8%a7%d8%aa-%d9%86%d9%82%d9%84-%d8%a7%d9%84%d8%b9%d9%81%d8%b4-%d8%a8%d9%85%d9%83/
http://jumperads.unblog.fr/2017/03/20/%d8%b4%d8%b1%d9%83%d8%a7%d8%aa-%d9%86%d9%82%d9%84-%d8%a7%d9%84%d8%a7%d8%ab%d8%a7%d8%ab-%d8%a8%d8%a7%d9%84%d8%b1%d9%8a%d8%a7%d8%b6-%d9%85%d9%88%d9%82%d8%b9-%d8%a7%d9%84%d9%85%d8%aa%d8%ad%d8%af%d8%a9/
http://jumperads.unblog.fr/2017/03/23/%d8%b4%d8%b1%d9%83%d8%a7%d8%aa-%d9%86%d9%82%d9%84-%d8%a7%d9%84%d8%b9%d9%81%d8%b4-%d8%a8%d8%ad%d8%a7%d8%a6%d9%84-2017/
http://jumperads.unblog.fr/2017/03/23/%d8%b4%d8%b1%d9%83%d8%a9-%d8%aa%d9%86%d8%b8%d9%8a%d9%81-%d9%85%d8%aa%d8%ae%d8%b5%d8%b5%d8%a9-%d9%85%d8%af%d9%8a%d9%86%d8%a9-%d8%a7%d9%84%d9%82%d8%b7%d9%8a%d9%81/
http://jumperads.unblog.fr/2017/03/23/%d9%86%d9%82%d9%84-%d8%a7%d9%84%d8%b9%d9%81%d8%b4-%d8%a8%d9%8a%d9%86%d8%a8%d8%b9/
http://jumperads.unblog.fr/
Geek Squad Chat is a well-known tech service provider company in the world. Our experts offer support for all types of technical problems faced in computers, laptops and smartphones. They even provide solutions for home appliances, TV, smart wearable and home theatre. We help our customers in setup, installation, repair, uninstallation, replacement, damage and other issues. Our dedicated support team is always available at your service and assures you for the quick resolutions. Book geek squad chat today and get your device fixed without wasting much time.
ReplyDeletegeek squad chat with agent |
geek squad chat with an agent |
geeksquad.com -chat-with-an-agent |
www.geeksquad.com chat with agent |
geek squad online chat with an agent |
geek squad tech support |
geek squad chat with an agent |
geeksquad.com -chat-with-an-agent |
www.geeksquad.com chat with agent |
geek squad online chat with an agent |
geek squad tech support |
Geek Squad Chat is a well-known tech service provider company in the world. Our experts offer support for all types of technical problems faced in computers, laptops and smartphones. They even provide solutions for home appliances, TV, smart wearable and home theatre. We help our customers in setup, installation, repair, uninstallation, replacement, damage and other issues. Our dedicated support team is always available at your service and assures you for the quick resolutions. Book geek squad chat today and get your device fixed without wasting much time.
ReplyDeletegeek squad chat |
geek squad chat with agent |
geek squad chat with an agent |
geeksquad.com -chat-with-an-agent |
www.geeksquad.com chat with agent |
geek squad online chat with an agent |
geek squad tech support |
Webocity Technologies is best website designing company in delhi, Best Website development company in Delhi, We Offer Best Digital Marketing services in Delhi.
ReplyDeleteInterpages
ReplyDeleteGuest Blogger
Guest Blogging Site
Guest Blogging Website
Guest Posting Site
We offer luxury, one in a million puppies of the highest quality and standards Puppies.
ReplyDeletePlease browse through our complete site ask any question you may have will most likely be answered.
We have a lot of information about French Bulldog puppies for sale, Care and information regarding our puppies for sale.vist
website for available puppies
weblink https://frenchieslove.com/
https://frenchieslove.com/product/buy-purebred-french-bulldog/
https://frenchieslove.com/product/fawn-french-bulldog-puppy/
https://frenchieslove.com/product/blue-french-bulldog-puppies/
https://frenchieslove.com/product/french-bulldog-for-sale-in-california/
https://frenchieslove.com/product/cheap-bulldog-puppies-for-sale/
https://frenchieslove.com/product/merle-french-bulldog-for-sale/
https://frenchieslove.com/product/rare-chocolate-french-bulldog/
https://frenchieslove.com/product/fawn-french-bulldog-puppy/
Best Grade Health Care Products Available Online. Genuine and 100% Guarantee on all orders you place. We Have the Best you
ReplyDeletewill come across On line. We As well Provide Tracking , on Packages as they are being sent and also Tracking on recent
orders .
We have variety of Pain killers for sale at affordable prices.
We are the best suppliers of pain medications and research chemicals .our prices are affordable .
For more details contact our website through any of the details provided below
Management,
Medication Kings
Website ... http://www.medicationkings.net
Emails .... info@medicationkings.net
medicationkings@gmail.net
weblink.... https://www.medicationkings.net
Phone .... +1 (503) 985-6723
Great Content & Thanks For Sharing With oflox. Do You Want To Know How To Earn Money From Helo App
ReplyDeletehttps://sites.google.com/view/dllrepair/ |
ReplyDeletehttps://sites.google.com/view/dllfixe/ |
https://sites.google.com/view/dllrepairtool/ |
https://sites.google.com/view/repairdll/ |
https://sites.google.com/view/repairdllfiles/ |
https://sites.google.com/view/windowsdllrepair/ |
https://sites.google.com/view/fixdll/ |
https://sites.google.com/view/dllfix/ |
https://sites.google.com/view/dllrepairtoolwindows10/ |
https://sites.google.com/view/dllfixerdownload/ |
https://sites.google.com/view/dllfixtool/ |
https://sites.google.com/view/windowsdllfixer/ |
https://sites.google.com/view/dllrepairtoolwindows7/ |
https://sites.google.com/view/windows7dllrepairtool/ |
https://sites.google.com/view/missingdllfileswindows10/ |
https://sites.google.com/view/downloaddllkit/ |
https://sites.google.com/view/dllfilerepairtool/ |
https://sites.google.com/view/dllfilerepair/ |
https://sites.google.com/view/downloaddllfixer/ |
https://sites.google.com/view/windows7dllrepair/ |
https://sites.google.com/view/downloadmissingdll/ |
https://sites.google.com/view/dllfilesfixer/ |
https://sites.google.com/view/windowsdllrepairtool/ |
https://sites.google.com/view/restoredllfiles/ |
https://sites.google.com/view/dllfilesrepair/ |
https://dll-repair.jimdosite.com/ |
https://dllrepair.weebly.com/ |
Wonderful Information. This post is written in a very ideal manner. Keep sharing such a post.
ReplyDeleteGet in touch with our Qwik Aid email technical support staff, who is available 24*7 to help you fix your problems as quickly as possible. If you need help Recover Hushmail Account Password or with other problems, call our toll-free numbers. Our professionals are always available to answer your email, whether day or night.If you contact our Qwik Aid support team, we guarantee that our support team will be ready to assist you in any way possible.
Appreciating Post. Thanks for sharing with us. Keep sharing again.
ReplyDeleteBest Loan company in India
Loan Company in India
Loan Finance Companies
Loan Companies
Top Loan Company in India
Loan Company Near Me
Best Loan Company Near Me
Online Application for Loans
Free Loan Apply
Apply Online Loan
Informative Post. Such a one of useful information. Thanks for sharing with us.
ReplyDeleteWith skilled and trained electricians, Todd Peters Electric as a Electrician Corona Ca service provider has come to your doorstep with extreme electrical solutions. We provide quality Electrical Services with careful analysis and observation of client's demands and budgets. If you would like to hire an electrician, simply dial our toll-free number or visit our website.
Brilliant post, checkout this as well:- algebra 2 tutor
ReplyDeleteเว็บพนันออนไลน์ คาสิโนมือถือ BETFLIX แจก เครดิต ฟรี แทงบอลสด บาคาร่าออนไลน์ เกมสล้อต ยอดนิมยม เกมยิงปลา เว็บเดิมพันฝากถอนไว ที่สุด ฝาก100รับ200 ทันที.
ReplyDelete