Intro Math Problem Solving November 2

Slides:



Advertisements
Similar presentations
Introduction to MATLAB The language of Technical Computing.
Advertisements

Matlab Tutorial. Session 1 Basics, Filters, Color Space, Derivatives, Pyramids, Optical Flow Gonzalo Vaca-Castano.
Digital Image Processing Lecture 3: Image Display & Enhancement
Laboratory of Image Processing Pier Luigi Mazzeo
CS Spring 2009 CS 414 – Multimedia Systems Design Lecture 4 – Digital Image Representation Klara Nahrstedt Spring 2009.
Image Display MATLAB functions for displaying image Bit Planes
Computer Science 111 Fundamentals of Programming I More Digital Image Processing.
Computational Biology, Part 23 Biological Imaging II Robert F. Murphy Copyright  1996, 1999, All rights reserved.
Digital Imaging and Image Analysis
1Ellen L. Walker ImageJ Java image processing tool from NIH Reads / writes a large variety of images Many image processing operations.
HUE Hue is color.. Hue Hue is the name of a distinct color of the spectrum—red, green, yellow, orange, blue, etc. It refers to the particular wavelength.
Insight Through Computing 22. Working with Image Files imread, imwrite, imshow, uint8, rgb2gray.
MSU CSE 803 Stockman Linear Operations Using Masks Masks are patterns used to define the weights used in averaging the neighbors of a pixel to compute.
Lecture 7 Sept 19, 11 Goals: two-dimensional arrays (continued) matrix operations circuit analysis using Matlab image processing – simple examples Chapter.
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
Insight Through Computing 23. Working with Image Files Cont’d Filtering Noise Edge Detection.
Matlab Tutorial Continued Files, functions and images.
Computer Vision Lecture 3: Digital Images
Machine Vision ENT 273 Image Filters Hema C.R. Lecture 5.
Digital Image Processing Image Enhancement Part IV.
Seeram Chapter #3: Digital Imaging
September 5, 2013Computer Vision Lecture 2: Digital Images 1 Computer Vision A simple two-stage model of computer vision: Image processing Scene analysis.
1 COMS 161 Introduction to Computing Title: Digital Images Date: November 12, 2004 Lecture Number: 32.
AdeptSight Image Processing Tools Lee Haney January 21, 2010.
Machine Vision ENT 273 Image Filters Hema C.R. Lecture 5.
Intelligent Vision Systems ENT 496 Image Filtering and Enhancement Hema C.R. Lecture 4.
 By Bob “The Bird” Fiske & Anita “The Snail” Cost.
L23. Working with Image Files imread, imwrite, imshow, uint8, rgb2gray.
CS 1114: Finding things Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Non-linear filtering Example: Median filter Replaces pixel value by median value over neighborhood Generates no new gray levels.
Simple Image Processing and Object Detection using Matlab Akshar Prabhu Desai.
Nobody’s perfect. Fixing that with Photoshop..
Computer Application in Engineering Design
(Project) by:- ROHAN HIMANSHU ANUP 70282
L24. More on Image File Processing
John Federici NJIT Physics Department
Pixels, Colors and Shapes
Linear Algebra Review.
1-Introduction (Computing the image histogram).
Week 13 - Monday CS 121.
IIS for Image Processing
"Digital Media Primer" Yue-Ling Wong, Copyright (c)2013 by Pearson Education, Inc. All rights reserved.
Enhancing a Document Part 1
Computer Vision Lecture 5: Binary Image Processing
CS 2770: Computer Vision Linear Algebra and Matlab
CS 1674: Intro to Computer Vision Linear Algebra Review
Enhancing a Document Part 1
Digital Image Processing using MATLAB
Computer Vision Lecture 3: Digital Images
Image Enhancement in the Spatial Domain
Nobody’s perfect. Fixing that with Photoshop..
CSC 381/481 Quarter: Fall 03/04 Daniela Stan Raicu
Spatial operations and transformations
Fundamentals of Programming I Introduction to Digital Image Processing
Digital Image Processing
Technique 6: General gray-level transformations
COMS 161 Introduction to Computing
Matlab tutorial course
Linear Operations Using Masks
Technique 6: General gray-level transformations
Magnetic Resonance Imaging
Programming for Engineers in Python
© 2010 Cengage Learning Engineering. All Rights Reserved.
L19. Two-Dimensional Arrays
© 2010 Cengage Learning Engineering. All Rights Reserved.
EET 2259 Unit 9 Arrays Read Bishop, Sections 6.1 to 6.3.
Intro Math Problem Solving October 31
Spatial operations and transformations
DIGITAL IMAGE PROCESSING Elective 3 (5th Sem.)
Presentation transcript:

Intro Math Problem Solving November 2 Lightening A Dark Image Repairing A Damaged Image Finding Edges in an Image Homework #9

Reference Chapter 12, Section 4 of our textbook “Insight Through Computing” discusses the way black-and-white and color image data is stored and manipulated. .

Lighten a Dark Image

Dark Image Many photographs are taken in bad light, or have been badly developed. Darkened images, in particular, are hard for the eye to interpret, whereas lighter shades are easier to "read". For this photograph, we can display how the dark and light shades have been used by counting how many times each gray shade was used, between 0 and 255. In other words, we want a bar plot or histogram.

Convert Back and Forth When working with image data, sometimes it will be easier to make a "double" copy of the uint8 array, and then use any numerical operations we want, and at the end, convert the whole array back to uint8 format: A = double ( I ); % <- copy I ... % <- work on A I = uint8 ( A ); % <- replace I with results

Why So Dark? A histogram of the gray levels will show how often each shade is used. However, MATLAB's hist() command will only accept "double" information, and it must be a vector: I = imread ( 'snap.png' ); G = double ( I ); <- Convert to double [ m, n ] = size ( G ); gvec = reshape ( G, m * n, 1 ); <- Column vector hist ( gvec, 256 ); <- histogram

RESHAPE: Matrix -> Vector MATLAB’s histogram plotting command hist(g,n) expects the input to be a 1D vector of type “double”, but we want to histogram all the data in an image (a 2D matrix of type uint8). We have seen before how to use the double() function to convert the uint8 data to the right type. We introduce MATLAB’s reshape() command: a_new = reshape ( a_old, m_new, n_new ); which copies data in the MxN object A_OLD into the M_NEW x N_NEW object A_NEW. We can easily copy the matrix data into a vector shape.

RESHAPE Examples A = [ 11, 12, 13; 21, 22, 23 ]; <- 2x3 matrix b = reshape ( A, 1, 6 ); <- 1x6 row vector b <- [ 11, 21, 12, 22, 13, 23 ]; C = reshape ( A, 3, 2 ); <- 3x2 matrix C <- [ 11, 22; 21, 13; 12, 23];

Heavy use of Darkest Grays

Lighten the Levels We are hardly using the lighter gray levels in the picture. Most of the picture information is presented in dark grays. Our eye has trouble seeing the details. A better picture would spread the information out. Here are two ways we can try: 1) Double all grays between 0 and 127; Grays above 127 become 255; 2) Gray -> sqrt(Gray/255) * Gray;

Doubling I = imread ( 'snap.png' ); i1 = ( I < 128 ); i2 = ( 128 <= I ); I2(i1) = 2 * I(i1); % <- Double the darks I2(i2) = 255; % <- Lights -> White imshow ( I2 );

Lightening by Doubling

Sqrt I = imread ( 'snap.png' ); G = double ( I ); G = sqrt ( G / 255 ) * 255; I3 = uint8 ( G ); imshow ( I3 );

Lighten by Sqrt

Compare Double vs Sqrt

Dark, Medium, Light We can separate the dark, medium and light sectors: I = imread ( 'snap.png' ); i1 = ( I < 25 ); i2 = ( 25 <= I & I < 100 ); i3 = ( 100 <= I ); I2 = I; I2(i1) = 0; I2(i2) = 127; I2(i3) = 255; imshow ( I2 );

3 shades: Black, Medium Gray, White

Repairing Damaged Images

Just a Bunch of Numbers 1458-by-2084 150 149 152 153 152 155 150 149 152 153 152 155 151 150 153 154 153 156 153 151 155 156 155 158 154 153 156 157 156 159 156 154 158 159 158 161 157 156 159 160 159 162

Dirt! 1458-by-2084 Note how the “dirty pixels” look out of place 150 149 152 153 152 155 151 150 153 154 153 156 153 2 3 156 155 158 154 2 1 157 156 159 156 154 158 159 158 161 157 156 159 160 159 162 Note how the “dirty pixels” look out of place

Can We Filter Out the “Noise”?

Idea 1458-by-2084 Assign “typical” neighborhood gray values to 150 149 152 153 152 155 151 150 153 154 153 156 153 ? ? 156 155 158 154 ? ? 157 156 159 156 154 158 159 158 161 157 156 159 160 159 162 Assign “typical” neighborhood gray values to “dirty pixels”

“Typical neighborhood gray values” Getting Precise “Typical neighborhood gray values” Could use Median Or Mean radius 1 radius 3 We’ll look at “Median Filtering” first…

Median Filtering Visit each pixel. Replace its gray value by the median of the gray values in the “neighborhood”.

Using a radius 1 “Neighborhood” 6 7 6 7 6 7 Before After

How to Visit Every Pixel m = 9 n = 18 for i=1:m for j=1:n Compute new gray value for pixel (i,j). end

Replace with the median of the values under the window. Original: i = 1 j = 1 Filtered: Replace with the median of the values under the window.

Replace with the median of the values under the window. Original: i = 1 j = 2 Filtered: Replace with the median of the values under the window.

Replace with the median of the values under the window. Original: i = 1 j = 3 Filtered: Replace with the median of the values under the window.

Replace with the median of the values under the window. Original: i = 1 j = n Filtered: Replace with the median of the values under the window.

Replace with the median of the values under the window. Original: i = 2 j = 1 Filtered: Replace with the median of the values under the window.

Replace with the median of the values under the window. Original: i = 2 j = 2 Filtered: Replace with the median of the values under the window.

Replace with the median of the values under the window. Original: i = m j = n Filtered: Replace with the median of the values under the window.

What We Need… (1) A function that computes the median value in a 2-dimensional array C: m = medVal(C) (2) A function that builds the filtered image by using median values of radius r neighborhoods: B = medFilter(A,r)

If n is even, then use : med = ( x(m) + x(m+1) )/2 Computing Medians x : 21 89 36 28 19 88 43 x = sort(x) x : 19 21 28 36 43 88 89 n = length(x); % n = 7 m = ceil(n/2); % m = 4 med = x(m); % med = 36 If n is even, then use : med = ( x(m) + x(m+1) )/2

Median of a 2D Array C function med = medVal( C ) [p,q] = size(C); x = reshape ( C, 1, p*q ); % x = sort ( x ); % middle = ceil ( ( p * q ) / 2 ) % med = x(middle); med = median ( x ); return end

Medians vs Means A = 150 151 158 159 156 153 151 156 155 151 150 155 152 154 159 156 154 152 158 152 152 158 157 150 157 Median = 154 Mean = 154.2

Medians vs Means A = 150 151 158 159 156 153 151 156 155 151 150 155 0 154 159 156 154 152 158 152 152 158 157 150 157 Median = 154 Mean = 148.2

Back to Filtering… m = 9 n = 18 for i=1:m for j=1:n Compute new gray value for pixel (i,j). end

Window Inside… m = 9 n = 18 New gray value for pixel (7,4) = medVal( A(6:8,3:5) )

Window Partly Outside… m = 9 n = 18 New gray value for pixel (7,1) = medVal( A(6:8,1:2) )

Window Partly Outside… m = 9 n = 18 New gray value for pixel (9,18) = medVal( A(8:9,17:18) )

function B = medFilter(A,r) % B from A via median filtering % with radius r neighborhoods. [m,n] = size(A); B = A; for i=1:m for j=1:n C = pixel (i,j) neighborhood B(i,j) = medVal(C); end

The Pixel (i,j) Neighborhood iMin = max(1,i-r) iMax = min(m,i+r) jMin = max(1,j-r) jMax = min(n,j+r) C = A(iMin:iMax,jMin:jMax) m A r = 1 r = 2 n

I = imread ( ‘glassware_noisy.png’ )

I2 = MedianFilter ( I );

What About Using the Mean instead of the Median? Replace each gray value with the average gray value in the radius r neighborhood. Bits of noise don’t get removed, just smeared around. They are still visible.

I3 = MeanFilter ( I )

capture representative values. Why it Fails 150 149 152 153 152 155 151 150 153 154 153 156 153 2 3 156 155 158 154 2 1 157 156 159 156 154 158 159 158 161 157 156 159 160 159 162 The mean does not capture representative values. 85 86 87 88

Finding Edges

Near an edge, grayness values change abruptly What is an Edge? Near an edge, grayness values change abruptly 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 100 100 200 200 200 100 100 100 200 200 100 100 100 100 200 100 100 100 100 100

General plan for showing the edges in in image Identify the “edge pixels” Highlight the edge pixels make edge pixels white; make everything else black 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 100 100 200 200 200 100 100 100 200 200 100 100 100 100 200 100 100 100 100 100

General plan for showing the edges in in image Identify the “edge pixels” Highlight the edge pixels make edge pixels white; make everything else black 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 100 100 200 200 200 100 100 100 200 200 100 100 100 100 200 100 100 100 100 100 BLACK W H I T E BLACK

The Rate-of-Change-Array Suppose A is an image array with integer values between 0 and 255 B(i,j) be the maximum difference between A(i,j) and any of its eight neighbors.

The Rate-of-Change-Array Suppose A is an image array with integer values between 0 and 255 Let B(i,j) be the maximum value in A(max(1,i-1):min(m,i+1),... max(1,j-1):min(n,j+1)) - A(i,j) Neighborhood of A(i,j)

Rate-of-change example 59 90 58 60 56 62 65 57 81 Rate-of-change at middle pixel is 30 Be careful! In “uint8 arithmetic” 57 – 60 is 0

function Edges(jpgIn,jpgOut,tau) % jpgOut is the “edge diagram” of image jpgIn. % At each pixel, if rate-of-change > tau % then the pixel is considered to be on an edge. A = rgb2gray(imread(jpgIn)); [m,n] = size(A); B = uint8(zeros(m,n)); for i = 1:m for j = 1:n B(i,j) = ????? end Built-in function to convert to grayscale. Returns 2-d array.

Recipe for rate-of-change B(i,j) % The 3-by-3 subarray that includes % A(i,j) and its 8 neighbors Neighbors = A(i-1:i+1,j-1:j+1); % Subtract A(i,j) from each entry Diff= abs(double(Neighbors)– ... double(A(i,j))); % Compute largest value in each column colMax = max(Diff); % Compute the max of the column max’s B(i,j) = max(colMax);

function I2 = rgb_edges(I,tau) % I2 is the “edge diagram” of image I. % At each pixel, if rate-of-change > tau % then the pixel is considered to be on an edge. A = rgb2gray(I); [m,n] = size(A); I2 = A; for i = 1:m for j = 1:n I2(i,j) = ????? end

function I2 = rgb_edges(I,tau) % I2 is the “edge diagram” of image I. % At each pixel, if rate-of-change > tau % then the pixel is considered to be on an edge. A = rgb2gray(I); [m,n] = size(A); I2 = A; for i = 1:m for j = 1:n Neighbors = A(max(1,i-1):min(i+1,m), ... max(1,j-1):min(j+1,n)); I2(i,j)=max(max(abs(double(Neighbors)– ... double(A(i,j))))); end

“Edge pixels” are now identified; display them with maximum brightness (255) threshold 1 1 1 1 1 1 1 1 1 1 90 90 1 1 1 90 90 90 1 1 90 90 90 90 if B(i,j) > tau B(i,j) = 255; end I2(i,j) 0 0 0 0 0 0 0 0 0 89 89 89 0 0 89 89 0 0 0 89 89 0 0 0 0 89 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 255 255 0 0 0 255 255 0 0 0 0 255 0 0 0 0

function I2 = rgb_edges(I,tau) % I2 is the “edge diagram” of image I. % At each pixel, if rate-of-change > tau % then the pixel is considered to be on an edge. A = rgb2gray(I); [m,n] = size(A); I2 = A; for i = 1:m for j = 1:n Neighbors = A(max(1,i-1):min(i+1,m), ... max(1,j-1):min(j+1,n)); I2(i,j)=max(max(abs(double(Neighbors)– ... double(A(i,j))))); if I2(i,j) > tau I2(i,j) = 255; end

I = imread ( ‘charlie.jpg’ )

I2 = rgb_edges ( I, 15 )

Threshhold = 40

Threshhold = 20

Threshhold = 30

Homework #9 hw047: Read a PNG file containing a grayscale image of Saturn, change to white all the pixels in a wide strip along the border of the image, and save the modified image to a JPG file. hw048: Read a PNG file containing a grayscale image of a scene from the movie "Casablanca"; copy Ingrid Bergman's head and use it to replace the heads of Humphrey Bogart and Dooley Wilson; save the image as a JPG file. hw049: Read a JPG file which is a photograph of a person. Make a new JPG file which only displays pixels that closely match a particular shade of the person's face. Homework #9 is due by midnight, Friday November 10th. Homework #8 is due by tomorrow night.