OPENCV Learning notes Read, modify, save images

Source: Internet
Author: User

Direct attached program and Effect diagram:

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/ highgui.hpp>
#include <iostream>

using namespace std;
using namespace CV;

int main (int argc,char** argv)
{
	
	Mat img;//original image
	Img=imread ("1.jpg", Cv_load_image_color);

	if (! Img.data)
	{
		cout<< "could not open" <<endl;
		return-1;
	}
	Mat gray_img;//Grayscale Image
	Cvtcolor (img,gray_img,cv_bgr2gray);

	Imwrite ("Gray_img.jpg", gray_img);

	Namedwindow ("Img", window_autosize);
	Namedwindow ("Gray_img", window_autosize);
	
	Imshow ("IMG", IMG);
	Imshow ("Gray_img", gray_img);
	Waitkey (0);

	return 0;
}
Analytical:

1, first, must contain the necessary library files and avoid naming duplicates;

2. Imread function: Loads an image from a file

C + +: Mat imread (const string& filename, int flags=1)

      python: cv2.imread (filename[, flags])  →retval  

        Parameters:   filename  –name of file to be Loaded.
                 flags  –flags specifying the color Type of a loaded image:                       Cv_load_imag E_anydepth -If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it To 8-bit.
                      Cv_load_image_color -I F set, always convert image to the color one
                    &N Bsp Cv_load_image_grayscale -If set, always convert IMAGE to the grayscale one
    &N Bsp                 >0  return a 3-channel color image.

Note

The current implementation The alpha channel, if any, was stripped from the output image. Use negative value if you need the alpha channel.
=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha channel).

3. Cvtcolor function: Converts an image from one color space to another.
C + +: void Cvtcolor (Inputarray src, outputarray dst, int code, int dstcn=0)

Python:cv2.cvtColor (SRC, code[, dst[, DSTCN]) →dst

Parameters: src–input image:8-bit unsigned, 16-bit unsigned (Cv_16uc ...), or single-precision floating-point.
DST–output image of the same size and depth as SRC.
Code–color Space Conversion Code (RGB GRAY (Cv_bgr2gray,cv_rgb2gray,cv_gray2bgr,cv_gray2rgb)RGB YCRCB (Cv_bgr2ycrcb,cv_rgb2ycrcb,cv_ycrcb2bgr,cv_ycrcb2rgb) RGB HSV (Cv_bgr2hsv,cv_rgb2hsv,cv_hsv2bgr,cv_hsv2rgb )).
DSTCN–number of channels in the destination image; If the parameter is 0, the number of the channels was derived automatically from SRC and code 4, Imwrite function: Saves an IMA GE to a specified file.

C + +: BOOL Imwrite (const string& filename, inputarray img, const vector<in t>& params=vector<int> ())

Python:cv2.imwrite (filename, img[, params]) →retval

Parameters: filename –name of the file.
img –image to be saved.
params –format-specific Save parameters encoded as pairs paramid_1, paramvalue_1,

Paramid_2, paramvalue_2, .... The following parameters is currently supported:for JPEG, it can b e a quality (cv_imwrite_jpeg_quality) from 0 to + (the higher is the better). Default value is 95.
For PNG, it can being the compression level (Cv_imwrite_png_compression) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.
For PPM, PGM, or PBM, it can be a binary format flag (cv_imwrite_pxm_bin ARY), 0 or 1. Default value is 1.



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.