Because to read the research, and the direction of the image, the teacher said to engage in the direction of the image Matlab and OpenCV is the most commonly used, so before reading the code from C # and Java to go to C + +, and for later in-depth learning image to lay a good foundation. About
Take this small example to get a quick look at the whole process of reading from memory, manipulating images in memory, showing on the screen, and finally saving to the hard drive. I like to write the program into a method, using a few necessary parameters to make calls, you can prevent more and more code later, so that they do not understand.
1. First introduce several structural bodies
"1" iplimage:Intel Image processing Library (IPL)
Baidu Encyclopedia said this is the most important structure, because this type is equivalent to C # bitmap or a two-dimensional array to represent the image, but OpenCV has a unique structure.
"2" cvsize:
typedef struct CVSIZE
{int width;/* Rectangle width */int height;/* Rectangle high */} Cvsize;
In fact, the definition of a long, wide structure, why use it, their own definition is not good? The image needs to be long and wide when the image is created, so it needs cvsize to do the parameter.
2. Common function Introduction
① Image loading function (to read the image file on the hard disk into memory)
The function Cvloadimage loads the specified image file and returns a iplimage pointer to the file. The function supports images in formats such as BMP, JPG, PNG, TIFF, and so on.
Its function prototype is as follows:iplimage* cvloadimage (const char* filename, int iscolor);where filename is the name of the image to be loaded, including the extension of the image; IsColor is an auxiliary parameter entry with an optional positive number, 0 and negative three values, a positive value that is loaded as a three-channel (Cv_load_image_color) image, and 0 (cv_load_ Image_grayscale) indicates that the image is a single-channel image, and a negative number (cv_load_image_unchanged) indicates that the image-loaded channel is determined by the picture file itself.
② Create image (create first address and allocate storage space)
Its function prototype is as follows:
iplimage* cvcreateimage (cvsize size, int depth, int channels);
Creating an image requires first selecting the length and width of the cvsize to determine the shape size of the image.
depth of Image : The image depth is the number of bits used to store each pixel, as well as the color resolution of the image.
Depth the bit depth of an image element, which can be one of the following: ipl_depth_8u-unsigned 8-bit integer ipl_depth_8s-signed 8-bit integer ipl_depth_16u-unsigned 16-bit integer ipl_depth_16s- Signed 16-bit integer ipl_depth_32s-signed 32-bit integer ipl_depth_32f-single-precision floating-point number ipl_depth_64f-double-precision floating-point numbernumber of channels in the image: The number of channels of the grayscale graph is 1, the color graph channel is 3, describes a pixel point, if it is grayscale, then only need a numeric value to describe it, is a single channel. If a pixel, there are RGB three colors to describe it, is three channels
③ Window definition function (picture Frame, window is this frame)
The function Cvnamedwindow defines a window for displaying an image. Its function is prototyped as follows: int Cvnamedwindow (const char* name, unsigned long flags), where name is the window name, Flags is the window property metric value, you can choose Cv_window_ AutoSize and 2 kinds of values. Cv_window_autosize indicates that the window size is the same as the original image size, and 0 indicates that the image is displayed in a fixed window size. The general picture frame is matched with the photograph.
④ Image Display function (can finally be displayed, display is the specified frame is which, the photo is which, their combination shows on the screen)
The function cvshowimage is to display the image in the specified window, its function prototype is as follows: void Cvshowimage (const char* name, const cvarr* image), where name is the window name, image is the type pointer Usually the iplimage pointer
⑤Image save function (memory image saved to hard disk)
The function Cvsaveimage saves a pointer variable of type iplimage with the specified file name, and its function prototype is as follows: int cvsaveimage (const char* filename, const cvarr* image); FileName is the image save path and name, and image is the iplimage pointer variable.
If you do not enter a path, it is stored directly under the current project file.
⑥ Image Destruction function
The function Cvreleaseimage destroys the defined iplimage pointer variable, freeing up memory space.
Its function is prototyped as follows: void Cvreleaseimage (iplimage** image), where image is a defined iplimage pointer. After using the image to destroy it, and release the pointer is the same reason.
3. Doubts
~ What is the form of iplimage type? How do I manipulate pixels at any location?
~ What is the nature of the conversion of pixel points when shrinking? The picture reduces the pixel point should be less, because the final file size is correspondingly smaller, but the degree of transformation and the proportion of the scale seems to be not proportional to, which is a relationship?
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/opencv.hpp> #include < Opencv2/highgui/highgui.hpp>using namespace Cv;using namespace Std;int main () {void Imagezoom (char *,float,char *) ; Imagezoom ("Lenna.jpg", 0.314, "Lenna scale graph. jpg"); return 0; }//image Scaling Method/*1, original picture and image after zooming 2, Ken to adjust the length and width of the parameters according to the multiple adjustment 3, and finally saved to the current project, the name is ImageName scale diagram */void imagezoom (char *imagename,float Scale,char *savename) {const char *pstrwindowssrctitle = "original"; const char *pstrwindowsdsttitle = "Zoom graph"; Double Fscale = 0.3; Zoom multiplier cvsize czsize; Target image size//read image from file iplimage *psrcimage = Cvloadimage (imagename,cv_load_image_unchanged); Iplimage *pdstimage = NULL; Calculate the target image size Czsize.width = Psrcimage->width * Fscale; Czsize.height = Psrcimage->height * Fscale; Create image and Zoom Pdstimage = Cvcreateimage (Czsize, psrcimage->depth, psrcimage->nchannels); Cvresize (Psrcimage, Pdstimage,cv_inteR_area); Create Window Cvnamedwindow (Pstrwindowssrctitle, cv_window_autosize); Cvnamedwindow (Pstrwindowsdsttitle, cv_window_autosize); Displays the image cvshowimage (Pstrwindowssrctitle, Psrcimage) in the specified window; Cvshowimage (Pstrwindowsdsttitle, pdstimage); Wait for key event Cvwaitkey (); Save the picture, after use to destroy the window and picture Cvsaveimage (Savename,pdstimage); Cvdestroywindow (Pstrwindowssrctitle); Cvdestroywindow (Pstrwindowsdsttitle); Cvreleaseimage (&psrcimage); Cvreleaseimage (&pdstimage);}
OPENCV Introductory Learning (i) image scaling "simple proportional shrinkage, no technical content"