120 likes | 258 Views
數位影像處理 Digital Image Processing. 吳育龍老師. Read image data. 1024. Screen Resolution : 1024 X 768. 768. Gray Level. PImage img; void setup() { size(1000,500); img = loadImage("lenna.png"); } void draw() { background(0,0,0); img.loadPixels(); int index=0;
E N D
Read image data 1024 Screen Resolution:1024 X 768 768
Gray Level PImage img; void setup() { size(1000,500); img = loadImage("lenna.png"); } void draw() { background(0,0,0); img.loadPixels(); int index=0; for (int y=0; y<img.height;y++) for (int x=0; x<img.width;x++) { float r=red(img.pixels[index]); float g=green(img.pixels[index]); float b=blue(img.pixels[index]); float ncolor=(r+g+b)/3; //將rgb轉換成灰階->rgb三個值相同 stroke(ncolor); point(x+500,y); index++; } image(img, 0,0); }
Black and White PImage img; void setup() { size(1000,500); img = loadImage("lenna.png"); } void draw() { background(0,0,0); img.loadPixels(); int index=0; for (int y=0; y<img.height;y++) for (int x=0; x<img.width;x++) { float r=red(img.pixels[index]); float g=green(img.pixels[index]); float b=blue(img.pixels[index]); float ncolor=(r+g+b)/3; if(ncolor>125) ncolor=255; else ncolor=0; //決定黑或白的門檻值 stroke(ncolor); point(x+500,y); index++; } image(img, 0,0); }
影像處理 • PImage • Fields • width- Image width • height - Image height • pixels[]- Array containing the color of every pixel in the image • Methods • get() - Reads the color of any pixel or grabs a rectangle of pixels • set() - Writes a color to any pixel or writes an image into another • copy() - Copies the entire image • mask() - Masks part of the image from displaying • blend() - Copies a pixel or rectangle of pixels using different blending modes • filter() - Converts the image to grayscale or black and white • save() - Saves the image to a TIFF, TARGA, PNG, or JPEG file • resize() - Changes the size of an image to a new width and height • loadPixels() - Loads the pixel data for the image into its pixels[] array • updatePixels() - Updates the image with the data in its pixels[] array 補充:主程式視窗就是一個Image
Image Mask PImage img; PImage maskImg; size(500,300); background(255); img = loadImage("bear.jpg"); maskImg = loadImage("mask.jpg"); image(img, 50, 50); img.mask(maskImg); image(img, 250, 50);
Image Filter PImage img; PImage maskImg; size(700,500); background(255); //原圖 img = loadImage("bear.jpg"); image(img, 50, 50); //黑白, 參數為黑白門檻 img = loadImage("bear.jpg"); img.filter(THRESHOLD,0.8); image(img, 250, 50); //灰階 img = loadImage("bear.jpg"); img.filter(GRAY); image(img, 450, 50); //反色 img = loadImage("bear.jpg"); img.filter(INVERT); image(img, 50, 250); //模糊, 參數為模糊程度 img = loadImage("bear.jpg"); img.filter(BLUR,5); image(img, 250, 250); //色階, 參數為顏色bit數 img = loadImage("bear.jpg"); img.filter(POSTERIZE, 4); image(img, 450, 250);
Image Blend PImage img; PImage maskImg; size(700,500); background(255); img = loadImage("bear.jpg"); maskImg = loadImage("child.jpg"); img.blend(maskImg, 0, 0, 68, 200, 132, 0, 68, 200,ADD); image(maskImg, 132, 0); image(img, 0, 0); img = loadImage("bear.jpg"); maskImg = loadImage("child.jpg"); img.blend(maskImg, 0, 0, 68, 200, 132, 0, 68, 200,SUBTRACT); image(maskImg, 482, 0); image(img, 350, 0); img = loadImage("bear.jpg"); maskImg = loadImage("child.jpg"); img.blend(maskImg, 0, 0, 68, 200, 132, 0, 68, 200,DARKEST); image(maskImg, 132, 200); image(img, 0, 200); img = loadImage("bear.jpg"); maskImg = loadImage("child.jpg"); img.blend(maskImg, 0, 0, 68, 200, 132, 0, 68, 200,LIGHTEST); image(maskImg, 482, 200); image(img, 350, 200); <Reference> PImage->Blend
Image Pixel PImage img; int dimension; float cr,cg,cb; int threshold=50; void setup() { size(200,200); img = loadImage("bear.jpg"); dimension = (img.width*img.height); cr=cg=cb=-1; } void draw() { background(200,0,0); img.loadPixels(); for (int i=0; i < dimension; i++) { if(abs(red(img.pixels[i])-cr)<threshold && abs(green(img.pixels[i])-cg)<threshold && abs(blue(img.pixels[i])-cb)<threshold) img.pixels[i] = color(255); } img.updatePixels(); image(img, 0,0); } void mousePressed() { loadPixels(); int cho=pixels[mouseX+mouseY*width]; cr=red(cho); cg=green(cho); cb=blue(cho); println("Red: "+cr+" Green: "+cg+" Blue: "+cb); } 此種作法稱為Chroma Key或去背
Video Filter import processing.video.*; Capture video; int alphac; void setup() { size(320, 240); video = new Capture(this, width, height, 30); alphac=50; } void draw() { if (video.available()) { video.read(); image(video, 0, 0, width, height); fill(255,100,100,alphac); rect(0,0,width/2,height); fill(100,100,255,alphac); rect(width/2,0,width,height); } }
Motion Blur int alphac; void setup() { size(320, 240); alphac=10; background(255); } void draw() { fill(255,255,255,alphac); stroke(255,255,255,alphac); rect(0,0,width,height); fill(255,0,0); stroke(255,0,0); ellipse(mouseX,mouseY,10,10); }
外部濾鏡函式庫 • http://www.jhlabs.com/ • http://www.jhlabs.com/ip/filters/index.html • import java.awt.image.BufferedImage; • import com.jhlabs.image.*; • BufferedImage b1,b2; • PImage p1; • BoxBlurFilter bbf; • p1=loadImage("pic.png"); • b1=(BufferedImage)p1.getImage(); • b2=(BufferedImage)p1.getImage(); • bbf=new BoxBlurFilter(2,2,2); • bbf.filter(b1,b2);