Home > Web Front-end > JS Tutorial > body text

Javascript image processing—threshold function example application_javascript skills

WBOY
Release: 2016-05-16 17:44:43
Original
1536 people have browsed it
Foreword

In the previous article , we explained the changes in brightness and contrast in image processing. In this article we will make a threshold function.

The simplest image segmentation method

Thresholding is the simplest image segmentation method.

For example, in order to segment an apple from the picture below, we use the grayscale difference between the foreground and the background to set a threshold. If the pixel is greater than this threshold, it will be represented in black, and if it is less than the threshold, it will be represented in gray.

Threshold simple example
Five threshold types

Like OpenCV, we will provide five threshold types for easy use.

The following is the waveform representation of the original image. The ordinate represents the gray value of the pixel, and the blue line is the threshold size.

Threshold Binary

Binary Thresholding

The formula is:

texttt{dst} (x,y) = fork{texttt{maxVal}}{if $texttt{src}(x,y) > texttt{thresh}$}{0}{otherwise}

The image representation is:

Threshold Binary

It can be seen that if the threshold is exceeded, it becomes the maximum value (that is, 255), otherwise it becomes the minimum value (that is, 0). We need a function to implement this function:

Copy code The code is as follows:

var CV_THRESH_BINARY = function(__value, __thresh, __maxVal){
return __value > __thresh ? __maxVal : 0;
};

Inverse binary thresholding

The formula is:

texttt{dst} (x,y) = fork{0}{if $texttt{src}(x,y) > texttt{thresh}$}{texttt{maxVal}}{otherwise}

The image representation is:

Threshold Binary Inverted

This is the opposite, if it exceeds the threshold, it becomes the minimum value, otherwise it becomes the maximum value. The function implementation is:

Copy code The code is as follows:

var CV_THRESH_BINARY_INV = function(__value, __thresh, __maxVal){
return __value > __thresh ? 0 : __maxVal;
};

Truncate thresholding

The formula is:

texttt{dst} (x,y) = fork{texttt{threshold}}{if $texttt{src}(x,y) > texttt{thresh}$}{texttt{src}(x,y)}{otherwise}

The image representation is:

Threshold Truncate

It can be seen that this is truncated if it exceeds the threshold. The function implementation is:

Copy code The code is as follows:

var CV_THRESH_TRUNC = function(__value, __thresh, __maxVal){
return __value > __thresh ? __thresh : 0;
};

Threshold to 0

The formula is:

texttt{dst} (x,y) = fork{texttt{src}(x,y)}{if $texttt{src}(x,y) > texttt{thresh}$}{0}{otherwise}

The image representation is:

Threshold Zero

In this case, all values ​​smaller than the threshold are set to 0. Function implementation:

Copy code The code is as follows:

var CV_THRESH_TOZERO = function(__value, __thresh, __maxVal){
return __value >


The formula is: texttt{thresh}$}{texttt{src}(x,y)}{otherwise}" src="http://files.jb51.net/file_images/article/201301/2013010314344061.png" >

The image representation is:

texttt{dst} (x,y) = fork{0}{if $texttt{src}(x,y) >This is set to 0 when the threshold is exceeded. The function implementation is: </P><P></P><P><IMG class=align-center style=Copy code


The code is as follows:

Threshold processing function Implement



Then we make a function to perform the above types of threshold processing on the entire image.

Copy code


The code is as follows:

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, assigning a value to each pixel as




Copy code


The code is as follows:
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template