Hexagonal mesh for image processing and hexagonal mesh for image processing

Source: Internet
Author: User
Tags id3

Hexagonal mesh for image processing and hexagonal mesh for image processing

I. Principle

Based on the input parameter blockSize, the image is segmented to determine the center of each block to all

The mean value of the sum of pixels is compared with each pixel in the block. The Geometric Distance between RGB values is the smallest center, overlap

Generate the update operation until the maximum number of cycles declared by the input parameter is reached, and then output the result image.

Ii. Program Implementation

Class MyCluster, stores the information of each pixel block center and the location of the computing center.

Class SuperPixelsFilter, filter implementation, complete the main features of hexagonal mesh division, in which distance calculation, base

Euclidean distance formula.

Iii. Effect

Source image:


Effect:


4. full source code

Package com. gloomyfish. image. cluster. effect; import java. awt. image. bufferedImage; import java. util. arrayList; import java. util. arrays; import java. util. list; import com. gloomyfish. filter. study. abstractBufferedImageOp; public class SuperPixelsFilter extends actbufferedimageop {private double [] distances; private int [] labels; private MyCluster [] clusters; private int maxClusteringLoops = 50; private dou Ble blockSize; private double modifier; public SuperPixelsFilter () {blockSize = 16; modifier = 130;} public double getBlockSize () {return blockSize;} public void setBlockSize (double blockSize) {this. blockSize = blockSize;} public double getModifier () {return modifier;} public void setModifier (double modifier) {this. modifier = modifier;} @ Overridepublic BufferedImage filter (BufferedImage src, BufferedImage Dest) {int width = src. getWidth (); int height = src. getHeight (); if (dest = null) dest = createCompatibleDestImage (src, null); int [] inPixels = new int [width * height]; getRGB (src, 0, 0, width, height, inPixels); int index = 0; // initialization distances = new double [width * height]; labels = new int [width * height]; Arrays. fill (distances, Integer. MAX_VALUE); Arrays. fill (labels,-1); initCluste Rs (width, height, inPixels, blockSize, modifier); // loop to get all block/cells, image segmentation int loops = 0; boolean pixelChangedCluster = true; while (pixelChangedCluster & loops <maxClusteringLoops) {pixelChangedCluster = false; loops ++; // for each cluster center C for (int I = 0; I <clusters. length; I ++) {MyCluster c = clusters [I]; // for each pixel I in 2 S region around // cluster center in T xs = Math. max (int) (c. avg_x-blockSize), 0); int ys = Math. max (int) (c. avg_y-blockSize), 0); int xe = Math. min (int) (c. avg_x + blockSize), width); int ye = Math. min (int) (c. avg_y + blockSize), height); for (int y = ys; y <ye; y ++) {for (int x = xs; x <xe; x ++) {int pos = x + width * y; int tr = (inPixels [pos]> 16) & 0xff; int tg = (inPixels [pos]> 8) & 0xff; int tb = inPixels [pos] & 0xff; double D = c. distance (x, Y, tr, tg, tb, blockSize, modifier, width, height); if (D <distances [pos]) & (labels [pos]! = C. id) {distances [pos] = D; labels [pos] = c. id; pixelChangedCluster = true;} // end for x} // end for y} // end for clusters // reset clusters for (index = 0; index <clusters. length; index ++) {clusters [index]. reset () ;}// add every pixel to cluster based on label for (int y = 0; y 

MyCluster code:

package com.gloomyfish.image.cluster.effect;public class MyCluster {int id;double inv = 0; // inv variable for optimizationdouble pixelCount; // pixels in this clusterdouble avg_red; // average red valuedouble avg_green; // average green valuedouble avg_blue; // average blue valuedouble sum_red; // sum red valuesdouble sum_green; // sum green valuesdouble sum_blue; // sum blue valuesdouble sum_x; // sum xdouble sum_y; // sum ydouble avg_x; // average xdouble avg_y; // average ypublic MyCluster(int id, int in_red, int in_green, int in_blue, int x,int y, double S, double m) {// inverse for distance calculationthis.inv = 1.0 / ((S / m) * (S / m));this.id = id;addPixel(x, y, in_red, in_green, in_blue);// calculate center with initial one pixelcalculateCenter();}public void reset() {avg_red = 0;avg_green = 0;avg_blue = 0;sum_red = 0;sum_green = 0;sum_blue = 0;pixelCount = 0;avg_x = 0;avg_y = 0;sum_x = 0;sum_y = 0;}/* * Add pixel color values to sum of previously added color values. */void addPixel(int x, int y, int in_red, int in_green, int in_blue) {sum_x += x;sum_y += y;sum_red += in_red;sum_green += in_green;sum_blue += in_blue;pixelCount++;}public void calculateCenter() {// Optimization: using "inverse"// to change divide to multiplydouble inv = 1 / pixelCount;avg_red = sum_red * inv;avg_green = sum_green * inv;avg_blue = sum_blue * inv;avg_x = sum_x * inv;avg_y = sum_y * inv;}double distance(int x, int y, int red, int green, int blue, double S,double m, int w, int h) {// power of color difference between// given pixel and cluster centerdouble dx_color = (avg_red - red) * (avg_red - red)+ (avg_green - green) * (avg_green - green) + (avg_blue - blue)* (avg_blue - blue);// power of spatial difference between// given pixel and cluster centerdouble dx_spatial = (avg_x - x) * (avg_x - x) + (avg_y - y)* (avg_y - y);// Calculate approximate distance D// double D = dx_color+dx_spatial*inv;// Calculate squares to get more accurate resultsdouble D = Math.sqrt(dx_color) + Math.sqrt(dx_spatial * inv);return D;}}
V. Refer to here

This filter is a simple application of the SuperPixel algorithm. Most of the time, we may be more familiar with it.

In fact, SuperPixel is one of the K-Means and other image segmentation algorithms.

Notice:

The blog has been updated since this month. Please pay attention to it. It has disappeared for a year.

I became my first draft on image processing. Thank you for your kindness.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.