Opencv learning: convert an image into a binary image (function cvtcolor and function threshold)

Source: Internet
Author: User

I want to change the csdn account's profile picture to my real profile picture, but I don't want to change it so directly. I just want to convert the profile picture into a two-value diagram, because the original image cannot be pushed out from the binary image. This process requires two functions of opencv. The first function is to convert a color image to a grayscale image: cvtcolor function; the next function is to convert a grayscale image to a binary image function: threshold function. The Code is as follows:

    cvtColor(img_origin,img_gray,CV_BGR2GRAY);    threshold(img_gray,img_binary,145,255,THRESH_BINARY);    imwrite("/home/hon/result.jpg",img_binary);    imshow("binary image",img_binary);

Now that we have talked about these two functions, let's talk about the usage of these two functions. These two functions are c ++ series functions in opencv, and the functions do not have a prefix CV (most of the opencv functions described in the reference books are C series functions with a prefix cv ).

Cvtcolor function:

Prototype:

 void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )

SRC and DST are the image to be converted (SRC) and the image to be converted (DST), respectively. Code is a mask that indicates how the conversion from SRC to DST is performed, for example, whether the color is converted to the gray level or the color is converted to the HSI mode. The last dstcn indicates the number of bands of the DST image. The default value is 0, which can be inferred from the parameter code.

The code mode includes:

Cv_rgb2gray: <color image --- grayscale image>

Cv_bgr2ycrcb, cv_rgb2ycrcb, cv_ycrcb2bgr, cv_ycrcb2rgb <BGR space --- ychcb space>

Cv_bgr2hsv,
Cv_rgb2hsv, cv_hsv 2bgr, cv_hsv 2rgb<RGB space --- HSV space>

For more information, see opencv 2.4.5 documentation: http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#void%20cvtColor%28InputArray%20src,%20OutputArray%20dst,%20int%20code,%20int%20dstCn%29

Threshold function:

Prototype:

double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)

SRC and DST are the image to be processed (SRC) and the binary image (DST) generated by Src respectively; thresh is the threshold, and the so-called threshold function must have a threshold; maxval is used in some modes, and type is the mode.

The code mode includes:

Value Calculation Method
Thresh_binary
Thresh_binary_inv
Thresh_trunc
Thresh_tozero
Thresh_tozero_inv

See related documentation: opencv 2.4.5 documentation: http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html? Highlight = threshold # cv. Threshold

Note:: The threshold function isSingle Channel Image, This must be noted!

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.