Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Image Processing with Plain CBMP Library
Synthetic Images
ORT Braude Engineering College, Course: Image Processing 31651,Lecturer: Dr. Samuel Kosolapov [email protected]
Image Creation with Lens
Real 2D Image
Real 3D Object
Thin Convex Lens:
Real Image Formation
Basic Eye Structure: Image Formation on the Retina
4
To the Brain
Basic Eye Structure: PhotoReceptors:
Finite Number of Rods and Cones
5
Basic Eye Structure: PhotoReceptor as Electronic DeviceLight leads to “structural changes” Current/Voltage changes
6
Basic Eye Structure: PhotoReceptor as Electronic Device:
Light Electric Signal
7
Basic Eye Structure: Final Number Of Receptors
Two points can/cannot be resolved Resolution concept
Pixel
8
Resolution concept PixelOnly Final Number of “pixels”
(Picture elements == pixel)are needed to be presented to the human eye
9
Considering “digital encoding of the light intensity” (details later)
Human Eye can distinguish Only Limited Number of “Gray Levels”
10
Human Eye has 4 types of PhotoReceptors:Rods (for the night “gray” vision)
and 3 types of Cones (day color vision)[Details later]
11
3 Types of color PhotoReceptors 3 basic colors (R G B)
are enough for human vision
12
Pixels: Gray and Color(RGB) images creation
13
BW Image as 2D matrix of PIXELS = Picture Elements
* * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * *
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Simplest image: BW – Black and White image. Two values of Pixel(s) are possible:Pixel Value "0" := Black : "*" ; Pixel Value "1" := White: "Space". 1 bit per pixelExample: White Rectangle on Black background
2D Matrix of BW pixelsPixel “A”:Row = 0;Column = 0;Pixel Value = 0 (Black)
History: “Console” presentationPixel “A”:Row = 0;Column = 0;Pixel Value = 0 (Black): “*”
14
Gray Image as 2D matrix of PIXELSGray Image:
Pixel Value 0 corresponds to BLACKPixel Value 255 corresponds to WHITE
Pixel Values between 0 and 255 are seen as GRAY. (256 Gray Levels) Pixel value is Byte
Generally: 1 Byte per Pixel is allocated (8 bit per pixel)
2D Matrix of Gray pixelsPixel “A”:Row = 0;Column = 0;Pixel Value = 0 (Black)
Pixel “B”:Row = 3Column = 6Pixel value = 127 (Gray)
0
100 121 112 133 132 131 127 122 55 199 200 100Row = 3
Co
lum
n = 6
15
RGB (True Color) Image as 3D matrix of PIXELS
RGB Pixel: Vector: {R, G, B} - Red, Green, Blue = 3 Bytes per Pixel, 24 bit per pixel
Each pixel on the computer monitor is made up of a triadof red, green, and blue phosphors
From the distance > 30 cm Human Eye “see” triad as some colorIndividual pixels are practically undistinguishable (CGA, VGA)
16
RGB Color treatment
Color RGB
Red {255,0,0}
Yellow {255,255,0}
Green {0,255,0 }
Cyan {0,255,255}
Blue {0,0,255}
Magenta {255,0,255}
White {255,255,255}
Black {0,0,0}
17
RGB Color presentation
Digital Color : RGB Space– 3 Basic colors
256 levels are good enough for Human Eye
Number of possible colors =256 Levels of Red*256 Levels of Green
*256 Levels of Blue
= ~ 16 millions POSSIBLE colors
== 24 bit per pixel
== true color image
RGB Color digital presentation : {R,G,B} Black : {0,0,0}, White: {255,255,255}Red: {255,0,0}
18
Gray Image Declaration2D Matrix of bytes
1 byte per pixel
#define NUMBER_OF_ROWS (240)#define NUMBER_OF_COLUMNS (320)
unsigned char ImageA [ NUMBER_OF_ROWS] [NUMBER_OF_COLUMNS] ;
Explain: unsigned char, macro usage, count from 0, +/- 1 error out of range
19
True Color Image Declaration3D Maitix of bytes3 bytes per pixel
#define NUMBER_OF_ROWS (240)#define NUMBER_OF_COLUMNS (320)
unsigned char ImageA [ NUMBER_OF_ROWS] [NUMBER_OF_COLUMNS] [3];
32 Bit Per Pixel: RGB + Alpha Channel: transparency (Starting from Windows Vista)32 bpp Image Quality: Is equivalent to 24 Bit Per Pixel
20
Sources of Images
• Digital camera,
• Medical Device
• File (BMP, JPG, etc.)
• Synthetic Image
Synthetic Image == Test Image Usage in the Image Processing: To check algorithms.
We know exactly what the input is(Camera – unknown noise is always present)
21
Goal: Create Set of Simple Test Images(Compare with: Signal Generator in electronics laboratory)
• White Rectangle on Black Background
• Rectangle of Color #1 on Background of Color #2
• Analog Gray Ramp
• Digital Gray Ramp
• Funny Color Image
• Gray Gaussian
• Gray 1D and 2D sin
• BW Fence
22
BMP Library: Goal & Rationale• We need to “create”/”process” images and present
results on the monitor• “Console” application has no simple means to present
image on the monitor (explain history of…)• This can be done by using Windows GUI API or by using
newer .NET approach.• But writing Windows GUI applications require OOP and
experience • Typical solution is: “create”/”process” images in the
“console” application and store image as file• BMP file will be used as appropriate format• JPEG format is too complex + compression creates
many problems in Image Processing
23
Multi File Project Structure
ImProc.cppContains:
#Include ImProcInPlainC.h to provide access to BMP functionsmain() function calling functions created by student
Functions to be created by students according to the assignment
ImProcInPlainC.h
Contains declarations and BMP functions prototypes
No need to modify this file except setting image sizes
ImProcInPlainC.cpp
Contains implementationof the BMP functions
No need to modify this file except bugs correction
24
ImProcInPlainC.h: Declarations#pragma once
// The following declarations are needed for Plain C programming#define CGA_WIDTH (320) // Please do not change#define CGA_HEIGHT (240) // Please do not change
#define VGA_WIDTH (640) // Please do not change#define VGA_HEIGHT (480) // Please do not change
#define SIZE_256 256 // for simple FFT tests#define SIZE_512 512 // for CT and MRI files
#define NUMBER_OF_ROWS (CGA_HEIGHT)#define NUMBER_OF_COLUMNS (CGA_WIDTH)
#define NUMBER_OF_COLORS (3)#define B (0)#define G (1)#define R (2)
25
ImProcInPlainC.h: Clarifications
/*Image size is restricted to properly aligned images like 320x240, 512x512
WARNING: Big image sizes may create Real-Time problems in STACKS640x480 may be big size
In that case declare images "globally" - bad practice, but...
Example of declaration of the Gray Image:unsigned char Image[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
Example of declaration of the BGR Image:unsigned char
Image[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS];
*/
26
ImProcInPlainC.h: “Store”functions prototypes declarations
void StoreBgrImageAsTrueColorBmpFile( unsigned char image[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS],char bmpFileName[] );
void StoreBgrImageAsGrayBmpFile( unsigned char image[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS],char bmpFileName[] );
void StoreGrayImageAsTrueColorBmpFile( unsigned char image[][NUMBER_OF_COLUMNS],char bmpFileName[] );
void StoreGrayImageAsGrayBmpFile( unsigned char image[][NUMBER_OF_COLUMNS],char bmpFileName[] );
27
ImProcInPlainC.h: “Load”functions prototypes declarations
void LoadBgrImageFromTrueColorBmpFile( unsigned char image[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS],char bmpFileName[] );
void LoadBgrImageFromGrayBmpFile( unsigned char image[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS],char bmpFileName[] );
void LoadGrayImageFromTrueColorBmpFile( unsigned char image[][NUMBER_OF_COLUMNS],char bmpFileName[] );
void LoadGrayImageFromGrayBmpFile( unsigned char image[][NUMBER_OF_COLUMNS],char bmpFileName[] );
28
ImProcInPlainC.h: Servicefunctions prototypes declarations
// Service functionsvoid WaitForUserPressKey();
void ConvertGrayImageToColorImage( unsigned char source[][NUMBER_OF_COLUMNS],unsigned char destination[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS]);
void ConvertColorImageToGrayImage( unsigned char source[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS],unsigned char destination[][NUMBER_OF_COLUMNS]);
29
ImProcInPlainC.cpp:“store/load & service ” functions
implementations
On this stage there is no need to explore this file.
It is Lecturer’ responsibility that implementation is correct
Later, explanations HOW to store/load BMP filesin a number of situations will be provided
30
ImProc.cpp: usage of“store/load & service” functions
void main(){
// Declare Gray Imageunsigned char Image[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
CreateGrayRectangle(Image);StoreGrayImageAsGrayBmpFile(Image, "GrayRectangle.bmp");
CreateGrayRamp(Image);StoreGrayImageAsGrayBmpFile(Image, "GrayRamp.bmp");
WaitForUserPressKey();}
31
ImProc.cpp: function CreateGrayRectangle
32
ImProc.cpp: function CreateGrayRamp
Remind: BYTE Arithmetic : 255+1 = 0; 255+2 =1
33
Run “ImProc.exe” (F5 or from current directory)
34
2 Standard BMP files created
Created “synthetic” BMP filescan be seen by any appropriate “viewer”,
for example MS PAINT
35
To see details of BMP image:use “BMP Viewer”
36
image[row][column] = column;
Demonstrate & Explain usage
To do: create really important Test-Image: “Analog Ramp” (Change the size to 100x1024)
37
To do: create really important Test-Image: “Digital Ramp” (Change the size to 100x1024)
38
Show Mach Bands(Google images of )
Experiment:How much Gray Level
Can really be distinguished
To do: create really important Test-Image: “Fence” (Change the size to 100x1024)
39
Warning: Use only “Pixel Viewer” like “BMP Viewer”Otherwise “strange effects” will be seen
Explanation (alias) will be provided in a due time
To do: create really important Test-Image: “2D Gaussian”
40
2
exp
xxF
2
0exp
xxxF
2
0
2
0 expexp,yx
yyxxyxF
To do: create really important Test-Image: “2D Gaussian”
41
2
0
2
0 expexp,yx
yyxxyxF
Recommendation for writing Math Code
42
Polite advice: Write complex math function in one line of code
only in case You are participating in the contest“Write the shortest unclear code in C”
Use a number of variables and advance step by step.Leave “optimization” to compiler
(in case You are not creating code for “Hetz” project)
Code of “2D Gaussian”
43
To do: create the following Test-Image “1D sin” Important for FFT: Value[0]==Value[Last]
Explain: Low/High space frequency(Illusion: 3D effect “Space Columns)
44
To do: create the following Test-Images “2D sin”
Important for FFT: Explain: Low/High space frequencies wx wy
45
To do: create the following Test-Image
46
Color Rectangle
To do: create the following Test-Images
47
Funny 1
Funny 2
Code for “Funny Image1”
48
Play with code and Explore Profile
Code for “Funny Image 2”
49
Play with code and Explore Profile
Blending Technique
50
Image C = Image A + Image BQ1. What does this may meanQ2. How to do
A1. Matrix C = Matric A + Matrix B(Matrix A,B,C are of the same size !!!)
A2. For each Pixel:ImageC[row,col] =
ImageA[row,col]+ImageB[row,col]
Blending Technique
51
Problem 1: Byte overflow(White + White = 200 + 200 = ?)
Solution: Use Factors
For each Pixel:ImageC[row,col] =
a* ImageA[row,col]+b*ImageB[row,col]
Blending Technique
52
Reminder: Specific VS problem(no more “DOS” development is welcome)
Arrays Declaration placement in “DOS C” (Stack or Heap ?)
Now Create something really funny
53
?