In my last post, Histogram Functions in Accelerate vImage, I looked using vImage's contrast stretching function to improve the contrast of an image. In this post, I'll look at another vImage histogram function: specification.This function allows us to apply an arbitrary histogram (typically from a suppled image) to an image. In the example above, the histogram from the leftmost photograph is applied to the sunset image in the centre to create the final blue tinted image on the right.

Technically, the histogram that was calculated from the leftmost image has been specified as the new histogram for the centre image.

Histogram Calculation

The first step to recreate the example above is to calculate the histogram. We'll use the same technique described in my previous post to convert a supplied CGImage to a vImage buffer. However this time, we're not populating a target image, we're populating four arrays of unsigned integers that will contain the histogram values for each of the four colour channels (red, green, blue and alpha).

vImage's API shows its C roots and to calculate the histogram of inBuffer,  we initialise four arrays, create pointers to those arrays and create pointers to an array of those pointers:


let alpha = [UInt](count: 256, repeatedValue: 0)
let red = [UInt](count: 256, repeatedValue: 0)
let green = [UInt](count: 256, repeatedValue: 0)
let blue = [UInt](count: 256, repeatedValue: 0)

let alphaPtr = UnsafeMutablePointer<vImagePixelCount>(alpha)
let redPtr = UnsafeMutablePointer<vImagePixelCount>(red)
let greenPtr = UnsafeMutablePointer<vImagePixelCount>(green)
let bluePtr = UnsafeMutablePointer<vImagePixelCount>(blue)

let rgba = [alphaPtr, redPtr, greenPtr, bluePtr]


let histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>>(rgba)

The histogram object, along with the source image, are passed to vImageHistogramCalculation_ARGB8888(_:_:_:):


vImageHistogramCalculation_ARGB8888(&inBuffer, histogram, UInt32(kvImageNoFlags))

The original arrays, alpha, red, green and blue, are now populated with the histogram data. 

Histogram Specification

Given the histogram data, we now need to specify it against another image (the sunset in our case). In my example, I've separated these two steps into two functions and my specification function requires a histogram tuple of the form:


histogram: (alpha: [UInt], red: [UInt], green: [UInt], blue: [UInt])

The vImage specification function, vImageHistogramSpecification_ARGB888(_:_:_:_:) requires an unsafe mutable pointer to an unsafe pointer of the pixel counts for each bucket for each channel. The following code prepares histogram to pass into vImage (vImagePixelCount is a type alias for UInt):


let alphaPtr = UnsafePointer<vImagePixelCount>(histogram.alpha)
let redPtr = UnsafePointer<vImagePixelCount>(histogram.red)
let greenPtr = UnsafePointer<vImagePixelCount>(histogram.green)
let bluePtr = UnsafePointer<vImagePixelCount>(histogram.blue)


let rgba = UnsafeMutablePointer<UnsafePointer<vImagePixelCount>>([alphaPtr, redPtr, greenPtr, bluePtr])

Using the inBuffer and outBuffer technique from the previous post, we can now specify the histogram for a source image and populate a vImage buffer:


vImageHistogramSpecification_ARGB8888(&inBuffer, &outBuffer, rgba, UInt32(kvImageNoFlags))

Wrapping in a Core Image Filter


If all that logic seems a bit daunting to implement every time you want to calculate and specify a histogram, it can all be wrapped up in a Core Image filter. Take a look at my VImageFilters.swift file under Filterpedia to see it in action. The final filter can be used in the same way as any other Core Image filter with one caveat: vImage sometimes crashes when run in background threads, so Filterpedia always runs the filter in the main thread. 

Conclusion

Histogram specification is a great tool for colorising monochrome images and matching colours of images for composition. vImage offers tools lacking in Core Image and, with a little work, its sightly arcane API can be hidden away inside a CIFilter wrapper. 

The demo code to accompany this post is available here.


0

Add a comment


It's been a fairly busy few months at my "proper" job, so my recreational Houdini tinkering has taken a bit of a back seat. However, when I saw my Swarm Chemistry hero, Hiroki Sayama tweeting a link to How a life-like system emerges from a simple particle motion law, I thought I'd dust off Houdini to see if I could implement this model in VEX.

The paper discusses a simple particle system, named Primordial Particle Systems (PPS), that leads to life-like structures through morphogenesis. Each particle in the system is defined by its position and heading and, with each step in the simulation, alters its heading based on the PPS rule and moves forward at a defined speed. The heading is updated based on the number of neighbors to the particle's left and right. 

The project set up is super simple: 



Inside a geometry node, I create a grid, and randomly scatter 19,000 points across it. An attribute wrangle node assigns a random value to @angle:
@angle = $PI * 2 * rand(@ptnum); 
The real magic happens inside another attribute wrangle inside the solver.

In a nutshell, my VEX code iterates over each point's neighbors and sums the neighbor count to its left and right. To figure out the chirality, I use some simple trigonometry to rotate the vector defined by the current particle and the neighbor by the current particle's angle, then calculate the angle of the rotated vector. 
while(pciterate(pointCloud)) {

    vector otherPosition;
    pcimport(pointCloud, "P", otherPosition);

    vector2 offsetPosition = set(otherPosition.x - @P.x, otherPosition.z - @P.z);
    float xx = offsetPosition.x * cos(-@angle) - offsetPosition.y * sin(-@angle);
    float yy = offsetPosition.x * sin(-@angle) + offsetPosition.y * cos(-@angle);
    
    float otherAngle = atan2(yy, xx); 

    if (otherAngle >= 0) {
        L++;
    } 
    else {
        R++;
    }   
}
After iterating over the nearby particles, I update the angle based on the PPS rule:
float N = float(L + R);
@angle += alpha + beta * N * sign(R - L);
...and, finally, I can update the particle's position based on its angle and speed:
vector velocity = set(cos(@angle) * @speed, 0.0, sin(@angle) * @speed);  
@P += velocity ;
Not quite finally, because to make things pretty, I update the color using the number of neighbors to control hue:
@Cd = hsvtorgb(N / maxParticles, 1.0, 1.0); 
Easy!

Solitons Emerging from Tweaked Model



I couldn't help tinkering with the published PPS math by making the speed a function of the number of local neighbors:
@speed = 1.5 * (N / maxParticles);
In the video above, alpha is 182° and beta is -13°.

References

Schmickl, T. et al. How a life-like system emerges from a simple particle motion law. Sci. Rep. 6, 37969; doi: 10.1038/srep37969 (2016).


5

View comments

  1. ok. I've got to finish current job, then crash course in programming, and ... this is very inspirational!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
About Me
About Me
Labels
Labels
Blog Archive
Loading