360 likes | 580 Views
Image Warps and Halftoning. Image Warping. Scale Rotate Warp Basically, we have to move image pixels This is done by mapping the pixels from the source to the destination image (actually backwards) then resampling. warp. Source image. Destination image. Image Warping. Overview Mapping
E N D
Image Warping • Scale • Rotate • Warp • Basically, we have to move image pixels • This is done by mapping the pixels from the source to the destination image (actually backwards) then resampling warp Source image Destination image
Image Warping • Overview • Mapping • Forward • Inverse • Resampling • Point sampling • Triangle filter (bilinear interpolation) • Gaussian filter
Mapping • Forward mapping: • Inverse mapping: v y u x
v y u x Mapping Examples • Scale (separately in horizontal and vertical)
v y u x Mapping Examples • Rotate counterclockwise θ degrees
Mapping Examples • General functions of u and v: swirl fisheye rain
Forward Mapping for (int u = 0; u < umax; u++) { for (int v = 0; v < vmax; v++) { float x = fx(u,v); float y = fy(u,v); dst (x,y) = src(u,v); } } source image destination image (u,v) f (x,y)
Multiple source pixels map to same destination No source pixels map to destination Forward Mapping • Iterate over source image • Need to resample destination • Generate one sample for each pixel • Complex to figure out where the nearest samples are for each pixel Rotate 30o
Inverse Mapping for (int x = 0; x < xmax; x++) { for (int y = 0; v < ymax; y++) { float u = fx-1(x,y); float v = fy-1(x,y); dst (x,y) = src(u,v); } } source image destination image (u,v) f-1 (x,y)
Inverse Mapping • Iterate over destination image • Need to resample source • Generate source sample for each destination pixel • May oversample source, but oversampling is simpler than in forward case Rotate -30o
Resampling • Need to evaluate source image at arbitrary (u,v) • (u,v) not generally integer coordinates • 3 methods • Point sampling • Triangle filtering • Gaussian filtering source image destination image (u,v) f-1 (x,y)
Point Sampling • Just find closest source pixel to each destination pixel int iu = round(u); int iv = round(v); dst(x,y) = src(iu, iv); • Simple, but causes aliasing Rotate -30o
(u1,v2) (u2,v2) a (u,v) b (u1,v1) (u2,v1) Triangle Filtering • Convolve four closest source pixels to desired (u,v) with triangle filter (bilinear interpolation) • Bilinear interpolation: • a = lerp(src(u1,v2),src(u2,v2)) • b = lerp(src(u1,v1),src(u2,v1)) • dst(x,y) = lerp(a,b)
Gaussian Filtering • Compute weighted sum of pixel neighborhood • Weights are normalized values of Gaussian function (u,v) w
Filter Comparison • Tradeoffs • Fast but aliased (point sampling) • Slow and blurry (Gaussian) Point sampling Bilinear (triangle) Gaussian
resample_src(u,v,w); w Image Warping Examples • Using reverse mapping and Gaussian filtering • for (int x = 0; x < xmax; x++) { for (int y = 0; v < ymax; y++) { float u = fx-1(x,y); float v = fy-1(x,y); dst (x,y) = resample_src(u,v,w); } } source image destination image (u,v) f-1 (x,y)
Scale • Scale(src,dst,sx,sy) • float w = max(1/sx,1/sy); • for (int x = 0; x < xmax; x++) { for (int y = 0; v < ymax; y++) { float u = x/sx ; float v = x/sy ; dst (x,y) = resample_src(u,v,w); } }
Rotate • Rotate(src,dst,θ) • for (int x = 0; x < xmax; x++) { for (int y = 0; v < ymax; y++) { float u = x*cos(-θ)- y*sin(-θ); float v = x*sin(-θ)+ y*cos(-θ); dst (x,y) = resample_src(u,v,w); } }
Swirl • Swirl(src,dst,θ,cx,cy) • for (int x = 0; x < xmax; x++) { • for (int y = 0; v < ymax; y++) • float u = (x-cx)*cos(-θ*|x-cx|)- (y-cy)*sin(-θ*|y-cy|)+cx; float v = (x-cx)*sin(-θ*|x-cx|)+ (y-cy)*cos(-θ*|y-cy|)+cy; dst (x,y) = resample_src(u,v,w); } }
Quantization Artifacts • Errors due to limited intensity resolution • Why? • Frame buffers only have so many bits per channel • Physical display devices have a limited dynamic range, especially hard copy devices
Uniform Quantization I(x) I I(x) P(x) x P(x) 4 bits/pixel
Uniform Quantization • Images with increasing bits per pixel 1 bit 2 bits 4 bits 8 bits
Dealing with Quantization • Halftoning techniques • Classical, or brute force halftoning • Dithering methods • Error Diffusion • Other techniques
Classical Halftoning • First Variant • First, use dots of varying sizes to represent intensities • Size of dot proportional to intensity I(x) P(x)
Classical Halftoning Newspaper image NYT 9/21/99
Classical Halftoning • What if your display device can’t display produce multiple dot sizes? • Use cluster of pixels • Number of “on” pixels in cluster proportional to intensity • Trades spatial resolution for intensity resolution
Dithering • Algorithms to distribute errors among pixels • Reduce objectionable regular artifacts • Replace them with noise Original – 8 bits/pixel Uniform quantization – 1 bit/pixel Floyd-Steinberg error diffusion – 1 bit/pixel
I I(x) P(x) x Random Dither • Randomize quantization error • Errors appear as noise I I(x) P(x) x
Random Dither Original – 8 bits/pixel Uniform quantization – 1 bit/pixel Random dither – 1 bit/pixel
Ordered Dither • Patterned errors that try to minimize regular artifacts • Recursively defined “Dither Matrix” stores pattern of thresholds
Ordered Dither Original – 8 bits/pixel Random dither – 1 bit/pixel Ordered dither – 1 bit/pixel
Floyd-Steinberg Error Diffusion • Quantization errors are spread over neighboring pixels to the right and below
Floyd-Steinberg Error Diffusion Original – 8 bits/pixel Random dither – 1 bit/pixel Ordered dither – 1 bit/pixel Floyd-Steinberg error diffusion – 1 bit/pixel