思いつきのわりにはおもしろかった

思いついて15分ぐらいでかいたのだけど,意外とおもしろかった.
マウスのドラッグでしきい値を変えていってふすまだけハワイになります.

Hawaii from nbqx on Vimeo.

import processing.core.*
import javax.swing.JFrame
import hypermedia.video.*

class Hello extends PApplet{
  def exp
  def th = 255
  OpenCV cv
  PImage img
  
  void setup(){
    size(320,240)
    smooth()
    
	noCursor()
	
    cv = new OpenCV(this)
    cv.capture(width,height)
	
	img = loadImage("hawaii.jpg")
  }

  void draw(){
    def blobs = []

	cv.read()
	cv.flip(OpenCV.FLIP_HORIZONTAL)
	cv.convert(OpenCV.GRAY)

	cv.absDiff()
	cv.threshold(th)

	blobs = cv.blobs(1000, width*height, 1, false)

	cv.restore()			  
	cv.convert(OpenCV.RGB)	  
	cv.flip(OpenCV.FLIP_HORIZONTAL)
	image(cv.image(),0,0)
	
	if(blobs.length>0){
		beginShape()
		blobs[0].points.each{p->
	  		vertex(p.x as Float, p.y as Float)
		}
		endShape(CLOSE)
	}

	applyImg()
  
  }
  
  def applyImg(){
	width.times{w->
	  height.times{h->
		def pos = h*width+w
		def c = get(w,h)
		if(c==color(255,255,255)){
		  set(w,h,img.pixels[pos])
		}
	  }
	}
  }

  void stop(){
    cv.stop()
    super.stop()
  }

  void mouseDragged(){
	th = map(mouseX,0,width,0,255);
  }
}

app = new Hello()
app.init()
new JFrame(title:"Hawaii",resizable:true).with{
  add(app)
  pack()
  defaultCloseOperation = JFrame.EXIT_ON_CLOSE
  visible = true
}