Javascript Image Processing-threshold function example application

Source: Internet
Author: User

Preface

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

Simplest Image Segmentation Method

The threshold is the simplest method for image segmentation.

For example, to split an apple, we use the gray difference between the foreground and the background to set a threshold. When the pixel is greater than this threshold, it is expressed in black. If the pixel is smaller than this threshold, it is indicated in gray.


Five threshold types

Like OpenCV, we provide five threshold types for ease of use.

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

Binary threshold

The formula is:

\ Texttt {thresh }$ }{ 0} {otherwise} "src =" http://www.bkjia.com/uploads/allimg/131017/0A525E10-2.png ">

Image Representation:

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 codeThe Code is as follows:
Var CV_THRESH_BINARY = function (_ value, _ thresh, _ maxVal ){
Return _ value> _ thresh? _ MaxVal: 0;
};

Anti-binary threshold

The formula is:

\ Texttt {thresh }$ }{\ texttt {maxVal }}{ otherwise} "src =" http://www.bkjia.com/uploads/allimg/131017/0A5251Z4-4.png ">

Image Representation:

In turn, if the threshold is exceeded, it is changed to the minimum value. Otherwise, it is changed to the maximum value. Function implementation:

Copy codeThe Code is as follows:
Var CV_THRESH_BINARY_INV = function (_ value, _ thresh, _ maxVal ){
Return _ value> _ thresh? 0: _ maxVal;
};

Truncation threshold

The formula is:

\ Texttt {thresh }$ {\ texttt {src} (x, y)} {otherwise} "src =" http://www.bkjia.com/uploads/allimg/131017/0A5253060-6.png ">

Image Representation:

This is truncated when the threshold is exceeded. Function implementation:

Copy codeThe Code is as follows:
Var CV_THRESH_TRUNC = function (_ value, _ thresh, _ maxVal ){
Return _ value> _ thresh? _ Thresh: 0;
};

The threshold is 0.

The formula is:

\ Texttt {thresh }$ }{ 0} {otherwise} "src =" http://www.bkjia.com/uploads/allimg/131017/0A5255J2-8.png ">

Image Representation:

This means that all those smaller than the threshold value are processed as 0. Function implementation:

Copy codeThe Code is as follows:
Var CV_THRESH_TOZERO = function (_ value, _ thresh, _ maxVal ){
Return _ value> _ thresh? _ Value: 0;
};

The anti-threshold value is 0.

The formula is:

\ Texttt {thresh }$ {\ texttt {src} (x, y)} {otherwise} "src =" http://www.bkjia.com/uploads/allimg/131017/0A52541R-10.png ">

Image Representation:

This is set to 0 when the threshold value is exceeded. The function implementation is:
Copy codeThe Code is as follows:
Var CV_THRESH_TOZERO_INV = function (_ value, _ thresh, _ maxVal ){
Return _ value> _ thresh? 0: _ value;
};

Implementation of threshold processing functions

Then we create a function to process the threshold values of the above types for the entire graph.
Copy codeThe Code is 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, assigning values to each pixel
Copy codeThe Code is as follows:
ThreshouldType (sData [offset], _ thresh, maxVal)

Return Value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.