The basic C interface in opencv for Imagine Processing

Source: Internet
Author: User
The basic C interface in opencv for Imagine Processing


Creating a window first, we want to show an image on the screen using highgui. Th e function that does this for us is Cvnamedwindow (). Th e function expects a name for the new window and one flag. the name appears at the top of the window, and the name is also used as a handle for the window that can be passed to other highgui functions. the flag indicates if the window shoshould autosize itself to fit an image we put into it.

Here is the full prototype:

Window title API

int cvNamedWindow(const char* name,int flags = CV_WINDOW_AUTOSIZE);


Notice the parameter flags. for now, the only valid options available are to set flags to 0 or to use the default setting, cv_window_autosize. if cv_window_autosize is set, then highgui resizes the window to fit the image. there after, the window will automatically resiically resize itself if a new image is loaded into the window but cannot be resized by the user. if you don't want autosizing, you can set this argument to 0; then users can resize the window as they wish. once we create a window, we usually want to put something into it. but before we do that, let's see how to get rid of the window when it is no longer needed. for this we useCvdestroywindow (), A function whose argument is a string: the name given to the window when it was created. in opencv, windows are referenced by name instead of by some unfriendly (and invariably OS-dependent) "handle ". conversion between handles
And names happens under the hood of highgui, so you needn't worry about it. having said that, some people do worry about it, and that's OK, too. for those people,

Highgui provides the following functions:

Obtain the window name

void* cvGetWindowHandle( const char* name );const char* cvGetWindowName( void* window_handle ); 
I have to speak out. These two APIs have a kind of tangle between Mr Egg and Mr ~


To resize a window, call (not surprisingly)


cvResizeWindow() :void cvResizeWindow(const char* name, int width,int height);


Here the width and height are in pixels and give the size of the drawable part of the window (which are probably the dimensions you actually care about ).

This API is used to set the display size of the image during Automatic pop-up (not manually adjusted), and the missing part is transparent.


Loading an image

Before we can display an image in our window, we'll need to know how to load an image from disk. The function for this is cvloadimage ():


IplImage* cvLoadImage(const char* filename , int iscolor = CV_LOAD_IMAGE_COLOR);


When opening an image,Cvloadimage ()Does not look at the file extension. instead, cvloadimage () analyzes the first few bytes of the file (aka its signature or "Magic sequence") and determines the appropriate code C using that. the second argument is color can be set to one of several values. by default, images are loaded as three-channel images with 8 bits per channel; the optional flag cv_load_image_anydepth can be added to allow loading of non-8-bit images. by default, the number of channels will be three because the is color flag has the default value of cv_load_image_color. this means that, regardless of the number of channels in the image

File, the image will be converted to three channels if needed.

This function does not determine how to load an image based on the image format extension (for example, jpg or PNG), but is determined based on the magic number of each file header. If the reader has never heard of the magic number, you can check out caspp. Here, the linking section mentioned magic number when talking about ELF format files)


Cv_load_image_color forcibly converts an image into a 3-channel image.
Cv_load_image_grayscale automatically converts an image into a suitable single-channel image (black and white image)

Cv_load_image_anycolor simply loads the image storage format without channel conversion.

This is my personal understanding. If you have any mistakes, I hope the readers will point it out.


If you want to load 16-channel images, set the second parameter to cv_load_image_color | cv_load_image_anydepth.

If you want both the color and depth to be loaded exactly "as is", you cocould instead use the all-purpose flag cv_load_image_unchanged.


Note that if cvloadimage () is incorrect, nothing will be processed during the runtime. The function only returns a null pointer.


The obvious complementary function to cvloadimage () is cvsaveimage (), which takes two arguments:

Save image

int cvSaveImage( const char* filename, const CvArr* image);


Recall that cvarr is kind of a C-style way of creating something equivalent to a base-class in an object-oriented language; wherever you see cvarr *, you can use an iplimage *. the cvsaveimage () function will store only 8-bit single-or three-channel images for most file formats. newer back ends for flexible image formats like PNG, Tiff or 2000allow storing 16-bit or even float formats and some allow four-channel images (BGR plus alpha) as well. the return value will be 1 if the Save was successful and shoshould be 0 if the Save was not. *




Displaying images


Now we are ready for what we really want to do, and that is to load an image and to put it into the window where we can view it and appreciate its profundity.

We do this via one simple function, cvshowimage ():


void cvShowImage(const char* name,const CvArr* image);

The first argument here is the name of the window within which we intend to draw. The second argument is the image to be drawn.
 
A simple image displayedCvshowimage ()Before we move on, there are a few other window-related functions you ought to know about. They are:

void cvMoveWindow( const char* name, int x, int y );void cvDestroyAllWindows( void );int cvStartWindowThread( void );

Cvmovewindow () simply moves a window on the screen so that its upper left corner is positioned at X, Y.


Cvdestroyallwindows () is a useful cleanup function that closes all of the windows and deallocates the associated memory.

 

On Linux and MacOS, cvstartwindowthread () tries to start a thread that updates the window automatically and handles resizing and so forth. A return value of 0 indicates that no thread cocould be started-for example, because there is no support for this feature in the version of opencv that you are using. note that, if you do not start a separate window thread, opencv can react to user interface actions only when it is explicitly given time to do so (this happens when your program invokes cvwaitkey (), as described next ).


Waitkey observe that inside the while loop in our window creation example there is a new function we have not seen before: cvwaitkey (). this function causes opencv to wait for a specified number of milliseconds for a user key stroke. if the key is pressed within the allotted time, the function returns the key pressed; * otherwise, it returns 0.

With the construction:


while( 1 ) {if( cvWaitKey(100)==27 ) break;}


We tell opencv to wait 100 MS for a key stroke. if there is no key stroke, then repeat ad infinitum. if there is a key stroke and it happens to have ASCII value 27 (The Escape key), then break out of that loop. this allows our user to leisurely peruse the image before ultimately exiting the program by hitting escape.


As long as we're re introducing cvwaitkey (), it is worth mentioning that cvwaitkey () can also be called with 0 as an argument. in this case, cvwaitkey () will wait indefinitely until a keystroke is wrongly Ed and then return that key. thus, in our example we coshould just as easily have used cvwaitkey (0 ). the difference between these two options wocould be more apparent if we were displaying a video, in which case we wowould want to take an action
(I. e., display the next frame) if the user supplied no keystroke.



Next, we will provide a test demo for the above interfaces.


/************************************************************code writer : EOFcode date : 2014.08.04code file : basic_imagine_interface_demo.ce-mail  : [email protected] [email protected]code purpose:This demo is coded for someone who is a beginner withOpenCV. If there is something wrong with my code, please touchme by e-mail. Thank you.************************************************************/#include "opencv2/highgui/highgui.hpp"#include "opencv2/highgui/highgui_c.h"#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/imgproc/imgproc_c.h"#include <stdio.h>int main(int argc,char* argv[]){const char* ptr_string = "Empty Castle . L";IplImage* img = cvLoadImage(argv[1],CV_LOAD_IMAGE_GRAYSCALE);/*The first parameter is the imagine file's path.The second parameter has a lot of choices:CV_LOAD_IMAGE_COLOR(defualt)imagine 3 channels each channel with 8-bits.(regardless the channel in imagine, the imagine would be read in as 3 channel)CV_LOAD_IMAGE_ANYDEPTHimagine could be non-8 bits per channel.CV_LOAD_IMAGE_ANYCOLOR  imagine is read as single channelCV_LOAD_IMAGE_UNCHANGED imagine is loaded as what it is and keep it unchange.*/printf("CV_LOAD_IMAGE_COLOR : %d\n",CV_LOAD_IMAGE_COLOR);printf("CV_LOAD_IMAGE_GRAYSCALE : %d\n",CV_LOAD_IMAGE_GRAYSCALE);printf("CV_LOAD_IMAGE_ANYDEPTH : %d\n",CV_LOAD_IMAGE_ANYDEPTH);printf("CV_LOAD_IMAGE_UNCHANGED : %d\n",CV_LOAD_IMAGE_UNCHANGED);cvNamedWindow(ptr_string,0);/*The second parameter of the function just have two choice:A:CV_WINDOW_AUTOSIZE would resize the window and fit into the size of imagineB:0                  would not set the window's size fit into the size of the imagine but let the programer call 'cvResizeWindow(const char* name,int width,int height)' to resize the window*/printf("CV_WINDOW_AUTOSIZE : %d\n",CV_WINDOW_AUTOSIZE);printf("\n\n");cvResizeWindow(ptr_string,100,500);//print out as 100*500 pixel,100 is width of the imagine and 500 is the height of the imagine.cvShowImage(ptr_string,img);//Obviously, show the picture that you inputed.cvSaveImage("/home/jasonleaster/The_L.jpg",img,0);/*resave the imagine as another file. In this case ,"The L" is the new file's name.*/while(1){if(cvWaitKey(5000) == 27){/*5000 is 5000 ms == 5sDuring this time, nothing would happen, but after this timeif we press down the 'Enter' key which's ASCII is 27,the cvWaitKey return the ascii code of the key, otherwise return 0.*/break;}}//pause and let the user see the picture.cvReleaseImage(&img);//Finally, release the struture, otherwise, memory leak !return 0;}


Running effect:







Related Article

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.