OpenCV Learning Note 2: Turn on the camera and handle the captured image __opencv

Source: Internet
Author: User

It was a bit too gradual, a waste of time, and later code annotations as much as possible in the comments, concise and clear. Open Camera

#include "cv.h"//OPENCV core function library #include "highgui.h"//gui function library//Main method (note the formal parameter list under vs) int main (int argc, char** argv) {/
    /Create window (window name, Window property flag)//int Cvnamedwindow (const char* name, int flags=cv_window_autosize);

    Cvnamedwindow ("Win"); Cvcapture structure, which is used to hold the image capture information//cvcreatecameracapture (int index); Initialize the video from the camera and only one camera when the index number takes 0 cvcapture* capture = CVCR

    Eatecameracapture (0);

    Iplimage is a structural body iplimage* frame which represents an image;

        while (1) {//cvqueryframe crawls a frame from a camera or file in a dead loop, and then extracts and returns the frame = Cvqueryframe (capture);  Fetch failure when the return value is NULL//failure is often due to the camera index number is not correct or just initialized in the grab frame (camera boot need time) if (null==frame) break;

        Cvshowimage ("Win", frame);
        When the delay is less than or equal to 0, if there is no keyboard trigger, wait until the return value is-1//Otherwise the return value is the keyboard-pressed code//when delay is greater than 0 o'clock, if there is no trigger for the keyboard, wait delay milliseconds, the return value is-1 Otherwise the return value is the keyboard-pressed code character//int Cvwaitkey (inT delay=0);

        char C = cvwaitkey (50);
    If the return value is (ESC's ASCII) if (c =) break;//exits the loop}//cvreleasecapture: Frees up the memory space that the cvcapture structure creates

    Cvreleasecapture (&capture);

    Cvdestroywindow: Destroys a window with a specified name Cvdestroywindow ("Win");
return 0; }

Artwork-> grayscale-> two value

#include "cv.h" #include "highgui.h" int main (int argc, char** argv) {//Create window (window name, Window property flag) Cvnamedwindow ("captured artwork")
    ;
    Cvnamedwindow ("After the turn of gray-scale map");

    Cvnamedwindow ("second-valued");
    Cvcapture structure, which is used to hold the information of image capture cvcapture* capture = cvcreatecameracapture (0);

    Iplimage is a structural body iplimage* frame which represents an image;
        while (true) {/**----camera-> The original image----**///crawl and return a frame = Cvqueryframe (capture);

        Crawl failure if (null==frame) break;
        /**----Grayscale and two-valued map space opening----**///Create image allocation storage space (image size, image depth, channel number);
        iplimage* Cvcreateimage (cvsize cvsize (int width, int height), int depth, int channels);
        Here ipl_depth_8u is a 8-bit unsigned integer iplimage *dst_grey = Cvcreateimage (Cvgetsize (frame), ipl_depth_8u, 1);

        Iplimage *dst_binary = Cvcreateimage (Cvgetsize (frame), ipl_depth_8u, 1);
        /**----Original-> grayscale map----**///Color space conversion (input image, output image, conversion code);
        void Cvcvtcolor (const cvarr* SRC, cvarr* dst, int code); Here CV_bgr2gray is from RGB to grayscale Cvcvtcolor (frame, Dst_grey, Cv_bgr2gray);
        /**----Grayscale-> Two value----**///fixed threshold operation for single channel array (input image, output image, threshold, maximum value, threshold type);
        void Cvthreshold (const cvarr* SRC, cvarr* DST, double threshold, double max_value, int threshold_type);

        Here Cv_thresh_otsu the function uses the OTSU law to look for thresholds, no longer using their own threshold values Cvthreshold (Dst_grey, dst_binary,, 255, Cv_thresh_otsu);
        /**----Display the image in the specified window----**/cvshowimage ("captured artwork", frame);
        Cvshowimage ("After the gray-scale map", Dst_grey);

        Cvshowimage ("second-valued", dst_binary);
        50ms or with key char C = Cvwaitkey (50);
            Exit if (c = = 27) {//Free space Cvreleaseimage (&dst_binary) when pressing ESC;
            Cvreleaseimage (&dst_grey);
        Break
    }//Free space cvreleaseimage (&frame);
    Cvreleasecapture (&capture);
    Destroy All Windows Cvdestroyallwindows ();
return 0; }

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.