Annotate an opencv camera program

Source: Internet
Author: User

/* I have not systematically read the books of opencv. However, during this period, I want to create a program for camera identification and locating specific color blocks, and I have changed it from the existing one. This is a program for collecting real-time images from the camera. It also displays the foreground and background images of the collected images. According to my personal understanding, I made a comment. */

# Include <stdio. h>

# Include <cv. h>
# Include <cxcore. h>
# Include

Int main (INT argc, char ** argv)
{
// Declare the iplimage pointer. The iplimage pointer is used as the main processing parameter in subsequent camera image transmission, image processing, and image display. According to the instruction, the members mainly include: Size, version, number of channels, bit depth, color channel (Cross access or not), top/bottom left structure, width pixel, and high pixel, areas of interest to the image, the size of the image data, point to the arrangement, and arrange the size of the image row. Members ignored by opencv or left empty include alphachannel and colormodel [4.
Iplimage * pframe = NULL;

Iplimage * pfrimg = NULL;
Iplimage * pbkimg = NULL;

// Declare the cvmat pointer. According to this program, the initial understanding is that cvmat is mainly used in image processing. cvconvert (SRC, DST) can complete mutual conversion between iplimage and cvmat. Cvmat members include: type, row Data Length, data reference count, and row and column information (several Union). It seems that a matrix model similar to an image is easy to compute by understanding, according to a document, beginners need to be skilled in using it. The information is as follows:Cvmat usage and Examples

Cvmat * pframemat = NULL;
Cvmat * pfrmat = NULL;
Cvmat * pbkmat = NULL;

// Cvcapture has no public interface and can only be regarded as a parameter obtained by the video.

Cvcapture * pcapture = NULL;

Int nfrmnum = 0;

// Create a window. prototype: int cvnamewindow (const char * Name, int flag = cv_window_autosize)
Cvnamedwindow ("video", 1 );
Cvnamedwindow ("background", 1 );
Cvnamedwindow ("foreground", 1 );
// Arrange the window in sequence
Cvmovewindow ("video", 30, 0 );
Cvmovewindow ("background", 360, 0 );
Cvmovewindow ("foreground", 690, 0 );

If (argc> 2)
{
Fprintf (stderr, "Usage: bkgrd [video_file_name] \ n ");
Return-1;
}

// Open the camera. The prototype is iplimage * cvqueryframe (cvcapture * capture). The cvqueryframe function captures a frame from the camera or file, decompress the frame, and return the frame. This function is only a combination of cvgrabframe and cvretrieveframe. The returned image cannot be released or modified by the user.

If (argc = 1)
If (! (Pcapture = cvcapturefromcam (-1 )))
{
Fprintf (stderr, "can not open camera. \ n ");
Return-2;
}

// Open the video file
If (argc = 2)
If (! (Pcapture = cvcapturefromfile (argv [1])
{
Fprintf (stderr, "can not open video file % s \ n", argv [1]);
Return-2;
}

// Read the video frame by frame
While (pframe = cvqueryframe (
Pcapture ))
{
Nfrmnum ++;

// If it is the first frame, you need to apply for memory and initialize it. What is required for initialization is (cvsize (width and height), depth, and cross-Access)
If (nfrmnum = 1)
{
Pbkimg = cvcreateimage (cvsize (pframe-> width, pframe-> height), ipl_depth_8u, 1 );
Pfrimg = cvcreateimage (cvsize (pframe-> width, pframe-> height), ipl_depth_8u, 1 );

Pbkmat = cvcreatemat (pframe-> height, pframe-> width, cv_32fc1); // initialize the Matrix
Pfrmat = cvcreatemat (pframe-> height, pframe-> width, cv_32fc1 );
Pframemat = cvcreatemat (pframe-> height, pframe-> width, cv_32fc1 );

// Isn't the function of image processing just getting started?
// Convert to a single-channel image for reprocessing
Cvcvtcolor (pframe,
Pbkimg, cv_bgr2gray );
Cvcvtcolor (pframe,
Pfrimg, cv_bgr2gray );

Cvconvert (pfrimg,
Pframemat );
Cvconvert (pfrimg, pfrmat );
Cvconvert (pfrimg,
Pbkmat );
}
Else
{
Cvcvtcolor (pframe, pfrimg, cv_bgr2gray );
Cvconvert (pfrimg,
Pframemat );
// Gaussian filter first to smooth the image
// Cvsmooth (pframemat, pframemat, cv_gaussian, 3, 0, 0 );

// Subtract the current frame from the background image
Cvabsdiff (pframemat, pbkmat, pfrmat );

// Binarization foreground Diagram
Cvthreshold (pfrmat, pfrimg, 60,255.0, cv_thresh_binary );

// Perform morphological filtering to remove noise
// Cverode (pfrimg, pfrimg, 0, 1 );
// Cvdilate (pfrimg, pfrimg, 0, 1 );

// Update the background
Cvrunningavg (pframemat, pbkmat, 0.003, 0 );
// Convert the background to the image format for display
Cvconvert (pbkmat, pbkimg );

// Display the image
Cvshowimage ("video", pframe );
Cvshowimage ("background ",
Pbkimg );
Cvshowimage ("foreground ",
Pfrimg );

// If there is a button event, it will jump out of the loop
// This wait time also provides time completion display for the cvshowimage Function
// The wait time can be adjusted based on the CPU speed
If (cvwaitkey (2)> = 0)
Break;

}

}
// Destruction window
Cvdestroywindow ("video ");
Cvdestroywindow ("background ");
Cvdestroywindow ("foreground ");

// Release the image and Matrix
Cvreleaseimage (& pfrimg );
Cvreleaseimage (& pbkimg );

Cvreleasemat (& pframemat );
Cvreleasemat (& pfrmat );
Cvreleasemat (& pbkmat );

Cvreleasecapture (& pcapture );

Return 0;
}

Let's sum up the results of this program. This program annotation mainly focuses on image acquisition and display, but does not pay attention to how it is processed. The camera uses cvcapture * cvcapturefromcam (INT index) to transmit the real-time image information to iplimage. Finally, the processed iplimage is displayed by the cvshowimage (window, iplimage *) function. However, cvmat is limited to image processing and has nothing to do with input and output.

Next are some notes

Channel Number of each element (pixel. it can be 1, 2, 3, or 4. the channel is cross-access. For example, the general color image data arrangement is B0 G0 R0 B1 G1 R1... although the IPL image format can store non-Cross-access images and some opencv can also process them, this function can only create cross-access images.

Http://baike.baidu.com/view/3440672.htm

Highgui Reference Manual

Http://fsa.ia.ac.cn/opencv-doc-cn/opencv-doc-cn-0.9.7/ref/opencvref_highgui.cn.htm#decl_cvReleaseCapture

It's a classic story. It turned out to be translated by Shiqi Yu of FSA and Zhang zhaoxiang's predecessors.

Cvcvtcolor

Http://baike.baidu.com/view/2816025.htm

Cvconvert
Low Position, high precision, and high conversion

Http://hi.baidu.com/megachan/blog/item/8b166dc520f1f1bb8226acbc.html

Cvconvert (SRC, DST) performs two operations: Change the SRC image data type to the DST image data type; assign the SRC data to the DST

Void cvabsdiff (const cvarr * src1, const cvarr * src2, cvarr * DST );
DST (I) c = ABS (src1 (I) c-src2 (I) c ).
All arrays must have the same size (or ROI size) of the same data type)

From: http://www.cnblogs.com/mlv5/archive/2011/02/11/emy_yu.html

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.