Background ❖The Basics of Intensity Transformations and Spatial Filtering • The term spatial domain refers to the image plane itself, and image processing methods in this category are b
Trang 1XỬ LÝ ẢNH TRONG CƠ ĐIỆN TỬ
Machine Vision
1
TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI
Giảng viên: TS Mạc Thị Thoa Đơn vị: Bộ môn Cơ điện tử, Viện Cơ khí
Hà Nội, 2020
Trang 2Chapter 3 Intensity Transformations and Spatial Filtering
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
❖Two principal categories of spatial processing are intensity transformations and
spatial filtering
➢ Intensity transformations operate on single pixels of an image for tasks such
as contrast manipulation and image thresholding
➢ Spatial filtering performs operations on the neighborhood of every pixel in an
image
➢ Examples of spatial filtering include image smoothing and sharpening.
Trang 3Chapter 3 Intensity Transformations and Spatial Filtering
1 Background
2 Some Basic Intensity Transformation Functions
3 Histogram Processing (xử lý Histogram-biểu đồ tần suất)
4 Fundamentals of Spatial Filtering (Nguyên lý lọc trên miền không
7 Highpass, Bandreject, and Bandpass Filters from Lowpass Filters
8 Combining Spatial Enhancement Methods (Các phương pháp cải
thiện chất lượng ảnh)
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
Trang 41 Background
❖The Basics of Intensity Transformations and Spatial Filtering
• The term spatial domain refers to the image plane itself, and image processing methods
in this category are based on direct manipulation of pixels in an image.
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
• Understand the meaning of spatial domain processing, and how it differs from transform
domain processing.
• Be familiar with the principal techniques used for intensity transformations.
• Understand the physical meaning of image histograms and how they can be manipulated for
image enhancement.
• Understand the mechanics of spatial filtering, and how spatial filters are formed.
• Understand the principles of spatial convolution (tích chập) and correlation (Tương quan)
• Be familiar with the principal types of spatial filters, and how they are applied.
• Be aware of the relationships between spatial filters, and the fundamental role of lowpass
filters.
Trang 51 Background
❖The Basics of Intensity Transformations and Spatial Filtering
➢ The spatial domain processes are based on the expression
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
where f(x, y) is an input image, g(x, y) is the
output image, and T is an operator on f defined
over a neighborhood of point (x, y).
A 3x3 neighborhood about a point (x0, y0) in an image The neighborhood is moved from pixel to pixel in the image to generate an output image.
hoặc trên các điểm ảnh của các ảnh các nhau
Trang 61 Background
❖The Basics of Intensity Transformations and Spatial Filtering
➢ intensity (also called a gray-level, or mapping) transformation function, s and r to
denote, respectively, the intensity of g and f at any point (x, y)
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
The result of applying the transformation to every pixel
in f to generate the corresponding pixels in g
would be to produce an image of higher contrast than
the original, by darkening the intensity levels below k and brightening the levels above k In this technique, sometimes called contrast stretching
Trang 71 Background
❖The Basics of Intensity Transformations and Spatial Filtering
➢ intensity (also called a gray-level, or mapping) transformation function
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
T(r) produces a two-level (binary) image A
mapping of this form is
called a thresholding function
Trang 82 Some Basic Intensity Transformation Functions
❖Intensity transformations are among the simplest of all image processing techniques
❖ The values of pixels, before and after processing, by r and s
❖ With digital quantities, values of an intensity transformation function typically arestored in a table, and the mappings from r to s are implemented via table lookups For
an 8-bit image, a lookup table containing the values of T will have 256 entries
❖ Three basic types of functions used frequently in image processing: linear (negative and identity transformations), logarithmic (log and inverse-log transformations), and power-
law (nth power and nth root transformations).
❖ The identity function is the trivial case in which the input and output intensities areidentical
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
Trang 9Some basic intensity transformation functions Each curve was scaled independently
so that all curves would fit in the same graph Our
interest here is on the shapes of the curves, not on their relative values
Image Negatives
The negative of an image with intensity
levels in the range [0, L-1] is obtained by
using the negative transformation
function
Trang 102 Some Basic Intensity Transformation Functions
❖Image Negatives The negative of an image with intensity levels in the range is
obtained by using the negative transformation function
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
(a) A digital mammogram (b) Negative image obtained using Eq (3-3) (Image (a) Courtesy of General Electric Medical Systems.)
Trang 112 Some Basic Intensity Transformation Functions
❖Log Transformations
• maps a narrow range of low intensity values in the input into a wider range of output
levels, higher values of input levels are mapped to a narrower range in the output
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
where c is a constant and it is assumed that r 0
(a) Fourier spectrum displayed as a grayscale image (b) Result of applying the log
transformation in Eq (3-4) with c = 1 Both images are scaled to the range [0, 255].
Trang 122 Some Basic Intensity Transformation Functions
❖Power-Law (Gamma) Transformations
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
where c and are positive constants
Plots of the gamma equation s = cr for various values
of (c = 1 in all cases) Each curve was scaled
independently so that all curves would fit in the same graph Our interest here is on the shapes of the curves,
not on their relative values.
Trang 132 Some Basic Intensity Transformation Functions
❖Power-Law (Gamma) Transformations
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
(a) Image of a human retina (b) Image as
as it appears on a monitor with a gamma setting of 2.5 (note the darkness) (c) Gammacorrected image (d) Corrected image, as it appears on the same monitor (compare with the original image) (Image (a) courtesy of the National Eye Institute, NIH)
Trang 142 Some Basic Intensity Transformation Functions
❖Power-Law (Gamma) Transformations
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
a) Magnetic resonance image (MRI- Hình ảnh cộng hưởng từ) of a fractured human spine- vùng xương sống bị gãy (the region of the fracture is enclosed by the circle) (b)–(d) Results of applying the transformation in Eq (3-5) with c =1 and γ= 0.6,0.4 and 0.3, respectively (Original image courtesy of Dr David R Pickens, Department of Radiology and Radiological Sciences, Vanderbilt
University Medical Center.)
Trang 152 Some Basic Intensity Transformation Functions
❖Power-Law (Gamma) Transformations
➢ Another illustration of power-law transformations
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
(a) Aerial image (b)–(d) Results of applying the transformation in Eq (3-5) with = 3.0, 4.0 and 5.0, respectively
(c = 1 in all cases.) (Original image courtesy of NASA.)
Trang 162 Some Basic Intensity Transformation Functions
❖Piecewise Linear Transformation Functions
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
Contrast stretching (a) Piecewise linear transformation function (b) A low-contrast electron microscope image of pollen, magnified
700 times (c) Result of contrast stretching (d) Result of thresholding (Original image courtesy
of Dr Roger Heady, Research School of Biological Sciences, Australian National University, Canberra, Australia.)
where rmin and rmax denote
the minimum and maximum
intensity levels in the input
image, respectively
where m is the mean intensity
level in the image
Trang 172 Some Basic Intensity Transformation Functions
❖Piecewise Linear Transformation Functions
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
Trang 182 Some Basic Intensity Transformation Functions
❖Piecewise Linear Transformation Functions
➢ Intensity-Level Slicing Cắt theo mức
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
Figure 2: (a) Aortic angiogram (b) Result of using a slicing transformation
of the type illustrated in Fig 1(a) , with the range of intensities of interest selected in the upper end of the gray scale (c) Result of using the
transformation in Fig 1(b) , with the selected range set near black, so that the grays in the area of the blood vessels and kidneys were preserved (Original image courtesy of Dr Thomas R Gest, University of Michigan Medical School.)
Trang 192 Some Basic Intensity Transformation Functions
❖Piecewise Linear Transformation Functions
➢ Bit-Plane Slicing Trích chọn bít: Mục đích là để làm nổi bật các thành phần trên toàn
ảnh bởi việc sử dụng các bít đặc biệt
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
Bit-planes of an 8-bit image.
Trang 202 Some Basic Intensity Transformation Functions
❖Piecewise Linear Transformation Functions
➢ Bit bậc cao có độ bảo toàn thông tin tốt hơn bit bậc thấp
➢ Bit bậc thấp thường biểu diễn nhiễu hoặc nền
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
Để trích chọn bit có nghĩa nhất: bit thứ n và hiện thị chúng,
dùng biến đổi sau:
Trang 212 Some Basic Intensity Transformation Functions
❖Piecewise Linear Transformation Functions
➢ Bit-Plane Slicing
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
(a) An 8-bit gray-scale image of size pixels (b) through (i) Bit planes 8 through 1, respectively, where plane 1 contains the least significant bit
Each bit plane is a binary image Figure (a) is an SEM image of a
trophozoite that causes a
disease called giardiasis
(Courtesy of Dr Stan Erlandsen, U.S Center for Disease Control and Prevention.)
Trang 222 Some Basic Intensity Transformation Functions
❖Piecewise Linear Transformation Functions
➢ Bit-Plane Slicing
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
Image reconstructed from bit planes: (a) 8 and 7; (b) 8, 7, and 6; (c) 8, 7, 6, and 5.
Trang 233 Histogram Processing
❖Histogram
➢ The unnormalized histogram:
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
where r k is the k-th intensity level of an L-level
digital image f(x, y); n k is the number of pixels
in f with intensity r k and the subdivisions of the
intensity scale are called histogram bins.
là biểu đồ tần xuất thống kê số lần xuất
hiện các mức sáng trong ảnh
Trang 243 Histogram Processing
❖Histogram
➢ The normalized histogram:
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
where M and N are the number of image rows and columns, respectively.
𝑘=1 𝐿−1
Trang 253 Histogram Processing
❖Histogram
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
Four image types and their corresponding histograms (a) dark; (b) light; (c) low contrast; (d) high contrast
The horizontal axis of the histograms are values of r k and the vertical axis are values of p(r k)
Trang 263 Histogram Processing
cân bằng, giá trị các điểm ảnh không bị co cụm tại một khoảng nhỏ mà được
"kéo dãn" ra Cân bằng histogram là một phương pháp tiền/hậu xử lí ảnh rất
mạnh mẽ Đặc biệt trong nhiều bài toán mình từng làm trong lĩnh vực compute
vision, phương pháp tiền xử lí ảnh này cho chất lượng dữ liệu rất cao, cải thiện
chất lượng model deep learning rất nhiều
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
a) T(r) is a monotonic increasing (tăng đơn điệu) function in the interval 0 r
L-1; and
b) 0 T(r) L-1 for 0 r L-1
Trang 273 Histogram Processing
➢ The inverse transformation
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
(a’) T(r) is a strictly monotonic increasing function in the interval 0 r L-1
➔ The mappings from s back to r will be one-to-one,
thus preventing ambiguities
Trang 28First Program in OpenCV
Mat img = imread( "lena.jpg" );
namedWindow( "DisplayImage" , WINDOW_NORMAL ); imshow( "DisplayImage" , img);
waitKey(0);
}
Trang 29imwrite( "lena_800x400.jpg" , img_resize);
imshow( "Source image" , img);
imshow( "Resize Image" , img_resize);
waitKey(0);
return 0;
}
Trang 30• Mat img = imread( "lena.jpg" , cv:: IMREAD_COLOR ); // (512, 512, 3)
• //cout << "Width : " << img.size().width << endl;
• //cout << "Height: " << img.size().height << endl;
• //cout << "Width : " << img.cols << endl;
• //cout << "Height: " << img.rows << endl;
• int startX = 120, startY = 50, width = 280, height = 350;
• Mat ROI(img, Rect (startX, startY, width, height));
• Mat croppedImage;
• // Copy the data into new matrix
• ROI.copyTo(croppedImage);
• imshow( "Source image" , img);
• imshow( "Resize Image" , croppedImage);
• waitKey(0);
• return 0;
• }
Trang 31const float* histRange = { range };
Histogram 3 channel in OpenCV
Trang 32/// Compute the histograms:
calcHist(&bgr_planes [ 0 ] , 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate); calcHist(&bgr_planes [ 1 ] , 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate); calcHist(&bgr_planes [ 2 ] , 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);
// Draw the histograms for B, G and R
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound(( double )hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3 , Scalar(0, 0, 0));
/// Normalize the result to [ 0, histImage.rows ]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX , -1, Mat());
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX , -1, Mat());
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX , -1, Mat());
/// Draw for each channel
for ( int i = 1; i < histSize; i++)
{line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(b_hist.at< float >(i - 1))),
Point(bin_w*(i), hist_h - cvRound(b_hist.at< float >(i))),
Scalar(255, 0, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(g_hist.at< float >(i - 1))),
Point(bin_w*(i), hist_h - cvRound(g_hist.at< float >(i))),
Scalar(0, 255, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(r_hist.at< float >(i - 1))),
Point(bin_w*(i), hist_h - cvRound(r_hist.at< float >(i))),
Scalar(0, 0, 255), 2, 8, 0);
}
/// Display
imshow( "Source image" , src);
namedWindow( "calcHist Demo" , CV_WINDOW_AUTOSIZE );
imshow( "calcHist Demo" , histImage);
waitKey(0);
return 0;
}
Trang 34Mat gray = imread( "lena.jpg" , 0);
namedWindow( "Gray" , 1); imshow( "Gray" , gray);
calcHist(&gray, 1, 0, Mat (), hist, 1, &histSize, ranges, true , false );
// Show the calculated histogram in command window
double total;
total = gray.rows * gray.cols;
for ( int h = 0; h < histSize; h++)
Trang 35// Plot the histogram
Trang 37For example the original gray levels are [100,150] and the grey target levels are [0, 255]
g(f)=((f-100)/50)*255 of fk =100, fk+1=150, gk=0, gk+1=255
a=?, b=?
Trang 403 Histogram Processing
❖Histogram Equalization
Rafael C Gonzalez, Richard E Woods, “Digital image processing,” Pearson (2018).
(a) Monotonic increasing function, showing how multiple values can map to a single value (b) Strictly monotonic increasing function This is a one-to-one mapping, both ways.