Preface
In the previous article, we explained the changes in brightness and contrast in image processing, and this article we'll do a threshold function.
The simplest method of image segmentation
The threshold is the simplest method of image segmentation.
For example, in order to separate the apples from the image below, we use the gray difference between the foreground and the background, by setting a threshold value, which is black when the pixel is greater than this threshold, and is less than gray.
Five types of threshold values
As with OPENCV, we will provide five types of thresholds for ease of use.
The following is the waveform representation of the original image, the ordinate indicates the pixel gray value size, the Blue line is the threshold size.
Binary threshold value
The formula representation is:
\texttt{thresh}$}{0}{otherwise}" src="http://files.jb51.net/file_images/article/201301/2013010314344053.png">
The image representation is:
The maximum value (that is, 255) that exceeds this threshold is visible, otherwise the minimum value (that is, 0) is changed. We need a function to implement this function:
Copy Code code as follows:
var cv_thresh_binary = function (__value, __thresh, __maxval) {
return __value > __thresh? __maxval:0;
};
anti-binary threshold value
The formula representation is:
\texttt{thresh}$}{\texttt{maxVal}}{otherwise}" src="http://files.jb51.net/file_images/article/201301/2013010314344055.png">
The image representation is:
This, in turn, exceeds the threshold to the minimum, otherwise it becomes the maximum value. The function implementation is:
Copy Code code as follows:
var cv_thresh_binary_inv = function (__value, __thresh, __maxval) {
return __value > __thresh? 0: __maxval;
};
truncation threshold Value
The formula representation is:
\texttt{thresh}$}{\texttt{src}(x,y)}{otherwise}" src="http://files.jb51.net/file_images/article/201301/2013010314344057.png">
The image representation is:
This is truncated when it is above the threshold value. The function implementation is:
Copy Code code as follows:
var cv_thresh_trunc = function (__value, __thresh, __maxval) {
return __value > __thresh? __thresh:0;
};
threshold value to 0
The formula representation is:
\texttt{thresh}$}{0}{otherwise}" src="http://files.jb51.net/file_images/article/201301/2013010314344059.png">
The image representation is:
This is less than the threshold value of all to 0 processing. Function implementation:
Copy Code code as follows:
var Cv_thresh_tozero = function (__value, __thresh, __maxval) {
return __value > __thresh? __value:0;
};
anti-threshold value to 0
The formula representation is:
\texttt{thresh}$}{\texttt{src}(x,y)}{otherwise}" src="http://files.jb51.net/file_images/article/201301/2013010314344061.png">
The image representation is:
This is set to 0 when the threshold is exceeded, and the function implementation is:
Copy Code code as follows:
var cv_thresh_tozero_inv = function (__value, __thresh, __maxval) {
return __value > __thresh? 0: __value;
};
threshold processing Function Implementation
And then we do a function on the whole picture for the above types of threshold processing.
Copy Code code as follows:
var threshold = function (__SRC, __thresh, __maxval, __thresholdtype, __DST) {
(__src && __thresh) | | Error (Arguments.callee, is_undefined_or_null/* {line} */);
if (__src.type && __src.type = "Cv_gray") {
var width = __src.col,
Height = __src.row,
SData = __src.data,
DST = __DST | | New Mat (height, width, cv_gray),
Ddata = Dst.data,
Maxval = __maxval | | 255,
Threshouldtype = __thresholdtype | | Cv_thresh_binary;
var i, j, offset;
for (i = height; i--;) {
for (j = width; j--;) {
offset = i * width + j;
Ddata[offset] = Threshouldtype (Sdata[offset], __thresh, maxval);
}
}
}else{
Error (Arguments.callee, unspport_data_type/* {line} */);
}
return DST;
};
This function is relatively simple, that is, the value of each pixel is assigned to
Copy Code code as follows:
Threshouldtype (Sdata[offset], __thresh, maxval)
The value returned.