Using opencv to read the pixels of the image's mouse points is a bug.

Source: Internet
Author: User


Skyseraph
Source: http://www.cnblogs.com/skyseraph/

The following code is widely used on the Internet.

However, after debugging and running, it is found that the function is correct, but the memory runs out quickly, resulting in a crash. After searching, add:


Cvreleaseimage (& img1); // release the memory occupied by the source Image


This line is added by me (szliug), otherwise the memory will soon be exhausted and will crash.

And then run normally.



/* ===================================================== ============ //

Function: opencv utility: Reading image pixels Value
Time: 4/4/2011 skyseraph hqu
// ================================================ =========== */
# Include "iostream"
# Include "cv. H"
# Include "highgui. H"
Using namespace STD;

# Include "math. H"

# Pragma comment (Lib, "highgui. lib ")
# Pragma comment (Lib, "cv. lib ")
# Pragma comment (Lib, "cvaux. lib ")
# Pragma comment (Lib, "cxcore. lib ")

// Source Image
Const char * filename = "../picture/flo.jpg ";

// Hide the console window
# Pragma comment (linker, "/subsystem: \" WINDOWS \ "/entry: \" maincrtstartup \"")


/* ===================================================== ======= */
Void mousehandler (INT event, int X, int y, int flags, void * PARAM)
{
Iplimage * img0, * img1;
Img0 = (iplimage *) Param;
Img1 = cvcloneimage (img0 );
/* ------------- Cvinitfont -------------------//
// Function: Initialize the font Structure
// Prototype: void cvinitfont (
Cvfont * font, // initialized font Structure
Int font_face, // font name identifier
$ Cv_font_hershey_simplex-normal-sized unlined font.
$ Cv_font_hershey_plain-specifies the font of the no-line model.
$ Cv_font_hershey_duplex-normal size unlined font. (More complex than cv_font_hershey_simplex)
$ Cv_font_hershey_complex-the normal size has a linefeed font.
$ Cv_font_hershey_triplex-normal size with lined fonts (more complex than cv_font_hershey_complex)
$ Cv_font_hershey_complex_small-cv_font_hershey_complex.
$ Cv_font_hershey_script_simplex-handwritten font.
$ Cv_font_hershey_script_complex-more complex than cv_font_hershey_script_simplex.
// This parameter can be combined by a value and selectable cv_font_italic font mark, which is a Italic
Double hscale, // font width.
// If it is equal to 1.0f, the character width is the original font width. If it is f, the character width is half of the original font width.
Double vscale, // font height.
Double Shear = 0, // the slope of the font.
// When the value is 0, the characters are not skewed; when the value is 1.0f, the font is skewed to 45 degrees, and so on. The thickness is highlighted by letters. The cvline function is used to draw letters.
Int Thickness = 1, // font stroke width
Int line_type = 8 // font stroke type
);
//--------------------------------------------*/
Cvfont font;
Uchar * PTR;
Char Label [20];
Cvinitfont (& font, cv_font_hershey_plain,. 8,. 8, 0, 1, 8 );
If (event = cv_event_lbuttondown) // click the left button
{
/* Read pixel */
PTR = cvptr2d (img1, Y, X, null); // returns a pointer to a special array element & cvget2d returns an element pointing to a special Array
/* Display the BGR value */
Sprintf (label, "(% d, % d, % d)", PTR [0], PTR [1], PTR [2]);
Cvrectangle (
Img1,
Cvpoint (X, Y-12 ),
Cvpoint (x + 100, Y + 4 ),
Cv_rgb (255, 0, 0 ),
Cv_filled,
8, 0
);
Cvputtext (
Img1,
Label,
Cvpoint (x, y ),
& Font,
Ct_rgb (255,255, 0)
);
Cvshowimage ("IMG", img1 );
}

Cvreleaseimage (& img1); // release the memory occupied by the source image. This line is added by me (szliug). Otherwise, the memory will soon run out and it will crash.

}

Int main (INT argc, char ** argv)
{
Iplimage * IMG;
/* Usage: <prog_name> <image> */
// If (argc! = 2)
//{
// Printf ("Usage: % S <image> \ n", argv [0]);
// Return 1;
//}
/* Load image */
IMG = cvloadimage (filename, 1 );
/* Always check */
Assert (IMG );
/* Create a window and install mouse handler */
Cvnamedwindow ("IMG", 1 );
// Register a mouse event
Cvsetmousecallback ("IMG", mousehandler, (void *) IMG );
Cvshowimage ("IMG", IMG );
Cvwaitkey (0 );
/* Be tidy */
Cvdestroyallwindows ();
Cvreleaseimage (& IMG );
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.