CodeBlocks

Friday 27 December 2013

Doge Coins

Recently as of the 6th December a new crypto currency has popped onto the market and gained a huge following in a very short amount of time, it is one of the fastest growing crypto currencies. That currency is Dodgecoin.



WoW much coin

With a market cap of 100 billion coins and over 13% of those already mined and with its biggest heist occurring on the on Christmas 2013 it also has tons of support in the reddit community. Dogecoin is gaining tons of attention and still growing strong.

Mining Dogecoin is similar to Litecoin since it also uses scrypt in its proof-of-work algorithm, meaning that you can configure cgminer to mine Dogecoins. Dogecoins currently get mined at a ridiculous speed, in only a few hours I've been able to mine 300 Dogecoins using a R9 270 graphics card. Ypool.net also support dogecoin with use of a proxy to work with their push through protocol.

The things that really separates dogecoins from all the other is the community, the community is extremely open and friendly and will constantly tip dogecoins to other members. And becuase of this dogecoins makes it easier to get into crypto currencies as a whole. It allows you to play around with it, with very little risk to you and you can then test the waters. Since all crypto currencies essentially work the same, you can transfer that knowledge to other currency.

So start mining play around with it the trading and get a general feel for how crypto currencies operate. It all quite simple really, it just seems scary.

Mining Dogecoins

To mine dogecoin using ypool.net you first have to download cgminer, then go to ypool.net login and select dogecoin as you currency(at current coin mode), then click on "how dig coin" and select the "Dogecoin | Doge" tab and download the proxy.

Then setup a worker under the "Many workers" -> "Many worker" links

Extract and run the proxy and keep it running, then in your cgminer folder create a new .bat file inside that put the following:

cgminer --scrypt --fix-protocol -o localhost:8332 -u <login username>.<worker name> -p <worker password>

so that it looks something like this

cgminer --scrypt --fix-protocol -o localhost:8332 -u accountName.workerName -p workerPassword 

Save and run the .bat file



DBSwVKhJax1DtUm7CKPZ3ZaRzmnGkX8aNM 

Thursday 26 December 2013

Mining Crypto Coins

Bit coins(Crypto Currency) have recently gained a lot of attention which has caused it's value to shoot through the roof and made early miners - people that get paid in bitcoins for compute power - scramble to find the coins they mined to cash in. Since the complexity and time it takes to mine bitcoins increases at regular intervals until 21million coins have been mined.

However for regular users mining bitcoins is no longer practical since ASIC (application-specific integrated circuit) for mining came along, these ASIC allows for far faster mining but can be costly, not to mention the power requirements it takes to mine a bitcoin with a decent setup is somewhat unpractical.

However there exists other Crypto Currencies such as LiteCoin, PeerCoin, PrimeCoin here a list of other crypto currencies. And one can always trade one crypto currency for another relatively easily.

So how do you mine crypto currencies? Well first you need a few things, 
  1. Type of crypto currency you want to mine (LiteCoin, BitCoin etc..)
  2. Mining hardware (CPU, GPU, FPGA, ASIC)
    1. Not all coins can be mined with GPU, FPGA, ASIC
  3. Mining Software for that crypto currency
  4. Wallet to store you coins for that crypto currency
  5. An account at a pool
So lets work with Primecoins which at this moment can only be mined with cpu, the process to mine other coins are almost exactly the same and if lost reddit will most likely have a community that can help you out.

Step 1 - Mining software
Typically just google for primecoin mining software but as of now the most popular software is  jhPrimeminer which you can get here

Step 2 - Get a wallet
A wallet is a program that will store you coins, you can get a wallet here
The wallet will take a while to update but you can start mining before it is done. 

Step 3 - Create an account
Setup an account at ypool.net here your mining software will connect to and get job to compute they will then either store the money with them or payout into your wallet. Once there click on workers there should already be one. Add a password to it and click on update

Step 4 - Setup the mining software
Go to the folder where you extracted jhPrimeminer create a new .bat file which you can name mine.bat, inside that .bat file add the following:
start jhPrimeMiner.exe -o http://ypool.net -xpt -u <username>.<workername> -p <password>

eg.
start jhPrimeMiner.exe -o http://ypool.net -xpt -u jj.worker_1 -p badpass

Where username is you ypool account username, workername is the name your worker (found in the worker tab of ypool) and password is the worker password NOT you ypool password

Step 5 - Setup payment to you wallet
To get the money to go to you wallet you have to get your wallets address which can be found in the wallet under the Transactions tab and will look like a hash string of random characters. Copy this address right click -> copy address. Then in ypool go to account-> main options and put that address in there.

Now double click on the .bat file you created and presto.

Sunday 6 October 2013

Finding Duplicate Videos

Absence

Well its been a while since my last post mainly due to work and when I wasn't working I was doing my hobbies which involved traveling a lot, so past while Ive been going places rock climbing. But I'm not going to go into too much detail about that.





So anyway in this post I wanted to talk about how to find duplicate videos files, now I have written a while ago a program that can do this with a high accuracy. I wanted to talk about the different approaches I've tried and how to go about doing those approaches.

The hard part is deciding what information about the video you are going to use to compare against other videos, the simplest is using metadata such as the name, length and resolution however this is far from optimal and many videos can go unmatched.

The better option would be to match the visual content or perceptual information from a video to other videos, or some other technique such as key frame intervals, match audio tracks.

The method I chose was to extract the RGB values for a frame and calculate its kurtosis, variance and average and then build a timeline(and array of values) of how each of these values changes per frame per colour channel. I have created a earlier post that details how to do this here (its pretty much doing that image processing and storing the results). After the timeline was created I would perform a kurtosis and variance on the timeline itself and then loosely match videos that had a similar resulting values. 

Note that is wasn't necessary to process the whole video but only about 2min of footage to build a fairly unique timeline

Using this method I was capable of correctly matching videos that had a offset, encoding artifacts, different frame rates, different resolutions combinations of all four.

Sunday 7 July 2013

JQuery Mobile Tips

So over the past month I was developing a mobile webapp using JQuery Mobile and Grails and during that time the amount of silly glitches and bugs I encountered was relatively small but SO GOD DAMN annoying. So im going to post some of the common once I encounteted and how to solve them.

First off if you're experiencing JQueryMobile performance issues add the following CSS settings.

/*JQuery Mobile Optimisation */
.ui-shadow,.ui-btn-up-a,.ui-btn-hover-a,.ui-btn-down-a,
.ui-body-b,.ui-btn-up-b,.ui-btn-hover-b,.ui-btn-down-b,
.ui-bar-c,.ui-body-c,.ui-btn-up-c,.ui-btn-hover-c,
.ui-btn-down-c,.ui-bar-c,.ui-body-d,.ui-btn-up-d,
.ui-btn-hover-d,.ui-btn-down-d,.ui-bar-d,
.ui-body-e,.ui-btn-up-e,.ui-btn-hover-e,
.ui-btn-down-e,.ui-bar-e,.ui-overlay-shadow,
.ui-shadow,.ui-btn-active,.ui-body-a,.ui-bar-a 
{
    text-shadow: none;
    box-shadow: none;
    -webkit-box-shadow: none;
}

/*Enable CSS hardware acceleration*/ 
.hwaccel {  -webkit-transform: translateZ(0); } 


The visual impact is quite small but the performance gained is about 100% If you're having strange visual problems when linking from one page to another such as flashes, blinking where the screens flickers first then displays the page, the problem is with JQueryMobile transitions, there are 3ways to fix this. Set the transition in the to none this doesnt always want to work and its to much work to maintain. 2nd you can download a plugin that fixes the transitions but doesn't work with the new JQueryMobile 1.3.1. Or do this:

This will disable transitions for all links in a page. If you want to link to a page without using AJAX is sometimes it causes weird issues with redirects and linking add this to a tag data-rel="external" data-ajax="false" If you're mobile app support rotation(Which IT SHOULD!) and you have JQueryMobile Headers and Footers that don't always resize on rotation add the following Javascript:

If you're developing for Apple iOS as well then you might encounter a small zooming glitch where the device will automatically zoom in when selecting form items, to fix this disable zooming.

And there you go that will fix most of the annoying issues you might encounter with JQueryMobile. I'll update this when I encounter more bugs and fixes.

Sunday 12 May 2013

x264 Settings Comparison - Reference and BFrames

So its been a while since my last post, its quite difficult to find time for everything these days. So in this post im going to cover Reference frames, BFrames fast/optimal, since these don't influence quality and only compression, there is no need for screenshots.

Reference Frames
Default - 3
Controls the size of the DPB(Decoded Picture Buffer). Values range from 0-16. This is the number of previous frames each P-frame can use as a reference. 

Using more refernece frames increases memory usage and cpu load during playback, caused by having all the decoded reference frames in memory during playback. This can cause mobile devices or players to be unable to playback the video.

B-Frames
Default - 3 Fast
Sets the maximum number of consecutive B-Frames. B-Frames are capable of using motion prediction of future frames, this can lead to a significant compression gain.

Two different method to determine B-Frames, Fast and Optimal.

Reference Frames Compression

Futurama SD



Filesize Speed over reference frames
Performance compared to Medium

Reel Steel SD

Filesize speed over reference frames

Performance compared to Medium

Reel Steel HD

Filesize, Speed over reference frames

Performance compared to medium


It seems like animation benefits more from extra reference frames, than the live action, however the compression gained tapers off very quickly and appear to bottom out at around 8 reference frames. Interestingly the performance loss also tapers out.


B-Frames

Futurama SD


Fast Method
Optimal
Fast Method
Optimal
 

Reel Steel SD

Fast


Optimal
Fast
Optimal


Reel Steel HD

Fast
Optimal
Fast
Optimal

B-Fast method seems to have a near consistent speed regardless of the number of B-Frames and B-Optimal seems to slow down at a constant rate, however optimal performs much better at compression than fast with about 6% more compression, with only 5% speed impact.

Conclusion

Whilst the number of reference frames does improve performance it can cause other playback issues in memory and speed and the compression gained is not that significant. A safe value is 3, more than that and the compression gained for speed lost is large.

In BFrames we saw that optimal does a far better job at compression than Fast, but at a slower rate at around beyond 4 BFrames both start looses compression with some even doing worse at high values than lower ones. Also at 4 BFrames optimal is only marginally slower than Fast but provides about 6% extra compression

Recommended

Reference Frames : 3-5
B-Frames: Optimal : 3

Wednesday 3 April 2013

Creating dynamic account passwords

Creating dynamic account passwords

One of the worst things with the online world is passwords. Not just that you have to have a password, but that you have to have one for almost everything nowadays. And worst yet it that everything should have a different password(not that most people do). And when you do a different one, you can never remember which one you used where.

The thing is most people do reuse the one(also crappy) password they have[1], this means that if someone were to crack your password they would get everything, provided they can find where it is used. The reason why people do it is because a strong password is damn hard to remember without writing it down, since it contains: Uppercase, lowercase, numbers and special characters. and resembles something like :2cf@#dba5Fb0, no one will remember a password like this, let alone one for every site. Instead people will opt for a more comfortable approach. This is probably the reason why to most common passwords last year were:
  • password
  • 123456
  • 12345678
  • jesus
  • ninja
  • mustang
  • password1
And sometimes to "strengthen" it they add some numbers at the end(usually there birthday). Turning ninja into ninja24. To a computer that added almost no extra complexity.

But what if I told you that creating a different secure password for every site is quite easy? In fact you don't even have to remember you password. You are going to have to remember a few things, since what we're going to do is re-generate your password every time you use it. To do this we require two things, the two things you'll will have to remember. A pass phrase(think current password) and a formula.

How does it work?

Every time we log into a site we use our passphrase + formula to create our password. The formula will ensure our password is different every time, the pass phrase ensure that we know the key to generate it.

The formula stays the same all the time, however everybody can use a different formula. Im going to show you how a formula works and how you can create your own simple formula that you can use.

 

Going through the formula

So the formula is simple in that we are going to generate a set of Strings or characters and add that to our existing one.

For this example I'm going to generate a password for Facebook.

So the first thing I need is a pass phrase, now I suggest you look up a word in a dictionary then generate a random number from 10-100 and use that. For this example lets go with the pass phrase cold25.

So lets first check how strong cold25 is.
PasswordMeter :30%, Weak.
Secure Password : 0.54s to crack
Strength Test : Very Weak
MS Password Checker : weak

Step 1
So that pass phrase is crap. But we're not going to use it as our password. So lets make it more secure first lets take the first phrase of the website(or the last, or the whole) and stick it to our pass phrase(or the end, or the middle of after 2, or 5), I'm going to stick it on the back.

Step 2
That gives us cold25Face. Already better plus every site will have a different password! but the "Face" kinda gives away a hint :/. So lets shift our keys a bit, so now when you type "Face" move your fingers 1 to the left or right, or up, or down, or cycle through them(you can also use the values of your numbers as well). Lets go to the right:

if you reach an edge you can make your own rule either wrap around, move back or stay or whatever

The result: cold25Gsvr
Lets see how strong that is

PasswordMeter :72%, Strong.
Secure Password6 billion years to crack
Strength Test : Reasonable
MS Password Checker : Medium
Already looking much better and for that extra computing complexity we didn't really do anything, but we can do more:

Step 3
Lets all caps our website data(Face), or the pass phrase.
cold25GSVR
now lets use those numbers of ours(25) 2 and 5, and the length of the website data(Face) which is 4 and lets add that to our numbers. 2+4 =6, 5+4=9 and use those numbers for special characters. If your numbers exceed 10 then use just wrap around(if you warp around just use the last digit of the number) or backtrack the remainders on the keyboard.
6=^
9=(

and I'm going to append the key that most(or least) lines up with those as well as the length digit of the website data(4)
giving us: ^(bc,

And stick it to the end creating:
cold25GSVR^(bc,

And this gives us a new password strength of:
PasswordMeter :100%, Strong.
Secure Password : 4 trillion years
Strength Test : Strong
MS Password Checker : Strong

Example Formula
So the formula we used was: (pass phrase) + allcaps(Shift 1 Right(first syllable of web address)) + (Special chars of(numbers+website data)) + the keys they line up with best.

So using that formula lets create a password for Gmail
  1. cold25H<SO: (Caps + shift keys to right)
  2. cold25H<SO:&) (2+5=7 ="&", 5+5=10=")") = &)
  3. cold25H<SO:&)n.v (add keys they line up with) = n.v
and that password is how strong?
PasswordMeter :100%, Strong.
Secure Password : 412 trillion years
Strength Test : Strong
MS Password Checker : Strong

You can decide the complexity of your own formula and how many steps you want to do and if you want to do different steps, as you saw just by doing coldGSVR we already improved our password significantly, and every site will have a different password. I'm saying this since I know must of you will be to lazy to do anything extra.

So if you want to create your own formula, then just change the rule I used and the order I used them in so maybe first do the special character(^(bc,), then the pass phrase (cold25) and then the caps + key shift web address(GSVR) to create "^(bc,cold25GSVR". Or just use this as an idea on creating formula in the first place.

Now the password isn't perfect and using a formula isn't perfect either, the formula can easily be cracked by experts, but it gives an additional layer of security.

Monday 1 April 2013

X264 - ADM and Ab Frames- Comparison


x264 - ADM and Ab Frames - Comparison

Most of the next series of X264 related posts will be a lot lighter and smaller so I'm going to cover more settings. The settings I'll cover in thos post will be Adaptive Direct Mode(ADM) and Adaptive b-Frames(Ab-Frames)

Adaptive Direct Mode 
Also known as direct in the x264 settings, with 3 potential settings:
  1. None - Disabled direct motion vectors
  2. Spatial(default)
  3. Temporal
  4. Auto - x264 will swap between Spatial and Temporal.

Adaptive b-Frames 
Also known as b-adapt, this determine the placement of b-Frames and controls how x264 decides between adding a b-Frame of a P-frame. Since b-Frames are the most compressed out of all the frames having more b-frames should increase compression without having a image quality impact. b-adpt has 3 values to choose from and also uses the max bFrames setting to determine max consecutive beframes.
  1. Off/Disabled - Will always force the max number of consecutive bFrames after any p frame, not recommended with high consecutive b-frames as it will lead to some interesting stutter within the frame
  2. Fast - Fast attempt to determine bFrames and the quality and performance is not affective by the max number of consecutive bFrames.
  3. Optimal -  A slower but more accurate method, but performance will go down depending on the max number of consecutive bFrames. 

Since these don't really effect the quality of the video there is no reason to have any still frames.

Futurama  

Adaptive Direct Mode

So lets dive into it, these to graphs represents the file-size and performance of each method.


Filesize in KB
Performance FPS


Here we can see that auto performs almost identical to spatial suggesting that x264 uses spatial more often than temporal when left on auto, but since x264 has to do extra computations to determine which is better spatial out performs in performance. Interestingly none is the slowest of all the settings and the largest.

The next graph will show the relative differences in percentage between the settings when compared to spatial(default).

Percentage diff

As you can see none is 10.5% slower and yet 1.5% larger.
Temporal is 0.38 larger and 0.64 slower
Auto is 0.01 smaller but 1.13% slower

So during SD anime spatial is the best compression/performance wise
Note that it is said that auto is better suited for 2pass encodings.


Adaptive b-Frames

Filesize in KB

Performance FPS

Since bFrames are extremely small forcing bFrames at all time will lead to a smaller file size just like off does(max bFrames is 3), and since no additional decisions are made it is also faster, but not as fast as Fast. And with optimal having smaller filesize than fast.

 The next graph will show the relative differences in percentage between the settings when compared to Fast(default).

Percentage Diff



The first thing to note it that all of them are slower than Fast, however all of them have better compression than Fast. With optimal having a very good compression/performance ratio, with about 6% compression but only 4.3% slower.



ReelSteel SD
Adaptive Direct Mode
Well lets see how things change with Live Action Video.


Filesize in KB


Performance FPS

Percentage Diff

Interestingly optimal is only 0.47% slower but has 4.75% more compression


ReelSteel HD
Adaptive Direct Mode
Although I don't think HD video will have much of an impact on the data, I'm curious to see if the trend continues where Spatial and Optimal are very good.

Filesize in KB
 
Performance FPS


Percentage Diff


 Again nothing special Spatial performs exceptional well against the other settings

Although I don't think HD video will have much of an impact on the data, I'm curious to see if the trend continues where Spatial and Optimal are very good.
 Adaptive Direct Mode 
 I assume there won't be  much of a difference between HD and SD


Filesize in KB
Perormance FPS
Percentage Diff

 And the only diffirence is that Off is faster by 2% with about the same compression, and optimal is slower by 4.4% than in SD. Other than that it the same.

Conclusion
Well as there isn't that much to say other than spatial consistently had the best compression/performance ratio in Adaptive Direct mode. And Optimal had better compression than fast with a minimal to negligible performance impact.

Recommended
Adaptive Direct Mode: Spatial. is the best setting.

Adaptive b-Frames: Optimal - In my opinion the small performance hit is well worth compression gained, for now at least. We should see how they perform with varying max consecutive bFrames, which we will look into next time.

 Navigation
  1. Introduction
  2. Presets
  3. Subme  
  4. Motion estimation method 
  5. ADM and b-adapt.

Sunday 31 March 2013

Identifying Duplicate Images

Identifying similar images and determining which are identical. 


So in this post I want to talk about how you can write a program that will be able to compare images against each other and determine if they're identical or very similar. Basicly the same thing tineye and google's reverse image search does. So if you've ever wondered how they work then this post might clear things up a bit. In a follow up post I also want to describe how we can apply a similar technique to identify duplicate videos.

The Trick?
There is no, one solution to the problem and every solution has its advantages and disadvantages,  the approach im going to be using is based off of the histogram of the images. A very simple yet very effective method, the draw back of it being false positives and it can only match images that are visually very similar. But it can identify images with varing qualities of:
  • Resolution
  • Rotation
  • Filesize
  • format
  • compression
  • quality

What we are going to do is, extract the distribution of colours that make up the image and plot the frequecy of those colours. Also known as a histogram, in short a histogram will show us how much of each colour value(0-255) in each colour range(RGB) is used in the image.

The histogram represents the frequency of each colour value.

Two images that are visually identical will have a very similar histogram(98%) depending resolution and other factors. However images can have they same histogram yet not be visually similar, but at least we are certain to find duplicates, and these false positive detection is a problem we can reduce.





As the figures above show that two identical looking images with different properties in both size and resolution share a near identical histogram.

The Algorithm

The algorithm in broken up into 3 main sections. 
  1. Finding the images
  2. Analysis
    1. Load Images
    2. Extract Colour Map
    3. Generate Histogram
    4. Normalise Histogram
  3. Comparison

Step 1 - Finding the images

We can hardcode the locations of the images into the program, this is useful when just testing the analysis of the algorithm, but for practical purposes we want the program to be able find those images for us. To do this we will use another algorithm to recursively load each directory and subdirectory and find all the images, and add those to a list.

Select a Directory
public void LoadDirectory()
{  
 JFileChooser fileChooser = new JFileChooser();
 int result = fileChooser.showOpenDialog(null);
 
 if(result==JFileChooser.APPROVE_OPTION)
 {
  URL fileURL = null;
  try
  {
   fileURL = fileChooser.getSelectedFile().toURL();
   
   String _folderPath = fileURL.toString().substring(6);
   String[] folderPath = _folderPath.split("/");
   
   for(int i=0; i<folderPath.length-1; i++)
   {
    directory = directory+folderPath[i]+"/";
   } 
   
  }
  catch(Exception e)
  {}
 } 
 File folder = new File(directory);
 LoadSubDirectories(folder);
}
So the above snippet will allow us to pick a folder to start our search, and the last method called LoadSubDirectories(folder) will then load all the subdirectories in that folder 
Select Directory view



Load Subdirectories
public static int spc_count=-1;
private void LoadSubDirectories(File folder)
{    
    spc_count++;
  
    String spcs = "";
     
     for (int i = 0; i &lt spc_count; i++)
       spcs += " ";
     
     if(folder.isFile())
     {  
   String[] temp3 = folder.getName().split("\\.");
   
   if(
     temp3[1].equalsIgnoreCase("jpg")||
     temp3[1].equalsIgnoreCase("jpeg")
    )
   {                 
//Adds file location to ImageObject(my own class) you can just add
//absoluteFile parameter to an ArrayList
            ImageObject iObject = new ImageObject(folder.getAbsoluteFile());
   }   
     }
     else if (folder.isDirectory()) 
     {
       File[] listOfFiles = folder.listFiles();
       
       if(listOfFiles!=null) 
       {
  for (int i = 0; i &lt listOfFiles.length; i++)
   LoadSubDirectories(listOfFiles[i]);
       } 
     }
     
     spc_count--;

     if(spc_count==-1)
     {
  //Add logic of what to do when done searching 
     }   
}
This code will then load all the subdirectories and find all images ending with a jpg, jpeg and add those to a list, or in my case an ImageObject(own class), which I also use to store their features(histograms).

Step 2 - Analysis

In this step we are going to use the absolute addresses we found in step one to load those images and perform analysis on them. To perform analysis on the images we are going to have to do a few things
1. Load the image into the program
2. Extract the colour map
3. Generate Histogram
4. Normalise Histogram

2.1 - Loading the image

for(ImageObject listIfImages : iObject)

BufferedImage image = null;
try
{
//now this does use my class(ImageObject) to store and 
//load the images But all you have to do is replace
//iObject.toString() with the absolute path.toString()
//of the image 

image = ImageIO.read(new File(iObject.toString()));  

        Imager.process(image);//my class that processes the image

//Just storing the histograms into the image object, 
//so that I can save and load it to disk
//easier so that I don't have to process an 
//image more than once.
iObject.redHisto = Imager.getRedHisto();
iObject.greenHisto = Imager.getGreenHisto();
iObject.blueHisto = Imager.getBlueHisto();
}
catch(Exception e)
{}
So in my ImageProcessor Class I will have an a method that takes a buffered image. 2 - Extract the Colour map

2.2 - Extract Colour Map

public final void process(BufferedImage _image)
{   
 width = _image.getWidth(this);
 height = _image.getHeight(this);
 
 final int[] rawPixels = new int[width * height]; 
 PixelGrabber grabber = new PixelGrabber
 (
  _image,
  0,
  0,
  width,
  height,
  rawPixels,
  0,
  width
 );
  
 try
 {  
  grabber.grabPixels();
 }
 catch(Exception e)
 {}
 
 final int[][] pixels = new int[width][height];
 
 alpha = new int[width][height];  
 red = new int[width][height];
 green = new int[width][height];
 blue = new int[width][height];
   
int offset = 0;

//This double for loop is where the colour map gets made
//This colour map will be used to generate the histogram
//Note that is section can take the longest and might cause
//Java virtual machine to crash due too large images if this 
//happens do the following select the project:
//properties->Run/Debug Settings-> {class of main method}  
//(x)=Arguments->VM arguments->-Xmx1024m
for (int i = 0, n=width; i < n; i++) 
{
 for (int j = 0, m=height; j < m; j++) 
 { 
     pixels[i][j] = rawPixels[offset];
     // extract the 4 fields
     alpha[i][j] = (pixels[i][j] >> 24) & 0xff;
     red[i][j]   = (pixels[i][j] >> 16) & 0xff;
     green[i][j] = (pixels[i][j]  >>  8) & 0xff;
     blue[i][j]  = pixels[i][j]  & 0xff;
     
     offset++;
 }
}

redHisto = this.createHistogram(red);
greenHisto = this.createHistogram(green);
blueHisto = this.createHistogram(blue);
alphaHisto = this.createHistogram(alpha);

return feature;
}
In case you missed the comment in the middle if your JVM bombs out due to a memory error do the following: Right click on the project and select properties. Click Run/Debug Settings and select the class where your main method is located and click edit, select the (x)=Arguments tab and enter in the VM arguments text area: -Xmx1024


Solving JVM memory problem

2.3 - Generate Histogram

To actually generate a histogram from the colourmap is ridiculously simple
/**@_colorMap created an array of type int that represent 
 *the histogram of that colour
 * @histogram the array returned
**/
private final int[] createHistogram(int[][] _colorMap)
{
 int[] histogram = new int[256];
 
 for(int i=0, n=width; i<n; i++)
 {
  for(int j=0, m=height; j<m; j++)
  {
   histogram[_colorMap[i][j]]++;
  }
 }  
 return histogram;
}

So what this code snippet above does is create a histogram from the colormap that is parsed through. A new array of type int and size of 256(0-255) is created, each array index will then be the corresponding colour value. Such that all instances of red-145 will be stored in histogram[145]. So the trick is that we access the the Array with the value of the _colormap[i][j] and just increment that index.

We then simple return that array, which now represents that colour's histogram.

2.4 - Normalise Histogram

Since we want to compare images of all sizes you have to realise that larger versions of images will have larger value in the histogram array, but relatively speaking those histograms will be near identical. This we need normalise the histogram so that ALL histograms will have the same upper limit. Now you can scale the histogram right after you've generated it and just before you returned it, or you can do it during comparison.

I did mine during comparison so that I could save a raw copy of the histogram since during normalisation certain data is lost. Of course this is slightly slower since I would have to regenerate the histogram each time.

I'm no mathematician so there probably exists a more elegant solution than mine. But in short we going to find the largest value in the histogram and see how close it is to our limit, then divide or multiple all the values by the difference depending on if its larger or smaller;

private int[] normaliseHistogram(int[] _histo)
{
 double scaler=0.0;
 int largestValue=0;
 
 //Normalise all values relative to 255
 int limit = 255;
 
 //Finding Largest Value
 for(int i=0; i<_histo.length; i++)
 {
  if(_histo[i] > largestValue)
   largestValue = _histo.length;
 }
 
 //Calculate the scaler value
 scaler = largestValue/limit;
 
 for(int i=0; i< _histo.length; i++)
 {
  //If scaler is larger than 1 we devide all value by it
  if(scaler > 1)
  {
   _histo[i] =  (int) (_histo[i]/scaler);
  }
  else //if histo smaller than 1 we multiple all the values
  {
   _histo[i] = (int)(_histo[i]*scaler);
  }
 }
 
 return _histo;
}

Step 3 - Comparison

And only one thing left to do and that compare the histograms with each other. So to compare to histograms with each other.
public double compareHistogram(ImageObject _image1, ImageObject _image2, int _color)
{
 int tempHistImage1 = _image1.getHisto(_color);
 int tempHistImage2 = _image2.getHisto(_color);
 
 for(int i=0; i<256; i++)
 {
  double diff = 0;

  TotalIntervals++;
  
  if(temp1>=temp2)
  {
   diff += temp2/temp1*100;    
  }
  else 
  {
   diff += temp1/temp2*100; 
  }
 }
 return diff;
}

Note that this method takes in two ImageObjects again this is my own class I use to represent the data of each image such as there histograms, all I do with these objects is extract there histograms so I can use them. In this code snippet I compare the red histograms with each other, this code will have to be done for both green and blue.
Now all that’s left is to establish the threshold and see if it passes it, if it does then the images are a match.
histogramThreshold = 80.0;

if(compareHistogram(image1, image2, 1)>histogramThreshold)
 return true;


My final program

The image above is of my program that uses the same algorithm as above with some modification, including faster matching and reduced false positives. As well has the ability to find duplicate videos.

Sunday 17 March 2013

Java simple threading library

Simple threading in Java using JavaCP library

In this post I'm going to talk about a simple way to achieve multi threading and parallel computation using a simple library I've assembled based off of the work of Weimin Xiao and the rest at this stack overflow post.

I take no credit in any of the code written in the library it self(the code below is mine) I merely just assembled a simple library and have used it to successfully achieve embarrassingly thread execution. If you're not familiar with embarrassing threading, it is threading tasks that in no way affect the outcome of each other. E.g having to process a large library of files, this task can be easily threaded by having each task processed in a separate thread. Which is primarily what I've used it for in processing Photos and videos to find perceptually similar content. I'll discuss video and photo matching algorithm in a later post.

1. Where to get the library? here,
2. How to use the library?
  1. Place the JavaCP.jar anywhere you want we you can find it.
  2. In your IDE(eclipse etc..) add the JavaCP.jar as a library
    • In ecplise this is done by right clicking on the project root folder in the package explorer window and selecting properties
    • In the properties window select Java Build Path
    • Select the Libraries tab
    • Click on Add External JARs, navigate to where you placed the JavaCP.jar and select ok.

The library is now ready to be used.
The library is able to thread 2 different things. ForEach Loops and concurrent method execution.

In your program import the library

//Import the library
import JavaCP.*;

Example code to run Tasks
//Using it to execute Multiple methods concurrently
//The number of threads created depends on the number of cores available
Parallel.Tasks(new Task []
{
 
 //task-1
 new Task() {public void run()
 {
  loops.runLoop(1000000000);
 }},
   
 //task-2
 new Task() {public void run()
 {
  loops.runLoop(1000000000);
 }},
 
 new Task() {public void run()
 {
  loops.runLoop(1000000000);
 }},
 
 new Task() {public void run()
 {
  loops.runLoop(1000000000);
 }}
});
In this example the method loops.runLoop(int _value) will be executed multiple times along side other instance of the loops.runLoop(int _value).

A more practical use would be something like this, where each method as to do something time consuming since creating threads themselves are taxing.
Parallel.Tasks(new Task []
{
 //task-1
 new Task() {public void run()
 {
  feature[0] = Variance(red,0);   
 }},
   
 //task-2
 new Task() {public void run()
 {
  feature[1] = Kurtosis(red,0);
 }},   
 //task-3
 new Task() {public void run()
 {
  feature[2] = Variance(green,1);
 }}, 

 //task-4
 new Task() {public void run()
 {
  feature[3] = Kurtosis(green,1);
 }}, 
 //task-5 
 new Task() {public void run()
 {
  feature[4] = Variance(blue,2);
 }},
 //task-6
 new Task() {public void run()
 {
  feature[5] = Kurtosis(blue,2);
 }} 
});
The local array feature[] stores the result of the method execution. So what about ForEach loops? Well they will look a bit different following the next type of structure
Parallel.ForEach(arrayListName, new LoopBody()
{
 public void run(String _objectNameofItemInArray) 
 {
          //code of what must happen to the object
  objectNameofItemInArray.invokeMethod();
 }
}
such that it could look like this example
ArrayList listOfFiles = new ArrayList()

Parallel.ForEach(listOfFiles, new LoopBody()
{

 public void run(ImageObject p) 
 {
  p.process();
 }
}

//In ImageObject class there is a method called process()

public class ImageObject
{
  public ImageObject(){} //Default constructor

  public void process()
  { 
    //ToDo add method logic
  }
}
In this example listOfFiles is an ArrayList of type ImageObject which has a method called process. Threads will be created and multiple ImageObject will then be processed concurrently. There you go a simple way to achieve a basic level of multi threading. Both code snippets are from a project I created to identify duplicate videos and photos.

Saturday 16 March 2013

X264 - Motion Estimation Method- Comparison

Last time we looked at the effects of the various subpixel estimation complexity, today we will be looking at how the motion estimation method(MeM) impacts the filesize(all measured in KB), quality and performance(average FPS) according to CRF18 of x264.

There are a total of 5 methods these are in order of complexity:
  • Diamond(dia) - The most simplest method, checking motion vectors at one pixel up, left, down and right and picking the best candidate, this process repeats until it can't find a better motion vector
  • Hexagon(Hex) - Work similarly like Diamond but consists of a range-2 and 6 surrounding points, more efficient than diamond with little performance impact.
  • Uneven multi-hexagon(Uhm) - While slower than Hex, it is able to avoid missing harder-to-find motion vectors. The Me-Range parameter controls its search radius.
  • Exhaustive(Esa) - An optimised intelligent search of the complete motion vector space within Me-Range. Mathematically equivalent to brute force but faster, but still slower than Uhm.
  • Transformed exhaustive(Tesa) - is an algorithm which attempts to approximate the effect of running a Hadamard transform comparison at each motion vector; like exhaustive.

So now that we are more familiar with MeM lets see how it performs.


Futurama SD


The top graph shows the FileSize(Graph area) in KB as well as the performance(line) of each of the different methods. As you can see the relationship between compression and performance takes a turn for the worst with Esa and Tesa.

The bottom graph represents the difference in percentage for speed and compression compared to the default(medium) setting. Here we can see that with Esa for 29.5% performance reduction we get a measly 2.56% extra compression and Tesa does even worse. Uhm compresses better with a slight performance hit of 6.71% slower. Of course will have to see if this translates into any visual artefacts

Still Frame
Diamond
Hexagon

Uhm

Esa

Tesa





















Even though I can see a VERY slight difference between the different methods, I cannot say that one looks better than the other. 


Smaller Motion Detail
Diamond
Hexagon
Uhm
Esa
Tesa

























Again I can notice a difference between the methods but cannot say which is actually higher quality than the rest.

Reel Steel SD
 


So moving onto live action video, we can see that the same trend repeats it self with esa and tesa performing very poorly when compared to dia, hex and uhm.


Still Frame
Diamond
Hexagon





Uhm
Esa






Tesa









As with Futurama its difficult to tell which is better quality.


Smaller Motion Detail
Diamond
Hexagon

Uhm

Esa
Tesa








  .

 

 







And yet again the frame quality is too consistent to say which is better. Maybe we'll see a bigger deference with HD videos

 

Reel Steel HD 

So looking at HD video we see that esa and tesa performs worse than Uhm and is significantly slower. Hexagon seems like a good default method.


Still Frame 
Diamond
Hexagon
Uhm
Esa
Tesa

 

 

 

 

 

 

 

 

 








Yet again with MeM and with HD I can hardly see any difference let alone which is higher quality   

Smaller Motion Detail
Diamond
Hexagon
Uhm
Esa
Tesa

 



















So yet a again its almost impossible to tell which is which and which is better than the other.

Conclusion

Well as you can see visually there isn`t much of a difference, in terms of compression Uhm did consistently better than hexagon or diamond, and esa, tesa also performed better except at HD videos. Regarding performance Esa and Tesa took a massive performance hit, with Uhm only being slightly slower.

Recommended
Hexagon - Performed consistently close to the others.
Uhm - A slight compression gain at a margin performance loss.


 Navigation

  1. Introduction
  2. Presets
  3. Subme  
  4. Motion estimation method